Asp.Net 当文本框在 gridview 中时,文本框未声明错误 header

Asp.Net textbox is not declared error when textbox is in gridview header

我正在向每列 header 正下方的 asp.net gridview 添加几个文本框,以便将它们用作过滤器输入字段。

问题是当我尝试 运行 它时,我在每个字段上都收到以下错误:

Error   3   'txtS_AP' is not declared. It may be inaccessible due to its protection level.  

如果我取出 gridview 之外的文本框,那么我不会收到任何错误,所以我假设 "special" 将字段放在 gridview 中。

如果有人可以帮助避免这种情况,我将不胜感激。 谢谢

<asp:TemplateField >
  <HeaderTemplate>
    <asp:LinkButton ID="lbAP" runat="server" Text="AP" style="color:white;" CommandName="Sort" CommandArgument="AP"></asp:LinkButton> <br />
    <asp:TextBox runat="server" ID="txtS_AP"  CssClass="STD_searchfields" AutoPostBack="true" ></asp:TextBox>
  </HeaderTemplate>     

      <ItemTemplate>                                       
           <table  >
           <tr >
           <td class="STD_normal" style="width:100px;"><%#Eval("AP")%></td>                                                    
           </tr>
            <tr>
           <td class="STD_Normal_Grey" style="width:100px; height:20px"><%#Eval("OUNCKN")%> (<%#Eval("ORC")%>)</td>
           </tr>
           </table>                                           
        </ItemTemplate>                                       
      <HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>

由于它在容器内,您必须在使用前声明并找到它。你可以这样做:

VB.NET:

Dim txtS_AP As TextBox = GridView1.HeaderRow.FindControl("txtS_AP")

C#

TextBox txtS_AP = (TextBox)GridView1.HeaderRow.FindControl("txtS_AP");

执行此操作后 - 您应该能够访问它的所有属性(这里假设 GridView1 是您的网格 ID。)