Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a gridview nested within a gridview both grids have checkboxes. how can i access controls of the 2nd gridview from just a button click? i do not need onrowcommand.

more straight forward of what i am trying to accomplish: 1. click button, 2. on button click event i want to know if a checkbox on the nested gridview is checked or not.


VB
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
                            ShowHeader="False" GridLines="None" style="font-size:9pt; width:100%;">
                            <Columns>
                                <asp:TemplateField>
                                <ItemTemplate>
                                <table>
                                <tr>
                                <td style="margin-removed10px;">
                                    <asp:CheckBox ID="CheckBox1" runat="server" /><asp:Label ID="Label1" style="font-weight:bold;" runat="server" Text='<%# Bind("RDMCatName") %>' Visible="True"></asp:Label><asp:Label ID="Label6" runat="server" Text='<%# Bind("RDMCatID") %>' Visible="False"></asp:Label>
                                </td>
                                </tr>
                                <tr>
                                <td>
                                <asp:Panel ID="pnlOrders" runat="server" Style="display: block; margin-left:20px;">
                                    <asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" 
                                        ShowHeader="False" GridLines="None" style="font-size:9pt;">
                                        <Columns>
                                        <asp:TemplateField>
                                        <ItemTemplate>
                                        <table>
                                        <tr>
                                        <td>
                                        <asp:CheckBox ID="CheckBox11" runat="server" /><asp:Label ID="Label100" runat="server" Text='<%# Bind("RDMCatSubName") %>' Visible="True"></asp:Label><asp:Label ID="Label101" runat="server" Text='<%# Bind("RDMCatSubID") %>' Visible="False"></asp:Label><asp:Label ID="Label7" runat="server" Text='<%# Bind("RDMCatSubCatID") %>' Visible="False"></asp:Label>
                                        </td>
                                        </tr>
                                        </table>
                                        </ItemTemplate>
                                        </asp:TemplateField>
                                        </Columns>
                                    </asp:GridView>
                                        <%--<asp:Label ID="reasonlabel" runat="server" Text="asdfasdf" />--%>
                                        <%--<asp:Label ID="longd" runat="server" Text='<%# Bind("TickDLongDescription") %>' />--%>
                                </asp:Panel>
                                </td>
                                </tr>
                                </table>
                                </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>


<asp:Button ID="IButton1" runat="server" Text="Insert" OnClientClick="return confirm('Insert This Product?')" />


code behind accessing grid a:
For Each row As GridViewRow In GridView3.Rows
                                                Dim CheckBox1 As CheckBox = CType(row.FindControl("CheckBox1"), CheckBox)
                                                Dim Label1 As Label = CType(row.FindControl("Label1"), Label)
                                                Dim Label6 As Label = CType(row.FindControl("Label6"), Label)
                                                If CheckBox1.Checked = True Then
                                                    nsel = nsel + 1
                                                Else

                                                End If
                                            Next
how do i do the same for gridview4?


What I have tried:

i am not really sure what to try other than google searching, but i can not find an example of what i am trying to do.
Posted
Updated 26-Feb-16 8:20am

Use FindControl[^]. When controls are nested you use FindControl to get the child control from the parent, then use FindControl on that control to find things inside of it.
 
Share this answer
 
Comments
PTG.Jake 26-Feb-16 13:00pm    
so..like this?

Dim GridView4 As GridView = CType(FindControl("GridView3").FindControl("GridView4"), GridView)
For Each row As GridViewRow In GridView4.Rows
Dim CheckBox11 As CheckBox = CType(row.FindControl("CheckBox1"), CheckBox)
Dim Label100 As Label = CType(row.FindControl("Label100"), Label)
Dim Label101 As Label = CType(row.FindControl("Label101"), Label)
Dim Label102 As Label = CType(row.FindControl("Label102"), Label)
If CheckBox11.Checked = True Then
cval2 = ""
Else

End If
Next
f-es you are correct, this is what i ended up doing.

VB
For Each r As GridViewRow In GridView3.Rows
                                                        Dim GridView4 As GridView = CType(r.FindControl("GridView4"), GridView)
                                                        For Each row As GridViewRow In GridView4.Rows
                                                            Dim CheckBox11 As CheckBox = CType(row.FindControl("CheckBox11"), CheckBox)
                                                            Dim Label100 As Label = CType(row.FindControl("Label100"), Label)
                                                            Dim Label101 As Label = CType(row.FindControl("Label101"), Label)
                                                            Dim Label102 As Label = CType(row.FindControl("Label102"), Label)
                                                            If CheckBox11.Checked = True Then
                                                                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.[GetType](), "showalert", "alert(' Checked!');", True)
                                                            Else
                                                                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.[GetType](), "showalert", "alert(' Not Checked!');", True)
                                                            End If
                                                        Next
                                                    Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900