edit:
What is not working:- i have a two columns (approve all/reject all) how can i restrict the user to allow only one checkbox of each? the below code works if you dont use the gridview….
i have asked this question here (Allow only one (approve/reject) checkbox to be checked ) and its working as expected only if i have asp.net control meaning without using the gridview control and now i’m in a situation where i have to use the gridview control and seems my code does not work… i have maintained the same class names. any help?
here is my .aspx code with gridview:
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#C1All').click(function () { debugger
$('.col1 > input').attr("checked", $('#C1All').attr("checked"));
$('.col2 > input').removeAttr("checked");
$('#C2All').removeAttr("checked");
});
$('#C2All').click(function () { debugger
$('.col2 > input').attr("checked", $('#C2All').attr("checked"));
$('.col1 > input').removeAttr("checked");
$('#C1All').removeAttr("checked");
});
$('.col1').each(function () {
$(this).click(function () { debugger
var id = $("input", this).attr('id');
var coresId = id.replace('C1', 'C2');
$('#' + coresId).removeAttr("checked");
$('#C1All').removeAttr("checked");
$('#C2All').removeAttr("checked");
});
});
$('.col2').each(function () {
$(this).click(function () {debugger
var id = $("input", this).attr('id');
var coresId = id.replace('C2', 'C1');
$('#' + coresId).removeAttr("checked");
$('#C1All').removeAttr("checked");
$('#C2All').removeAttr("checked");
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
OnRowDataBound="gv_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Approve" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="50px" >
<HeaderTemplate>
Approve<br />
<asp:CheckBox ID="C1All" runat="server" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate >
<asp:CheckBox CssClass="col1" ID="chkApprove" runat="server" >
</asp:CheckBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Reject" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="180px">
<HeaderTemplate>
Reject<br />
<asp:CheckBox ID="C2All" runat="server" />
<asp:DropDownList ID="drpPaymentMethod" runat="server">
<asp:ListItem Value="-1">Please select</asp:ListItem>
<asp:ListItem Value="0">Month</asp:ListItem>
<asp:ListItem Value="1">At End</asp:ListItem>
<asp:ListItem Value="2">At Travel</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<div class="selectReason">
<asp:CheckBox CssClass="col2" ID="chkReject" runat="server" >
</asp:CheckBox>
<asp:DropDownList ID="drpPaymentMethod" runat="server">
<asp:ListItem Value="-1">Please select</asp:ListItem>
<asp:ListItem Value="0">Month</asp:ListItem>
<asp:ListItem Value="1">At End</asp:ListItem>
<asp:ListItem Value="2">At Travel</asp:ListItem>
</asp:DropDownList>
</div>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="ID" ControlStyle-Width="250px" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="FirstName" ControlStyle-Width="250px" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" ControlStyle-Width="250px" HeaderText="LastName"
SortExpression="LastName" />
<asp:TemplateField>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
It is all to do with how controls are rendered to the browser. So based on that you will need to modify the jQuery element selectors. In the case with your
GridViewwhat is happening is that instead ofC1AllandC2Allit is nowgv_C1Allandgv_C2All. In the case of checkboxes, instead of replacingC1withC2and vise versa, this time you will need to replace the wordApprovewithRejectand vise versa. Here is the modified jQuery. Now it should work.I would suggest you to have a look at the page HTML in the browser so that you can figure out how controls are rendered (you can press function key F12 in InternetExplorer to view the Developer tools pane, where you can see the HTML tree)