Asp.net, vb.net, C#.net Resources

Welcome to Asp.net, vb.net, C#.net Resources Sign in | Join | Help
in Search

How to validate a CheckBox?

Last post 10-10-2006, 3:18 PM by singlebrook. 3 replies.
Sort Posts: Previous Next
  •  08-21-2006, 5:34 AM 332

    How to validate a CheckBox?

    How to validate a CheckBox?
  •  08-22-2006, 5:43 AM 680 in reply to 332

    Re: How to validate a CheckBox?

    <asp:CheckBox runat="server" id="chkbox"></asp:CheckBox>
    <asp:CustomValidator runat="server" ErrorMessage="Error!" OnServerValidate="ServerValidation" ClientValidationFunction="ClientValidation"
              ID="Customvalidator1" />
    <asp:Button runat="server" text="submit" ID="Button1" />


     


    <script language="javascript">
    function ClientValidation(source, args)
    {
         args.IsValid = document.all["chkbox"].checked;
    }
    </script>


    VB.NET


    <script runat="server" language="vb">
    sub ServerValidation( source as object, args as ServerValidateEventArgs )
         args.IsValid = chkbox.Checked
    end sub
    </script>


    C#


    <script runat="server" language="cs">
    void ServerValidation(object source, ServerValidateEventArgs args)
    {
         args.IsValid = chkbox.Checked;
    }
    </script>
  •  09-15-2006, 9:22 PM 1624 in reply to 680

    Re: How to validate a CheckBox?

    I tried to implement this code and get an error.

    Exception Details: System.Web.HttpException: Control 'chkTerms' referenced by the ControlToValidate property of 'CVTerms' cannot be validated.

     aspx:

    <asp:CheckBox ID="chkTerms" runat="server" Text="I have read and agree with your <a href=&quot;#&quot; onClick=&quot;popup('terms.asp','500','400');&quot;>Terms &amp; Conditions</a>." />

    <asp:CustomValidator ID="CVTerms" runat=server ControlToValidate="chkTerms" ErrorMessage="You must accept our Terms and Conditions." ValidationGroup="CreateUserWizard1" OnServerValidate="ValidateChkTerms" ClientValidationFunction="ValidateChkTerms"  />

     

    .vb

    Protected Sub ValidateChkTerms(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
            Dim terms As CheckBox = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("chkTerms")
            e.IsValid = terms.Checked
    End Sub

         Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim terms As CheckBox = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("chkTerms")

            ClientScript.RegisterClientScriptBlock(Me.GetType(), "ValidateChkTerms", _
             "function ValidateChkTerms(oSrc, args){args.IsValid = document.getElementById('" & terms.ClientID & "').checked;}", True)
        End Sub

    Any idea whats wrong?  The Client=side validation works fine.
     

  •  10-10-2006, 3:18 PM 1637 in reply to 1624

    Re: How to validate a CheckBox?

    In your <asp:CustomValidator> tag you must remove the ControlToValidate attribute
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems