簡體   English   中英

GridView asp.net中的DropDownList

[英]DropDownList in GridView asp.net

我想為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(例如,在RowDataBoundRowCreated事件中)

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 */ }
}

你可以通過grid.findcontrol找到grid databound eventdropdown

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM