我正在使用常见的 JavaScript 函数来展开和折叠其父 gridview 中的子 gridview。我的问题是我想获取父 GridView 中当前行的单元格之一的值,或者至少是其行索引。我尝试过的任何方法似乎都不起作用 - 我不断获取子 GridView 当前行的值或行索引。
这是我的带有 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;
我尝试过使用隐藏字段、获取 gridview 模板字段中评估变量的值以及其他几种方法。由于展开/折叠是由 JavaScript 处理的,我无法弄清楚如何通过“正常”方式访问父 gridview,例如 _IndexChanged 或 _RowCommand。
为了方便起见,我只是将尝试在 _RowEditing 方法中使用的代码粘贴到其中,直到我弄清楚如何使其工作。我知道通过这样做, e 属于子 gridview 。这两行注释掉的命令要么不起作用,要么只是抓取了子 GridView 的当前行。
我承认我对 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);
}
}
您会看到您可以将标签(或任何控件)添加到 TemplatedField(例如 ID 为 myLabelId),然后您可以向该标签添加 html 属性或根据您的数据进行任何您想要的更改。在示例数据中-将customAttribute添加到控件中。您甚至可以添加onclick属性来了解该行是否是当前行。您可以从 e.Row.DataItem 访问数据,然后必须将其转换为数据对象(例如订单)。
使用 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') 访问它。
希望这些提示可以帮助您。
关于javascript - 访问子gridview时如何获取父gridview某一单元格的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35091896/