我正在尝试使用Search Box that is explained in this blog。问题是我在Repeater中有一个GridView。将根据HiddenField的值生成此GridView。我使用了博客中解释的代码,但是当我尝试在我的情况下使用它时,我收到一个错误,我不知道为什么每次都得到它。我得到的错误来自代码隐藏,错误是:
当前上下文中不存在名称“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;
}
}
答案 0 :(得分:2)
当将控件置于中继器(以及带模板的其他控件)中时,无法直接引用控件。
在您的情况下,事件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;
}
}