GridView,访问子GridView复选框

时间:2009-07-22 19:34:47

标签: c# .net gridview

我有一个父GridView,它有一个子GridView(下面的代码),如何获取子gridview复选框的值?而且,如何保存子gridview的状态,即它是否显示?这是按下按钮时触发的函数,该按钮读取父网格,查看已选择哪些出版物:

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>

2 个答案:

答案 0 :(得分:1)

对GridView2_ABPubs控件的复选框执行相同的row.FindControl()方法。这应该为您提供gridview,然后您可以对其进行查找控制。

然而,刚刚花了三天时间凝视和自定义GridView你的最后一个带有子网格视图的模板列不需要和节点,因为这些将由GridView控件自动添加,这可能使查找更加棘手儿童控制。

我还发现FindControl看起来不是很远,所以我创建了一个扩展方法来递归搜索控件:

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

答案 1 :(得分:0)

大卫,这是对的:

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

}

该类保存为FindControl.cs。我该如何调用该函数?

我感觉有点迟钝,但在我的辩护中,这是我第一次使用延伸课程。

我刚收到错误消息,必须在静态类中定义非泛型方法,所以我猜我有一些错误... lol

感谢。

相关问题