如何在访问子网格视图时获取父网格视图的一个单元格的值

How to grab the value of one of the cells of the parent gridview when accessing the child gridview?

本文关键字:视图 网格 单元格 一个 获取 访问      更新时间:2023-09-26

我正在使用一个常见的JavaScript函数来展开和折叠其父网格视图中的子网格视图。我的问题是我想在父网格视图中获取当前行的一个单元格的值,或者至少是它的行索引。我尝试过的任何方法似乎都不起作用 - 我一直在获取子网格视图当前行的值或行索引。

这是我使用 JavaScript 函数的 aspx:

<script language="javascript" type="text/javascript">
        function divexpandcollapse(divname)
        {                        
            var div = document.getElementById(divname);
            var img = document.getElementById('img' + divname);
            if (div.style.display == "none")
            {
                div.style.display = "inline";
                img.src = "minus.gif";             
            }
            else
            {
                div.style.display = "none";
                img.src = "plus.gif";          
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
          <div>
            <input id="Hidden1" type="hidden" runat="server" value="" />
            <asp:GridView ID="gvOrders" runat="server" DataKeyNames="OrderID" Width="750px" AutoGenerateColumns="False" OnRowDataBound="gvOrders_RowDataBound" GridLines="None"
                BorderStyle="Solid" BorderWidth="3px" BorderColor="Blue" style="z-index: 100; left: 126px; position: absolute; top: 96px" BackColor="White" OnRowCommand="gvOrders_RowCommand"
                OnSelectedIndexChanged="gvOrders_SelectedIndexChanged">
            <RowStyle BackColor="#E1E1E1" />
            <AlternatingRowStyle BackColor="White" />
            <HeaderStyle BackColor="LightBlue" Font-Bold="True" ForeColor="Black" />
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href="JavaScript:divexpandcollapse('div<%# Eval("OrderID") %>');">
                            <img id="imgdiv<%# Eval("OrderID") %>" width="9px" src="plus.gif" alt =""/>
                        </a>
                    </ItemTemplate>
                    <ItemStyle Width="20px" />
                    <ControlStyle BackColor="Black" />
                </asp:TemplateField>                
                <asp:BoundField DataField="OrderID" HeaderText="Order ID" >
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="CustomerID" HeaderText="Customer ID" >
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="Company" HeaderText="Company" >
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="OrderDate" HeaderText="Order Date" >
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="ShippingDate" HeaderText="Shipping Date" >
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>                
                <asp:TemplateField>
                    <ItemTemplate>
                        <tr>
                        <td colspan="100%">
                            <div id="div<%# Eval("OrderID") %>" style="display: none; position: relative; left: 15px; overflow: auto">
                               <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Double"  BorderColor="#df5015" GridLines="None" Width="700px"
                                    OnRowCancelingEdit="gvChildGrid_RowCancelingEdit" OnRowDeleting="gvChildGrid_RowDeleting" OnRowEditing="gvChildGrid_RowEditing" OnRowUpdating="gvChildGrid_RowUpdating" >
                                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                    <RowStyle BackColor="#E1E1E1" />
                                    <AlternatingRowStyle BackColor="White" />
                                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                    <Columns>
                                        <asp:TemplateField HeaderText="CoreID">
                                            <ItemTemplate>
                                                <asp:Label ID="lblCoreID" runat="server" Text='<%# Bind("CoreID") %>'></asp:Label>
                                                <asp:TextBox ID="tbxCoreID" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("CoreID") %>'></asp:TextBox>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="tbxCoreID" runat="server" Text='<%# Bind("CoreID") %>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="BoxID">
                                            <ItemTemplate>
                                                <asp:Label ID="lblBoxID" runat="server" Text='<%# Bind("BoxID") %>'></asp:Label>
                                                <asp:TextBox ID="tbxBoxID" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("BoxID") %>'></asp:TextBox>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="tbxBoxID" runat="server" Text='<%# Bind("BoxID") %>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="CoresPerBox">
                                            <ItemTemplate>
                                                <asp:Label ID="lblCoresPerBox" runat="server" Text='<%# Bind("CoresPerBox") %>'></asp:Label>
                                                <asp:TextBox ID="tbxCoresPerBox" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("CoresPerBox") %>'></asp:TextBox>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="tbxCoresPerBox" runat="server" Text='<%# Bind("CoresPerBox") %>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="PalletID">
                                            <ItemTemplate>
                                                <asp:Label ID="lblPalletID" runat="server" Text='<%# Bind("PalletID") %>'></asp:Label>
                                                <asp:TextBox ID="tbxPalletID" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("PalletID") %>'></asp:TextBox>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="tbxPalletID" runat="server" Text='<%# Bind("PalletID") %>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="BoxesPerPallet">
                                            <ItemTemplate>
                                                <asp:Label ID="lblBoxesPerPallet" runat="server" Text='<%# Bind("BoxesPerPallet") %>'></asp:Label>
                                                <asp:TextBox ID="tbxBoxesPerPallet" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("BoxesPerPallet") %>'></asp:TextBox>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="tbxBoxesPerPallet" runat="server" Text='<%# Bind("BoxesPerPallet") %>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:CommandField ShowEditButton="True" ShowCancelButton="True" CancelText="Cancel" HeaderText="Edit Controls" />
                                        <asp:CommandField ShowDeleteButton="True" />
                                    </Columns>
                                </asp:GridView>
                            </div>
                        </td>
                        </tr>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
                <EditRowStyle BackColor="#8080FF" />
            </asp:GridView>

以及我的部分代码隐藏:

protected void gvChildGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView gv = (GridView)gvOrders.Rows[e.NewEditIndex].FindControl("gvChildGrid");
            GridViewRow parentRow = (GridViewRow)gv.Parent.Parent;
            int indexRow = int.Parse(parentRow.RowIndex.ToString().Trim()); //gvOrders.Rows[e.NewEditIndex].Cells[1].Text.ToString().Trim());
            //int indexRow = parentRow.RowIndex;

我尝试使用隐藏字段,在网格视图模板字段中获取评估变量的值以及其他几种方法。由于展开/折叠是由 JavaScript 处理的,因此我无法弄清楚如何通过"正常"方式(例如 _IndexChanged 或 _RowCommand(访问父网格视图。

为了方便起见,我只是将我尝试使用的代码放在 _RowEditing 方法中,直到我弄清楚如何使其工作。我知道通过这样做,e 与子网格视图有关。注释掉的两个行是不起作用或只是抓取子网格视图的当前行的命令。

承认我对C#(但不是编程(比较陌生,并意识到我可能只是错过了一些东西。但我一辈子都想不通是什么。

也许这可以解决你的问题:使用 RowDataBound 事件,您可以在服务器端访问每一行:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType==DataControlRowType.DataRow)
        {
            ((Label)e.Row.FindControl("myLabelId")).Attributes.Add("data-customAttribute", ((Order)e.Row.DataItem).OrderNo);
        }
    }

您会看到您可以将标签(或任何控件(添加到模板字段(例如 ID 为 myLabelId(,然后您可以将 html 属性添加到该标签或根据您的数据进行任何您想要的更改。在添加到控件的示例数据自定义属性中。您甚至可以添加 onclick 属性以了解该行是否为当前行。您可以从 e.Row.DataItem 访问数据,然后您必须将其转换为您的数据对象(例如 Order(。

使用 Jquery,您可以在客户端访问标签:

// get all rows in table including header
var $rows=$('#<%=gvOrders.ClientID%>').find('tr');
// access the innerHTML of Label control we created before which contains the OrderNo
var OrderNo=$(aRow).find('#myLabelId').html();

请注意,ClientID 返回任何服务器控件的 html ID,您可以通过 document.getElementById 或 $('#id'( 访问它。

我希望这个提示可以帮到你。