I need to make sure that a checkbox is checked before a user can submit a form. is there any built-in validator that can handle a checkbox or will I have to do it manually in my code-behind?
The checkbox is to confirm that the user has read the terms and conditions. There is no validator that will handle checkboxes - i've checked. I have decided to use code-behind instead, and check the value of the checkbox in question before submitting the form.
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Me.CheckBox1.Attributes.Add("onClick", "javascript:EnableButton()"
End Sub
Thanks for the advice... I decided just to do a check within the code behind for the onclick event of the button to make sure that the checkbox was checked.
If the unchecked I would display and error message and not allow the user to continue. This seemed to be the easiest solution.
users prefer client side stuff, rather than being told after they've submitted a form that they did something wrong. The disable/enabling the button is an easy, clever way to do it, and is how i handle those situations.
I do admit you have a point, so i tried to implement your suggestion... I cam accross a problem tho as the checkbox doesn't seem to trigger it's checkchanged method once it is clicked by the user.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.