Building Applications using ASP.NET and C# / Session 4 / 1 of 15 Session 4
Building Applications using ASP.NET and C# / Session 4 / 2 of 15 Session Objectives Explore the various Validation Controls Explain code behind Implement code behind
Building Applications using ASP.NET and C# / Session 4 / 3 of 15 Validation Controls Restricts blank field Compares two fields Checks for specified range Checks value with expression Checks value by client-side or server-side function Lists validation errors of all controls on the page
Building Applications using ASP.NET and C# / Session 4 / 4 of 15 No value is entered Inline error message The User Id cannot be left Blank! The User Id cannot be left Blank! RequiredFieldValid ator
Building Applications using ASP.NET and C# / Session 4 / 5 of 15 Dynamic Display Validation Error Message
Building Applications using ASP.NET and C# / Session 4 / 6 of 15 <asp:comparevalidator controltovalidate="pwd_con" display="static" errormessage="the confirmation password does not match." controltocompare="pwd" type="String" operator="Equal" runat=server> * <asp:comparevalidator controltovalidate="pwd_con" display="static" errormessage="the confirmation password does not match." controltocompare="pwd" type="String" operator="Equal" runat=server> * <asp:comparevalidator controltovalidate="bid" display="static" errormessage="You cannot enter a bid for lesser than $100.“ valuetocompare=100 type="Integer" operator="GreaterThanEqual" runat="server">* <asp:comparevalidator controltovalidate="bid" display="static" errormessage="You cannot enter a bid for lesser than $100.“ valuetocompare=100 type="Integer" operator="GreaterThanEqual" runat="server">* String, Integer, DateTime, Currency, Double =,, =, Not Equal Comparing with static value, 100 CompareVal idator
Building Applications using ASP.NET and C# / Session 4 / 7 of 15 <asp:rangevalidator controltovalidate="r3" type="Integer" minimumvalue="1" maximumvalue="99" errormessage="Your age must be in the range of 1-99 yrs" display="static" runat="server" > * <asp:rangevalidator controltovalidate="r3" type="Integer" minimumvalue="1" maximumvalue="99" errormessage="Your age must be in the range of 1-99 yrs" display="static" runat="server" > * <asp:rangevalidator controltovalidate="r4" type="Integer" minimumcontrol="r1" maximumcontrol="r2" errormessage="Your age must be in the range of 1-99 yrs" display="static" runat="server" > * <asp:rangevalidator controltovalidate="r4" type="Integer" minimumcontrol="r1" maximumcontrol="r2" errormessage="Your age must be in the range of 1-99 yrs" display="static" runat="server" > * Specify value of the control for the range Specify name of the control for the range RangeValid ator
Building Applications using ASP.NET and C# / Session 4 / 8 of 15 SignMeaning ^ The caret sign ^ specifies that checking starts from here $ The “$” sign specifies that the checking ends here [] Square brackets “[]” checks that the value entered match with any of the characters that are in the square brackets. \w “\w” allows any value to be entered /d{} “/d” specifies that the value entered is a digit and {} specifies the number of occurrences of the specified data type +The + sign indicates that one or more elements to be added to the expression being checked RegularExpressionVali dator - 1
Building Applications using ASP.NET and C# / Session 4 / 9 of 15 <asp:regularexpressionvalidator controltovalidate=" id" display="static" ]+\.(com|net|org|edu|mil)$" runat=server> Not a valid address Validate an id RegularExpressionVali dator - 2
Building Applications using ASP.NET and C# / Session 4 / 10 of 15 <asp:customvalidator runat="server" controltovalidate="grade" clientvalidationfunction="clval" onservervalidate="serval" display="static"> Wrong value <asp:customvalidator runat="server" controltovalidate="grade" clientvalidationfunction="clval" onservervalidate="serval" display="static"> Wrong value Client-side function CustomVali dator
Building Applications using ASP.NET and C# / Session 4 / 11 of 15 ValidationSu mmary
Building Applications using ASP.NET and C# / Session 4 / 12 of 15 Page.IsValid Property void validate_page(Object Src, EventArgs E){ if (Page.IsValid == true) { lbl.Text = "Page is Valid!";} else { lbl.Text = "Page is not Valid!";} }
Building Applications using ASP.NET and C# / Session 4 / 13 of 15 disable client-side validation Uplevel and Downlevel Browsers
Building Applications using ASP.NET and C# / Session 4 / 14 of 15 Provides the functionality Code Behind - 1
Building Applications using ASP.NET and C# / Session 4 / 15 of 15 using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public class codeb: Page { public System.Web.UI.WebControls.Label lb1; public System.Web.UI.WebControls.Button bMe; protected void bMe_Click(Object sender, EventArgs e) { lb1.Text = "Clicked!"; } void Main() { } using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public class codeb: Page { public System.Web.UI.WebControls.Label lb1; public System.Web.UI.WebControls.Button bMe; protected void bMe_Click(Object sender, EventArgs e) { lb1.Text = "Clicked!"; } void Main() { } BIN Code Behind - 2