I have a gridview with a bunch of checkboxes. I would like to restrict the selection based on a value I get back from the database. I am able to do this on the serverside but it is very cluncky. Is there a way I could do that using javascript and ajax
or just a more elegant way to approach this. The gridview is in a usercontrol
Protected Sub selectionCount(ByVal sender As Object, ByVal e As EventArgs)
Dim cnt As Int16 = txtCnt.text
Dim chkbx As CheckBox = CType(sender, CheckBox)
If chkbx.Checked = False Then
Me.txtCnt.text = --cnt
Return
End If
If cnt = ModcntSelection Then 'I get this from the database
Dim lbl As Label = CType(CType(chkbx.Parent.Parent, GridViewRow).FindControl("lblErrorMsg"), Label)
lbl.Text = "More then you are allowed to select"
chkbx.Checked = False
Else
cnt = cnt + 1
txtCnt.text = cnt
End If
End Sub
<script type="text/javascript">
function ToggleCheckBox(checkBox, ErrorMessageLabelID) {
var Count = 12;
//Get this count value from Database
//two options here,
// Get from page Methods or web service
if (checkBox.checked) {
if (document.getElementById('<%= txtCount.ClientID %>').value == Count) {
document.getElementById(ErrorMessageLabelID).value = 'you cannot select any more';
checkBox.checked = false;
}
else {
document.getElementById(ErrorMessageLabelID).value = '';
}
}
}
</script>
In the gridview row databound event, add this event handler for the checkbox
kal30
Member
46 Points
170 Posts
checkboxes in a gridview
Jul 14, 2010 08:38 PM|LINK
Hello all
I have a gridview with a bunch of checkboxes. I would like to restrict the selection based on a value I get back from the database. I am able to do this on the serverside but it is very cluncky. Is there a way I could do that using javascript and ajax or just a more elegant way to approach this. The gridview is in a usercontrol
My code right now -
<asp:GridView ID="gvOption" runat="server" AutoGenerateColumns="false" ShowHeader ="false" > <FooterStyle Font-Names="Arial" ForeColor="#000066" /> <RowStyle ForeColor="#000066" /> <PagerStyle ForeColor="#000066" HorizontalAlign="Left" /> <SelectedRowStyle Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#006699" Font-Bold="True" Font-Names="Arial" ForeColor="White" /> <Columns> <asp:TemplateField> <ItemTemplate> <table cellspacing="0" width="100%" cellpadding="0"> <tr> <td width="100%" align="left"> <asp:Label ID="lblErrorMsg" runat="server" /> <asp:CheckBox ID="cbOption" runat="server" Text= '<%# eval("descrip") %>' AutoPostBack ="true" OnCheckedChanged ="selectionCount"/> </td> </tr> <tr> <td> <asp:UpdatePanel ID="updpnlpnlInfo" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true"> <ContentTemplate> <asp:Panel ID="info" runat="server" visible="true"> Some stuff here </asp:Panel> </ContentTemplate> </asp:UpdatePanel> </td> </tr> </table> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:TextBox ID="txtCnt" runat ="server" />
<div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Protected Sub selectionCount(ByVal sender As Object, ByVal e As EventArgs)</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Dim cnt As Int16 = txtCnt.text</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Dim chkbx As CheckBox = CType(sender, CheckBox)</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> If chkbx.Checked = False Then</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Me.txtCnt.text = --cnt</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Return</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> End If</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> If cnt = 2 Then</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Dim lbl As Label = CType(CType(chkbx.Parent.Parent, GridViewRow).FindControl("lblErrorMsg"), Label)</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> lbl.Text = "More then you are allowed to select"</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> chkbx.Checked = False</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Else</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> cnt = cnt + 1</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> txtCnt.text = cnt</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> End If</div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 448px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> End Sub</div>My selectioncount code is as follows -
Protected Sub selectionCount(ByVal sender As Object, ByVal e As EventArgs) Dim cnt As Int16 = txtCnt.text Dim chkbx As CheckBox = CType(sender, CheckBox) If chkbx.Checked = False Then Me.txtCnt.text = --cnt Return End If If cnt = ModcntSelection Then 'I get this from the database Dim lbl As Label = CType(CType(chkbx.Parent.Parent, GridViewRow).FindControl("lblErrorMsg"), Label) lbl.Text = "More then you are allowed to select" chkbx.Checked = False Else cnt = cnt + 1 txtCnt.text = cnt End If End SubThanks
ajay_gautamj...
Participant
1181 Points
281 Posts
Re: checkboxes in a gridview
Jul 14, 2010 09:52 PM|LINK
Try something like this, this is c# code
/// <summary> /// Enables or Disables the Remove and Add checkboxes in the grids /// </summary> /// <param name="isEnabled">Enable or Disable the checkboxes</param> private void EnabledDisabledCheckbox(bool isEnabled) { // Enable or Diable the GvAssignedRoles checkboxes based on isEnabled for (int iRow = 0; iRow < GvAssignedRoles.Rows.Count; iRow++) { CheckBox chkRemove = (CheckBox)GvAssignedRoles.Rows[iRow].FindControl("chkRemove"); if (chkRemove != null) { chkRemove.Enabled = isEnabled; } } // Enable or Diable the gvAvailableRoles checkboxes based on isEnabled for (int iRow = 0; iRow < gvAvailableRoles.Rows.Count; iRow++) { CheckBox chkAdd = (CheckBox)gvAvailableRoles.Rows[iRow].FindControl("chkAdd"); if (chkAdd != null) { chkAdd.Enabled = isEnabled; } } }hope it helps...Ajay
Ahmed Moosa
Star
7784 Points
1232 Posts
Re: checkboxes in a gridview
Jul 14, 2010 09:57 PM|LINK
Hi
May this help
MCC - MCPD -MCTS
sansan
All-Star
53942 Points
8147 Posts
Re: checkboxes in a gridview
Jul 14, 2010 10:08 PM|LINK
you can create a javascript function to do that
<script type="text/javascript"> function ToggleCheckBox(checkBox, ErrorMessageLabelID) { var Count = 12; //Get this count value from Database //two options here, // Get from page Methods or web service if (checkBox.checked) { if (document.getElementById('<%= txtCount.ClientID %>').value == Count) { document.getElementById(ErrorMessageLabelID).value = 'you cannot select any more'; checkBox.checked = false; } else { document.getElementById(ErrorMessageLabelID).value = ''; } } } </script>In the gridview row databound event, add this event handler for the checkbox
DirectCast(e.Row.FindControl("cbOption"),Checkbox).Attributes.Add("ToggleCheckBox(this,'" + DirectCast(e.Row.FindControl("lblErrorMessage"),Label).ClientID +"');")ajay_gautamj...
Participant
1181 Points
281 Posts
Re: checkboxes in a gridview
Jul 20, 2010 12:08 PM|LINK
Hi,
Is your problem solved?
Ajay