0

I have nested a Gridview2(child) and I want to get the checked rows on a button click.

When I try to access Gridview2 from button click event I am not able to do that. However, I can access the parent Gridview1.

Can somebody explain me how to get the Child Gridview's checked rows on button click.

Also, Button is a column header of child Gridview.

Here is my code.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Horizontal" 
    onrowdatabound="GridView1_RowDataBound" DataKeyNames="id1">
    <AlternatingRowStyle BackColor="#F7F7F7" />
    <Columns>

        <asp:TemplateField>
         <ItemTemplate>
                    <a href="javascript:collapseExpand('id1_<%# Eval("id1") %>');">
                        <img id="imageSubId_<%# Eval("id1") %>" alt="Click to show/hide orders" border="0"
                            src="Images/bullet_toggle_plus.jpg" /></a>
                </ItemTemplate>
            </asp:TemplateField>
        <asp:BoundField DataField="id1" HeaderText="ID" />
        <asp:TemplateField>
            <ItemTemplate>
            <tr>
            <td colspan="100%">
               <div id="rid1_<%# Eval("id1") %>" style="display: none; position: relative; left: 25px;">
                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" 
                    GridLines="None" OnRowCommand="Gridview2_RowCommand">
                    <Columns>

                    <asp:BoundField DataField="fname" HeaderText="First Name" />
                    <asp:BoundField DataField="mname" HeaderText="Middle Name" />
                    <asp:BoundField DataField="lname" HeaderText="Last Name" />
                    <asp:TemplateField>
                    <ItemTemplate>

                    <asp:CheckBox ID="checkselect" runat="server" />

                    </ItemTemplate>
                    <HeaderTemplate>
                        <asp:Button ID="Button4" runat="server" Text="Remove" CommandName="Split" OnClick="Button4_Click" />
                         <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="Button4" PopupControlID="Panel1" CancelControlID="Button1">
                        </ajaxToolkit:ModalPopupExtender>

                        </HeaderTemplate>

                    </asp:TemplateField> </Columns></asp:GridView>
                </div>
                </td>
                </tr>
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
       </asp:GridView>

1 Answer 1

1

What I'd do is add a CommandArgument to Button4's declaration that will allow me to find the related GridView2.

So, I'd use what you're using elsewhere, id1. Add CommandArgument='<%# Eval("id1") %>' to your Button4 declaration.

Now, in Button4_Click, you can cast the sender to a Button like so:

var button4 = (Button)sender;

Once you have button4 casted correctly to a Button, you can access the CommandArgument property.

var id1 = button4.CommandArgument;

Once you have id1, it's as simple as iterating through the parent GridView, GridView1. Looks like your second column is bound to id1 as well, so you'd do the following:

GridView foundGridView2;
foreach(GridViewRow gvr in GridView1.Rows)
{
    if(gvr.RowType == DataControlRowType.DataRow && gvr.Cells[1].Text == id1)
    {
        foundGridView2 = gvr.FindControl("GridView2");
        break; // Once we've found it, no need to iterate through other items
    }
}

At this point, you now have access to your found GridView2, and you can iterate through the rows of GridView2 and check the checkboxes. Let me know if you need help doing that.

Adding to the answer because I think the parent GridView1 may be swallowing Button4's click:

Create an event handler for GridView1's OnRowCommand event.

In the resulting GridView1_RowCommand() method, use the following to handle JUST your Button4 Click event:

if(e.CommandName == "Split")
{
    // At this point I would refactor out the code you put in Button4_Click 
    // into a separate method.  I'll give you an example:
    HandleButton4Click(e.CommandArgument); // e.CommandArgument will contain the id1
}
Sign up to request clarification or add additional context in comments.

6 Comments

Should I use 'id1_<%# Eval("id1")%>' or '<%# Eval("id1") %>'
You're trying to match it on `<asp:BoundField DataField="id1" HeaderText="ID" />', so if you preface it with "id1_", it will never match.
Thanks a ton Garrison. I truly appreciate this. Just last thing For some reason the button event is not firing. Is it because I am also using it in my ajaxmodalpopup.
I've never put a button in a HeaderTemplate like that, so all I can suggest is moving the Button out and above the GridView2 (but still in the TemplateField). If that doesn't work, the Click event may be being swallowed by the parent GridView1. If my first suggestion doesn't work, add an event handler for GV1's RowCommand, and check to see if it may be surfacing in there. You can use the CommandName to handle it in GV1.RowCommand method. Let me know if you need help with that.
Sure. Lets forget about Modal Extender. However, I am not able to call if (gvr.RowType == DataControlRowType.DataRow && gvr.Cells[1].Text == id1) { foundGridView2 = (GridView)gvr.FindControl("GridView2"); foreach (GridViewRow gvr2 in foundGridView2.Rows) { Response.Write(gvr2.Cells[1].Text); } }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.