2012-01-03 7 views
1

Я пытаюсь использовать Search Box that is explained in this blog. Проблема теперь у меня есть GridView внутри 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> 

Мой код-Behind:

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

ответ

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; 
    } 
} 
+0

Или вы можете использовать метод FindControl ретранслятора. Поскольку теперь у вас уже есть ссылка на сетку, FindControl не требуется. – VikciaR

+0

Большое спасибо, Дидье Г. Я очень ценю вашу помощь. – user1093651