flavour404
flavour404

Reputation: 6312

GridView, Accessing child GridView checkbox

I have a parent GridView that had a child GridView (code below), how do I get the value of the child gridview checkbox? And also, how do I save the state of the child gridview, i.e. if it is displayed or not? This is the function that is fired when the button is pressed that reads through the parent grid seeing which publications have been selected:

protected void DeleteSelectedProducts_Click(object sender, EventArgs e)
    {
        bool atLeastOneRowDeleted = false;

        // Iterate through the Products.Rows property
        foreach (GridViewRow row in GridView1.Rows)
        {
            // Access the CheckBox
            CheckBox cb = (CheckBox)row.FindControl("PublicationSelector");
            if (cb != null && cb.Checked)
            {
                // Delete row! (Well, not really...)
                atLeastOneRowDeleted = true;

                // First, get the ProductID for the selected row
                int productID =
                    Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

                // "Delete" the row
                DeleteResults.Text += string.Format(
                    "This would have deleted ProductID {0}<br />", productID);
                //DeleteResults.Text = "something";
            }


            // Show the Label if at least one row was deleted...
            DeleteResults.Visible = atLeastOneRowDeleted;
        }
    }

      <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" DataKeyNames="PublicationID" 
        DataSourceID="ObjectDataSource1" Width="467px" OnRowDataBound="GridView1_RowDataBound"
        Font-Names="Verdana" Font-Size="Small">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="PublicationSelector" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="NameAbbrev" HeaderText="Publication Name" SortExpression="NameAbbrev" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
            <asp:TemplateField HeaderText="Owners">
                <ItemTemplate>
                   <asp:Label ID="Owners" runat="server"></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
            <asp:TemplateField HeaderStyle-CssClass="hidden-column" ItemStyle-CssClass="hidden-column" FooterStyle-CssClass="hidden-column">
                <ItemTemplate>
                    <tr>
                        <td colspan="8" >
                            <div id="<%# Eval("PublicationID") %>" style="display: none; position: relative;" >
                                <asp:GridView ID="GridView2_ABPubs" runat="server" AutoGenerateColumns="false" Width="100%"
                                    DataKeyNames="PublicationID" Font-Names="Verdana" Font-Size="small">
                                    <Columns>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="ChildPublicationSelector" runat="server" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="NameAbbrev" HeaderText="Publication Name" SortExpression="NameAbbrev" />
                                    </Columns>
                                </asp:GridView>
                            </div>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData"
        TypeName="shoom_dev._Default">
    </asp:ObjectDataSource>

    <p>
        <asp:Button ID="DeleteSelectedProducts" runat="server" 
            Text="Delete Selected Products" onclick="DeleteSelectedProducts_Click" />
    </p>

    <p>
        <asp:Label ID="DeleteResults" runat="server" EnableViewState="False" Visible="False"></asp:Label>
    </p>

Upvotes: 0

Views: 3831

Answers (2)

flavour404
flavour404

Reputation: 6312

David, is this correct:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;

public static class FindControl<T>
{public static T FindControl<T>(this Control parent, string controlName) where T : Control 
    { 
    T found = parent.FindControl(controlName) as T;
    if (found != null)
        return found; 
    foreach (Control childControl in parent.Controls)
    { found = childControl.FindControl(controlName) as T; 
        if (found != null)
            break; 
    } 
    return found; }

}

The class is saved as FindControl.cs. How do I call the function?

I'm feeling a little retarded, but in my defence this is the first time that I have used extendor classes.

Well I just got the error message that non-generic methods must be defined in a static class so I am guessing that I have some errors... lol

Thanks.

Upvotes: 0

David McEwing
David McEwing

Reputation: 3340

Do the same row.FindControl() method you have for the checkbox for the GridView2_ABPubs control. This should give you the gridview that you can then do a find control on.

However, having just spent three days staring and customizing a GridView your last template column with the child grid view doesn't need the and nodes, as these will be added automatically by the GridView control, that maybe making it trickier to find the child control.

I also found that the FindControl didn't look very far down the stack so I created an extension method to recursively hunt out the control:

public static T FindControl<T>(this Control parent, string controlName) where T: Control
{
    T found = parent.FindControl(controlName) as T;
    if (found != null)
       return found;

    foreach(Control childControl in parent.Controls)
    {
        found = childControl.FindControl<T>(controlName) as T;
        if (found != null)
           break;
    }

    return found;
}

Upvotes: 1

Related Questions