gpt4 book ai didi

asp.net 如何以编程方式为大约 10 个下拉列表添加 TemplateField

转载 作者:行者123 更新时间:2023-12-04 07:08:00 28 4
gpt4 key购买 nike

这是我第三次问这个问题。我没有得到关于这个的好的答案。我希望我能得到一些帮助,但我会继续问这个问题,因为这是一个很好的问题,所以专家不应该忽视这个......

所以我有大约 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>

================================================== ======

添加于 5/3/09

这是我到目前为止所拥有的,但我仍然无法以编程方式添加下拉列表。
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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com