DropDownList的GridView控件在asp.net [英] DropDownList in GridView asp.net
问题描述
我要添加一个DropDownList在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>
在后端我有以下code,以绑定一个DataTable到下拉列表中。
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();
我的问题是下拉在网格视图(下拉)创建的列表不是在后端发现,如果它不存在,..
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
,所以不能的是为dropdownlists一个字段。不过,你可以检索单行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
本身并访问发件人
参数。
<asp:DropDownList ID="DropDown" OnLoad="dropdownLoad" />
protected void dropdownLoad(object sender, EventArgs e)
{
DropDownList dropdown = sender as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
这篇关于DropDownList的GridView控件在asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!