无法从GridView标题行访问DropDownList控件

时间:2017-12-05 13:46:04

标签: c# asp.net gridview

我的GridView标头有问题。我无法从标题中访问DropDown控件,即使有一个。这就是我的网格看起来像

<asp:GridView runat="server" 
               ID="AcceptedRecordsGridview"
               AutoGenerateColumns="false"
               DataKeyNames="idn" 
               CssClass="table" 
               GridLines="none"
               AllowPaging="true" 
               OnPageIndexChanging="OnPaging" 
               AllowSorting="true"
               PageSize="4" >
    <Columns>
        <asp:TemplateField HeaderText="ID" SortExpression="idn">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("idn") %>'>
                </asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("idn") %>'>
                </asp:Label>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Message info" SortExpression="ninfo">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ninfo") %>'>
                </asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ninfo") %>'>
                </asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Unit ID" SortExpression="idb">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("idb") %>'>
                </asp:Label>
           </ItemTemplate>
           <EditItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%# Eval("idb") %>'>
               </asp:Label>
           </EditItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField>
           <HeaderTemplate>
               Status:
               <asp:DropDownList ID="ddlStatus"
                                 runat="server"
                                 OnSelectedIndexChanged = "StatusChanged" 
                                 AutoPostBack = "true"
                                 AppendDataBoundItems = "true" >
                   <asp:ListItem Text = "ALL" Value = "ALL">      
                   </asp:ListItem>
                   <asp:ListItem Text = "Top 10" Value = "10">
                   </asp:ListItem>
               </asp:DropDownList>
           </HeaderTemplate>
           <ItemTemplate>
               <asp:Label ID="Label2" runat="server" Text='<%# Bind("status") %>'>
               </asp:Label>
           </ItemTemplate>
           <EditItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%# Eval("status") %>'>
               </asp:Label>
           </EditItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Received" SortExpression="time">
           <ItemTemplate>
               <asp:Label ID="Label2" runat="server" Text='<%# Bind("time") %>'>
               </asp:Label>
           </ItemTemplate>
           <EditItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%# Eval("time") %>'>
               </asp:Label>
           </EditItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Solved" SortExpression="timeUpdated">
           <ItemTemplate>
               <asp:Label ID="Label2" runat="server" Text='<%# Bind("timeUpdated") %>'>
               </asp:Label>
           </ItemTemplate>
           <EditItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%# Eval("timeUpdated") %>'>
               </asp:Label>
           </EditItemTemplate>
       </asp:TemplateField>
   </Columns>
</asp:GridView>

我想从HeaderTeamplate标记访问下拉控件。我尝试了不同的解决方案,例如DropDownList ddlStatus = (DropDownList)AcceptedRecordsGridview.HeaderRow.FindControl("ddlStatus"); 我也尝试使用 RowDataBound 来获取控件,并尝试使用 foreach 语句循环遍历行,但每次都获得空引用异常。谁能告诉我我做错了什么?非常感谢你提前。

2 个答案:

答案 0 :(得分:1)

foreach方法不起作用,因为GridView.Rows永远不会返回页眉或页脚,只会返回RowTypeDataRow的行。

所以你必须使用RowDataBound,它在每个行(包括页眉和页脚)的数据绑定之后调用。因此,有必要检查RowType

protected void AcceptedRecordsGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        DropDownList ddlStatus = (DropDownList) e.Row.FindControl("ddlStatus");
    }
}

答案 1 :(得分:0)

试试这个

foreach (GridViewRow gr in AcceptedRecordsGridview.Rows)
{ 
  if (gr.RowType == DataControlRowType.Header)
  {
   DropDownList drop = gr.FindControl("DropDownList1") as DropDownList; 
  }

}