Reputation: 2004
I have three checkboxes in a GridView but the user can only select one checkbox. So if they select the first checkbox, I need to alert them them that they cannot select the other two.
<asp:GridView CssClass="tblResults" runat="server" ID="dgDetails"
OnRowDataBound="dgDetails_ItemDataBound"
DataKeyField="ID" AutoGenerateColumns="false"
AlternatingRowStyle-BackColor="#EEEEEE">
<HeaderStyle CssClass="tblResultsHeader" />
<Columns>
<asp:TemplateField HeaderText="Approved1">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkApproved1" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approved2">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkApproved2" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approved3">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkApproved3" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind:
protected void dgDetails_ItemDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = sender as GridView;
Quote.QuoteDetails qd = e.Row.DataItem as Quote.QuoteDetails;
CheckBox chkApproved1 = e.Row.FindControl("chkApproved1") as CheckBox;
CheckBox chkApproved2 = e.Row.FindControl("chkApproved2") as CheckBox;
CheckBox chkApproved3 = e.Row.FindControl("chkApproved3") as CheckBox;
}
}
I tried using something like this in query:
$('#<%= chkApproved1.ClientID %>').change(function () {
if($(this).is(":checked")) {
}
});
But that causes the error:The name 'chkApproved1' does not exist in the current context.
So how can I check if the checkboxes in the GridView have been ticked?
Upvotes: 2
Views: 98
Reputation: 1456
if you want to give alert if first checkbox is checked use the below script
$(document).ready(function () {
$("table[id$='dgDetails']").find("input[id*='chkApproved1']").change(function () {
if ($(this).is(":checked")) {
alert('Hi');
}
});
});
Here is the code that will help you to prevent other checkbox to check if first is checked.
if ($(this).is(":checked")) {
if ($(this).attr("id").indexOf("chkApproved1") != -1) { var1 = 1 }
if ($(this).attr("id").indexOf("chkApproved2") != -1) { var2 = 1 }
if ($(this).attr("id").indexOf("chkApproved3") != -1) { var3 = 1 }
if (var1 == 1 && var2 == 1 && var3 == 1) {
var2 = 0; var3 = 0;
$("table[id$='dgDetails']").find("input[id*='chkApproved2']").attr("checked", false);
$("table[id$='dgDetails']").find("input[id*='chkApproved3']").attr("checked", false);
}
else if (var1 == 1 && var2 == 1) {
if ($(this).attr("id").indexOf("chkApproved1") != -1) { var1 = 0 }
if ($(this).attr("id").indexOf("chkApproved2") != -1) { var2 = 0 }
alert('You can\'t select this checkbox2');
$(this).attr("checked", false);
}
else if (var1 == 1 && var3 == 1) {
if ($(this).attr("id").indexOf("chkApproved1") != -1) { var1 = 0 }
if ($(this).attr("id").indexOf("chkApproved3") != -1) { var3 = 0 }
alert('You can\'t select this checkbox3');
$(this).attr("checked", false);
}
}
else {
if ($(this).attr("id").indexOf("chkApproved1") != -1) { var1 = 0 }
if ($(this).attr("id").indexOf("chkApproved2") != -1) { var2 = 0 }
if ($(this).attr("id").indexOf("chkApproved3") != -1) { var3 = 0 }
}
Hope this help
Upvotes: 3