解决方案:我最终使用服务器端控件,因为我在Internet上找不到任何解决问题的方法。
我有一个嵌套的GridView,其中gvSelect作为父网格,gvSelectExpand作为子网格。 目标 - 我想要实现的是当我在gvSelect上选中一个复选框时,将使用客户端控件检查该行下的gvSelectExpand中的所有复选框(该行的子网格)。
我有一个javascript会检查标题中的所有可见复选框,但我正在寻找更小的东西。有人可以帮我吗?感谢。
JavaScript - 选中所有复选框
<script language="JavaScript">
function select_deselectAll(chkVal, idVal) {
var frm = document.forms[0];
// Loop through all elements
for (i = 0; i < frm.length; i++) {
// Look for our Header Template's Checkbox
if (idVal.indexOf('chkbxSelectAll') != -1) {
// Check if main checkbox is checked, then select or deselect datagrid checkboxes
if (chkVal == true) {
frm.elements[i].checked = true;
}
else {
frm.elements[i].checked = false;
}
// Work here with the Item Template's multiple checkboxes
}
}
}
</script>
JavaScript - 展开嵌套网格
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("[src*=plus]").live("click", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "Images/minus.png");
});
$("[src*=minus]").live("click", function () {
$(this).attr("src", "Images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
ASPX
<asp:GridView ID="gvSelect" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvSelect_DataBound"
BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ItemNumber">
<Columns>
<asp:BoundField Visible="False" DataField="RecordUID" SortExpression="RecordUID" HeaderText="RecordUID" ReadOnly="True" />
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="Images/plus.png" />
<asp:Panel ID="pnlSelectExpand" runat="server" Style="display: none">
<asp:GridView ID="gvSelectExpand" runat="server" Width="350px" AutoGenerateColumns="false" DataKeyNames="Slab">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelectExpand" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Slab" SortExpression="Slab" ReadOnly="True" HeaderText="Slab" />
<asp:BoundField DataField="Size" SortExpression="Size" ReadOnly="True" HeaderText="Size" />
<asp:BoundField DataField="Sqft" SortExpression="Sqft" ReadOnly="True" HeaderText="Sqft" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Block" SortExpression="Block" ReadOnly="True" HeaderText="Block" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Totalweight" SortExpression="Totalweight" ReadOnly="True" HeaderText="Totalweight" ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select">
<HeaderTemplate>
<asp:CheckBox ID="chkbxSelectAll" runat="server" Font-Names="Verdana" Font-Size="XX-Small" Text="Select All Slabs"
onclick="javascript: return select_deselectAll (this.checked, this.id);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Bundle" SortExpression="Bundle" ReadOnly="True"
HeaderText="Bundle" />
<asp:BoundField DataField="ItemNumber" SortExpression="ItemNumber"
ReadOnly="True" HeaderText="ItemNumber" />
<asp:BoundField DataField="ItemDescription" SortExpression="ItemDescription"
ReadOnly="True" HeaderText="ItemDesc" />
<asp:BoundField DataField="whse" SortExpression="whse" ReadOnly="True"
HeaderText="Warehouse" />
<asp:BoundField DataField="NumSlabs" SortExpression="NumSlabs" ReadOnly="True"
HeaderText="NumSlabs" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="QtyTiedUp" SortExpression="QtyTiedUp" ReadOnly="True"
HeaderText="QtyTiedUp" ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>