locked
If gridview 'B' is nested in a field in gridview 'A', how do I catch its events? RRS feed

  • Question

  • User297437924 posted

    Suppose I have a gridview with one of its columns being an itemtemplate field which itself contains a (nested) gridview with its associated sqldatasource. 

    I can get that to work, but if I wanted to add a SELECT statement to the nested gridview, and have the SELECT statement refer to a routine in the code-behind page, I fail.  The compiler will not recognize a routine such as:

      Protected Sub GridViewSearchText_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewSearchText.SelectedIndexChanged

    If GridViewSearchText is the inner nested gridview.  It does not know that the inner gridview exists, and so cannot handle events from it.

    I might be able to put a button control into the nested gridview, with an 'onClientClick' event that fires some javascript, but then the JavaScript would have to do call a routine in the code-behind.

    Is there a better way?

    Saturday, January 7, 2017 9:45 PM

Answers

  • User-1716253493 posted

    Not sure what you mean.

    The grid is the sender, the grid namingcontainer is gridviewrow of parent grid

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 8, 2017 5:27 AM

All replies

  • User-1716253493 posted

    Not sure what you mean.

    The grid is the sender, the grid namingcontainer is gridviewrow of parent grid

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 8, 2017 5:27 AM
  • User297437924 posted

    To: Oned_gk:

    This is what I mean by a nested gridview: (in this example, GridViewSearchText is the outer gridview, and GridViewQText is the inner Gridview.  If you try it out, there is simply no way to capture events from the inner gridview in such a way that you can identify the datakey of the inner gridview row that was selected, as well as the datakey of the outergridviewrow that contains the inner gridview. 

    <asp:GridView ID="GridViewSearchText" runat="server"   AllowSorting="True" visible ="False"
             pagesize="4"  Style="margin-top:12px" allowpaging="true"
            AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" DataKeyNames="IDst"
            BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSourceSearchText" ForeColor="Black" GridLines="Vertical">
            <AlternatingRowStyle BackColor="White" />
                     
            <Columns>
                  
          
                    <asp:BoundField DataField="numAnswers" HeaderText="Answers so far" SortExpression="numAnswers" />
               
                  
                    
                <asp:TemplateField HeaderText="Reason for Complaint" SortExpression="Question"  ItemStyle-Width="374" >
                    
                    <ItemTemplate>
                         <asp:SqlDataSource ID="SqlDataSourceReasonComplain" runat="server" 
                             ConnectionString="<%$ ConnectionStrings:shmoozerConnectionString %>" 
                             SelectCommand="GetHighlightQuestionTexts" SelectCommandType="StoredProcedure">
                     <SelectParameters>
                         <asp:Parameter DefaultValue="0" Name="IDst" Type="Int32" />
                          <asp:SessionParameter DefaultValue="0" Name="videoid" SessionField="videoid_zUsers" Type="Int32" />
                     </SelectParameters>
                 </asp:SqlDataSource>   
                <asp:GridView ID="GridViewQText" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="IDqt"
                    skinID="GridViewMocha" PageSize ="3"
                    DataSourceID="SqlDataSourceReasonComplain">
                           <emptydatatemplate>No detailed questions</emptydatatemplate>
                    <Columns>
                 <asp:TemplateField HeaderText="Select"  >
                  
                    <ItemTemplate>
                      <asp:Button ID="ButtonSelectQuestion" runat="server" Text="Answer this" OnClick="ButtonSelectQuestion_Click"></asp:Button>
                    </ItemTemplate>
                </asp:TemplateField>
                        <asp:BoundField DataField="Question" HeaderText="Question" SortExpression="Question" ReadOnly="true" />
                        <asp:BoundField DataField="DateEntered" HeaderText="Date Time" SortExpression="DateEntered" ReadOnly="true"  DataFormatString = "{0:yyyy-MM-dd HH:mm}"/>
                    </Columns>
                 </asp:GridView>       
                    </ItemTemplate>
    
                </asp:TemplateField>
            </Columns>
           
        </asp:GridView>

    Sunday, January 8, 2017 12:39 PM
  • User297437924 posted

    "namingcontainer" was the answer.  the namingcontainer of a nested gridview is the gridviewrow, and the namingcontainer of the gridviewrow is the gridview.

    Thanks Oned_gk.

    Sunday, January 8, 2017 7:44 PM