session 4

15
Building Applications using ASP.NET and C# / Ses sion 4 / Session 4 Session 4

Upload: hyacinth-battle

Post on 31-Dec-2015

23 views

Category:

Documents


0 download

DESCRIPTION

Session 4. Advanced. Validation Techniques. Session Objectives. Explore the various Validation Controls. Explain code behind. Implement code behind. Validation Controls. Restricts blank field. RequiredFieldValidator. CompareValidator. Compares two fields. Checks for specified range. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Session 4

Building Applications using ASP.NET and C# / Session 4 / 1 of 15

Session 4Session 4

Page 2: Session 4

Building Applications using ASP.NET and C# / Session 4 / 2 of 15

Session ObjectivesSession Objectives

Explore the various Validation Controls

Explain code behind

Implement code behind

Page 3: Session 4

Building Applications using ASP.NET and C# / Session 4 / 3 of 15

Validation ControlsValidation ControlsRestricts blank field

Compares two fields

Checks for specified range

Checks value with expression

Checks value by client-side or server-side functionLists validation errors of all controls on the page

Page 4: Session 4

Building Applications using ASP.NET and C# / Session 4 / 4 of 15

No value

is entere

d

No value

is entere

d

Inline error

message

Inline error

message

<asp:requiredfieldvalidator controltovalidate="userid" display="static" errormessage="You must enter your user id." runat=server>The User Id cannot be left Blank!</asp:requiredfieldvalidator>

<asp:requiredfieldvalidator controltovalidate="userid" display="static" errormessage="You must enter your user id." runat=server>The User Id cannot be left Blank!</asp:requiredfieldvalidator>

RequiredFieldVRequiredFieldValidatoralidator

Page 5: Session 4

Building Applications using ASP.NET and C# / Session 4 / 5 of 15

Dynamic Display

Validation Validation Error MessageError Message

Page 6: Session 4

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>

<asp:comparevalidator controltovalidate="pwd_con" display="static" errormessage="the confirmation password does not match." controltocompare="pwd" type="String" operator="Equal" runat=server> *</asp:comparevalidator>

<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>

<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>

String, Integer, DateTime, Currency,

Double

String, Integer, DateTime, Currency,

Double

= , < , > , <= , >= , Not Equal

Comparing with static value, 100

CompareCompareValidatorValidator

Page 7: Session 4

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>

<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>

<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>

<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>

Specify value of the control for the

range

Specify name of the control for the range

RangeValRangeValidatoridator

Page 8: Session 4

Building Applications using ASP.NET and C# / Session 4 / 8 of 15

Sign Meaning

^ 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

RegularExpressionRegularExpressionValidator - 1Validator - 1

Page 9: Session 4

Building Applications using ASP.NET and C# / Session 4 / 9 of 15

<asp:regularexpressionvalidator controltovalidate="emailid" display="static" validationexpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$" runat=server>Not a valid e-mail address</asp:RegularExpressionValidator>

Validate an email id

RegularExpressionRegularExpressionValidator - 2Validator - 2

Page 10: Session 4

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>

<asp:customvalidator runat="server" controltovalidate="grade" clientvalidationfunction="clval"onservervalidate="serval" display="static">Wrong value</asp:customvalidator>

Client-side function

CustomVCustomValidator alidator

Page 11: Session 4

Building Applications using ASP.NET and C# / Session 4 / 11 of 15

<asp:validationsummary id="vs1" headertext="The errors found are: " displaymode="singleparagraph" runat="server"/> </asp:customvalidator>

<asp:validationsummary id="vs1" headertext="The errors found are: " displaymode="singleparagraph" runat="server"/> </asp:customvalidator>

ValidationValidationSummarySummary

Page 12: Session 4

Building Applications using ASP.NET and C# / Session 4 / 12 of 15

Page.IsValid Page.IsValid PropertyProperty<script language="C#" runat="server" >

void validate_page(Object Src, EventArgs E){ if (Page.IsValid == true) { lbl.Text = "Page is Valid!";} else { lbl.Text = "Page is not Valid!"; } } </script>

Page 13: Session 4

Building Applications using ASP.NET and C# / Session 4 / 13 of 15

<%@ Page ClientTarget=DownLevel %> <%@ Page ClientTarget=DownLevel %>

disable client-side validation

Uplevel and Uplevel and Downlevel Downlevel BrowsersBrowsers

Page 14: Session 4

Building Applications using ASP.NET and C# / Session 4 / 14 of 15

<%@ Page language="C#" Inherits="codeb" %> <html><script language="C#" runat ="server" ></script><form runat="server"><asp:button OnClick="bMe_Click" text="Click me!" id="bMe" runat="server"/><br><br><br><br><asp:label id="lb1" runat="server"/></form></html>

<%@ Page language="C#" Inherits="codeb" %> <html><script language="C#" runat ="server" ></script><form runat="server"><asp:button OnClick="bMe_Click" text="Click me!" id="bMe" runat="server"/><br><br><br><br><asp:label id="lb1" runat="server"/></form></html>

Provides the

functionality

Code Code Behind - Behind - 11

Page 15: Session 4

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 Code Behind - 2Behind - 2