简体   繁体   English

GridView,访问子GridView复选框

[英]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? 我有一个带有子GridView的父GridView(下面的代码),如何获得子GridView复选框的值? And also, how do I save the state of the child gridview, ie 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>

Do the same row.FindControl() method you have for the checkbox for the GridView2_ABPubs control. 对GridView2_ABPubs控件的复选框执行相同的row.FindControl()方法。 This should give you the gridview that you can then do a find control on. 这应该为您提供gridview,然后您可以对其进行查找控制。

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. 但是,仅花了三天的时间凝视并自定义GridView的最后一个带有子网格视图的模板列就不需要and节点,因为这些将由GridView控件自动添加,这可能会使查找子控件变得更加棘手。

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: 我还发现,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;
}

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. 该类另存为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. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM