GridView asp.net 中的 DropDownList [英] DropDownList in GridView asp.net
问题描述
我想为 gridview 中的每个条目添加一个下拉列表.
I want to add a dropdownlist to every entry in a 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>
在后端,我使用以下代码将数据表绑定到该下拉列表.
At the back end i have the following code in order to bind a datatable to that dropdown list.
DataTable reader = BusinessLayer.BusinessLayerHandler.GetBankList();
DropDown.DataSource = reader;
DropDown.DataTextField = "NAME";
DropDown.DataValueField = "NAME";
DropDown.DataBind();
我的问题是在网格视图(DropDown)中创建的下拉列表在后端找不到,好像它不存在..
My problem is that the drop down list created at the grid view (DropDown) is not found at the back end as if it doesn't exist..
我能做什么?
推荐答案
DropDownList
将为 GridView
中的每一个项目创建,因此可以't 是下拉列表的一个字段.不过,您可以检索单行的 DropDownList(例如,在 RowDataBound
或 RowCreated
事件中)
The DropDownList
will be created for every single item in the GridView
, so there can't be one field for the dropdownlists. Nevertheless, you can retrieve the DropDownList for a single row (e.g. in RowDataBound
or RowCreated
event)
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
参数.
Or you can use an event of the DropDownList
itself and access the sender
parameter.
<asp:DropDownList ID="DropDown" OnLoad="dropdownLoad" />
protected void dropdownLoad(object sender, EventArgs e)
{
DropDownList dropdown = sender as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
这篇关于GridView asp.net 中的 DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!