我想为 GridView 中的每个条目添加一个下拉列表。
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Bank">
<ItemTemplate>
<asp:DropDownList ID="DropDown"
AutoPostBack="true" runat="server" DataTextField="Name" DataValueField="Name"
>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
在后端我有以下代码,以便将数据表绑定(bind)到该下拉列表。
DataTable reader = BusinessLayer.BusinessLayerHandler.GetBankList();
DropDown.DataSource = reader;
DropDown.DataTextField = "NAME";
DropDown.DataValueField = "NAME";
DropDown.DataBind();
我的问题是在后端找不到在 GridView (DropDown) 中创建的下拉列表,就好像它不存在一样。
我能做什么?
最佳答案
将为 GridView
中的每一项创建 DropDownList
,因此下拉列表不能只有一个字段。不过,您可以检索单行的 DropDownList(例如,在 RowDataBound
或 RowCreated
事件中)
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(r.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dropdown = e.Row.FindControl("DropDown") as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
}
或者您可以使用 DropDownList
本身的事件并访问 sender
参数。
<asp:DropDownList ID="DropDown" OnLoad="dropdownLoad" />
protected void dropdownLoad(object sender, EventArgs e)
{
DropDownList dropdown = sender as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
关于c# - GridView 中的 DropDownList asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7305905/