作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我第三次问这个问题。我没有得到关于这个的好的答案。我希望我能得到一些帮助,但我会继续问这个问题,因为这是一个很好的问题,所以专家不应该忽视这个......
所以我有大约 10 个下拉列表控件,我手动添加到 DetailsView 控件中,如下所示。我应该能够以编程方式添加它。请帮助,不要忽视...
<asp:DetailsView ID="dvProfile" runat="server"
AutoGenerateRows="False" DataKeyNames="memberid" DataSourceID="SqlDataSource1"
OnPreRender = "_onprerender"
Height="50px" onm="" Width="125px">
<Fields>
<asp:TemplateField HeaderText="Your Gender">
<EditItemTemplate>
<asp:DropDownList ID="ddlGender" runat="server"
DataSourceid="ddlDAGender"
DataTextField="Gender" DataValueField="GenderID"
SelectedValue='<%#Bind("GenderID") %>'
>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate >
<asp:Label Runat="server" Text='<%# Bind("Gender") %>' ID="lblGender"></asp:Label>
</ItemTemplate>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
private void PopulateItemTemplate(string luControl)
{
SqlDataSource ds = new SqlDataSource();
ds = (SqlDataSource)FindControl("ddlDAGender");
DataView dvw = new DataView();
DataSourceSelectArguments args = new DataSourceSelectArguments();
dvw = (DataView)ds.Select(args);
DataTable dt = dvw.ToTable();
DetailsView dv = (DetailsView)LoginView2.FindControl("dvProfile");
TemplateField tf = new TemplateField();
tf.HeaderText = "Your Gender";
tf.ItemTemplate = new ProfileItemTemplate("Gender", ListItemType.Item);
tf.EditItemTemplate = new ProfileItemTemplate("Gender", ListItemType.EditItem);
dv.Fields.Add(tf);
}
public class ProfileItemTemplate : ITemplate
{
private string ctlName;
ListItemType _lit;
private string _strDDLName;
private string _strDVField;
private string _strDTField;
private string _strSelectedID;
private DataTable _dt;
public ProfileItemTemplate(string strDDLName,
string strDVField,
string strDTField,
DataTable dt
)
{
_dt = dt;
_strDDLName = strDDLName;
_strDVField = strDVField;
_strDTField = strDTField;
}
public ProfileItemTemplate(string strDDLName,
string strDVField,
string strDTField,
string strSelectedID,
DataTable dt
)
{
_dt = dt;
_strDDLName = strDDLName;
_strDVField = strDVField;
_strDTField = strDTField;
_strSelectedID = strSelectedID;
}
public ProfileItemTemplate(string ControlName, ListItemType lit)
{
ctlName = ControlName;
_lit = lit;
}
public void InstantiateIn(Control container)
{
switch(_lit)
{
case ListItemType.Item :
Label lbl = new Label();
lbl.DataBinding += new EventHandler(this.ddl_DataBinding_item);
container.Controls.Add(lbl);
break;
case ListItemType.EditItem :
DropDownList ddl = new DropDownList();
ddl.DataBinding += new EventHandler(this.lbl_DataBinding);
container.Controls.Add(ddl);
break;
}
}
private void ddl_DataBinding_item(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
ddl.ID = _strDDLName;
ddl.DataSource = _dt;
ddl.DataValueField = _strDVField;
ddl.DataTextField = _strDVField;
}
private void lbl_DataBinding(object sender, EventArgs e)
{
Label lbl = (Label)sender;
lbl.ID = "lblGender";
DropDownList ddl = (DropDownList)sender;
ddl.ID = _strDDLName;
ddl.DataSource = _dt;
ddl.DataValueField = _strDVField;
ddl.DataTextField = _strDTField;
for (int i = 0; i < _dt.Rows.Count; i++)
{
if (_strSelectedID == _dt.Rows[i][_strDVField].ToString())
{
ddl.SelectedIndex = i;
}
}
lbl.Text = ddl.SelectedValue;
}
}
最佳答案
我找到了这篇文章 http://aspalliance.com/1125关于以编程方式创建模板字段。我希望这可以帮助解决这个问题。
关于asp.net 如何以编程方式为大约 10 个下拉列表添加 TemplateField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/816062/
在 Rails 中,您可以使用嵌套路由为 has_one 和 has_many 关系创建 RESTful 路由。可以在 Rails Guides 上找到示例 请问有没有什么好的方法可以为habtm关系
我是一名优秀的程序员,十分优秀!