如何在GridView中修复此错误

本文关键字:错误 GridView | 更新日期: 2023-09-27 18:19:13

我正在尝试使用本博客中解释的搜索框。现在的问题是,我在中继器中有一个GridView。这个GridView将根据HiddenField的值生成。我使用了博客中解释的代码,但当我试图在我的情况下使用它时,我得到了一个错误,我不知道为什么我每次都得到它。我得到的错误来自于代码隐藏,错误是:

名称'GridView1'在当前上下文中不存在。

那么如何修正这个错误呢?

我的ASP。NET代码:

<input id=id_search type=text placeholder="Search">

     <br />  <br />  
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' />
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                                    SelectCommandType="StoredProcedure" SelectCommand="kbiReport"
                                    FilterExpression="[DivisionName] like '{0}%'">
                    <FilterParameters>
                        <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName" 
                                                 PropertyName="SelectedValue" Type="String" />
                    </FilterParameters>
                    <SelectParameters>
                        <%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values 
                            of GroupID--%>
                        <asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <asp:GridView ID="GridView1" runat="server" 
                                AllowSorting="True" 
                                CellPadding="3" 
                                DataSourceID="SqlDataSource1" 
                                ClientIDMode="Static"
                                CssClass="mGrid"
                                AlternatingRowStyle-CssClass="alt" 
                                RowStyle-HorizontalAlign="Center" 
                                OnRowDataBound="GridView1_RowDataBound" OnPreRender="GridView1_PreRender">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <HeaderStyle Font-Bold = "true" ForeColor="Black"/> 
                    <Columns>
                        <asp:CommandField ShowSelectButton="True" />
                    </Columns>
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>
            </ItemTemplate>
        </asp:Repeater>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                           ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                           SelectCommand="SELECT DISTINCT GroupID FROM courses">
        </asp:SqlDataSource>
我的后台代码:

protected void GridView1_PreRender(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            GridView1.UseAccessibleHeader = true;
            GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
    }

如何在GridView中修复此错误

当控件被放置在中继器(和其他带有模板的控件)中时,你不能直接引用它们。

在你的情况下,事件PreRender是由GridView触发的,所以sender必须我的网格:

protected void GridView1_PreRender(object sender, EventArgs e)
{
    var myGrid = sender as GridView;
    if (myGrid.Rows.Count > 0)
    {
        myGrid.UseAccessibleHeader = true;
        myGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
    }
}