c# – GridView asp.net中的DropDownList
发布时间:2020-12-16 01:47:07  所属栏目:百科  来源:网络整理 
            导读:我想为gridview中的每个条目添加一个下拉列表. asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onselectedindexchanged="GridView1_SelectedIndexChanged" Columns asp:TemplateField HeaderText="Bank" ItemTemplate asp:DropDow
                
                
                
            | 
 我想为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>在后端,我有以下代码,以便将数据表绑定到该下拉列表. DataTable reader = BusinessLayer.BusinessLayerHandler.GetBankList(); DropDown.DataSource = reader; DropDown.DataTextField = "NAME"; DropDown.DataValueField = "NAME"; DropDown.DataBind(); 我的问题是在后端找不到在网格视图(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 */ }
}(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
