我有一个Gridview,其中包含第一列作为“复选框”。我需要添加“全选”和“单个行选择”功能,并且需要在c#代码中捕获所选行,但是我无法获得触发该事件的复选框,并单击“提交”按钮。下面是我的网格代码
<asp:DataGrid ID="dgRegions" runat="server" CssClass="datagrid" AutoGenerateColumns="False" Width="100%" CellPadding="2" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" AllowSorting="True" OnItemDataBound="dgRegions_ItemDataBound" onrowdatabound="dgRegions_RowDataBound">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkheader" runat="server" Checked= "false" onclick="javascript:SelectheaderCheckboxes(this)"/>
</ItemTemplate>
<itemtemplate>
<asp:CheckBox ID="chkchild" runat="server"/>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="REQUEST NUMBER">
<ItemStyle VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.REQUEST_DETAIL_ID") %>' ID="REQUEST_DETAIL_ID">
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
我有一个单击按钮,需要获取REQUEST_DETAIL_ID
的值。下面是我的JavaScript代码
<asp:Content ID="cDefaultContent" ContentPlaceHolderID="cpDefaultContentPlaceHolder" runat="server">
<script type="text/javascript">
// Select/Deselect checkboxes based on header checkbox
function SelectheaderCheckboxes(headerchk) {
debugger
var gvcheck = document.getElementById('dgRegions');
var i;
//Condition to check header checkbox selected or not if that is true checked all checkboxes
if (headerchk.checked) {
for (i = 0; i < gvcheck.rows.length; i++) {
var inputs = gvcheck.rows[i].getElementsByTagName('input');
inputs[0].checked = true;
}
}
//if condition fails uncheck all checkboxes in gridview
else {
for (i = 0; i < gvcheck.rows.length; i++) {
var inputs = gvcheck.rows[i].getElementsByTagName('input');
inputs[0].checked = false;
}
}
}
//function to check header checkbox based on child checkboxes condition
function Selectchildcheckboxes(header) {
var ck = header;
var count = 0;
var gvcheck = document.getElementById('dgRegions');
var headerchk = document.getElementById(header);
var rowcount = gvcheck.rows.length;
//By using this for loop we will count how many checkboxes has checked
for (i = 1; i < gvcheck.rows.length; i++) {
var inputs = gvcheck.rows[i].getElementsByTagName('input');
if (inputs[0].checked) {
count++;
}
}
//Condition to check all the checkboxes selected or not
if (count == rowcount - 1) {
headerchk.checked = true;
}
else {
headerchk.checked = false;
}
}
</script>
这是我的C#
protected void dgRegions_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox headerchk = (CheckBox)dgRegions.FindControl("chkheader");
CheckBox childchk = (CheckBox)e.Row.FindControl("chkchild");
childchk.Attributes.Add("onclick", "javascript:Selectchildcheckboxes('" + headerchk.ClientID + "')");
}
}
我在做什么错?请帮助