Page 82 - DCAP312_WEB_TECHNOLOGIES_II
P. 82

Web Technologies-II



                   Notes
                                                 Performing a custom client-side validation using the Custom Validator server
                                                control
                                                Visual C# .NET
                                                <%@ Page Language= “C#” %>
                                                <script runat= “server”>
                                                void Button1_Click(Object sender, EventArgs e) {
                                                if (Page.IsValid) {
                                                Label1.Text = “VALID ENTRY!”;
                                                }
                                                }
                                                void ValidateNumber(object source, ServerValidateEventArgs
                                                args)
                                                {
                                                try
                                                {
                                                int num = int.Parse(args.Value);
                                                args.IsValid = ((num%5) == 0);
                                                }
                                                catch(Exception ex)
                                                {
                                                args.IsValid = false;
                                                }
                                                }
                                                </script>
                                                <html>
                                                <head>
                                                </head>
                                                <body>
                                                <form runat= “server”>
                                                <p>
                                                Number:
                                                <asp:TextBox id= “TextBox1”
                                                runat= “server “></asp:TextBox>
                                                 
                                                <asp:CustomValidator id= “CustomValidator1”
                                                runat= “server” ControlToValidate= “TextBox1”
                                                ErrorMessage= “Number must be even”
                                                OnServerValidate= “ValidateNumber”></asp:CustomValidator>
                                                </p>
                                                <p>
                                                <asp:Button id= “Button1” onclick= “Button1_Click”
                                                runat= “server” Text= “Button”></asp:Button>
                                                </p>


        76                                LOVELY PROFESSIONAL UNIVERSITY
   77   78   79   80   81   82   83   84   85   86   87