ASP NET Validators ASP NET Validators ASP NET

  • Slides: 37
Download presentation
ASP. NET - Validators

ASP. NET - Validators

ASP. NET - Validators • ASP. NET validation controls validate the user input data

ASP. NET - Validators • ASP. NET validation controls validate the user input data to ensure that useless, unauthenticated, or contradictory data don't get stored. • ASP. NET provides the following validation controls: – Required. Field. Validator – Range. Validator – Compare. Validator – Regular. Expression. Validator – Custom. Validator

Base. Validator Class • The validation control classes are inherited from the Base. Validator

Base. Validator Class • The validation control classes are inherited from the Base. Validator class hence they inherit its properties and methods

Members Description Control. To. Validate Indicates the input control to validate. Display Indicates how

Members Description Control. To. Validate Indicates the input control to validate. Display Indicates how the error message is shown. Enable. Client. Script Indicates whether client side validation will take. Enabled Enables or disables the validator. Error. Message Indicates error string. Text Error text to be shown if validation fails. Is. Valid Indicates whether the value of the control is valid. Set. Focus. On. Error It indicates whether in case of an invalid control, the focus should switch to the related input control. Validation. Group The logical group of multiple validators, where this control belongs. Validate() This method revalidates the control and updates the Is. Valid property.

Required. Field. Validator Control • The Required. Field. Validator control ensures that the required

Required. Field. Validator Control • The Required. Field. Validator control ensures that the required field is not empty. • It is generally tied to a text box to force input into the text box. <asp: Required. Field. Validator ID="rfvcandidate" runat="server" Control. To. Validate ="ddlcandidate" Error. Message="Please choose a candidate" Initial. Value="Please choose a candidate"> </asp: Required. Field. Validator>

Range. Validator Control • The Range. Validator control verifies that the input value falls

Range. Validator Control • The Range. Validator control verifies that the input value falls within a predetermined range. <asp: Range. Validator ID="rvclass“ runat="server“ Control. To. Validate="txtclass" Error. Message="Enter your class (6 - 12)" Maximum. Value="12" Minimum. Value="6“ Type="Integer"> </asp: Range. Validator>

Compare. Validator Control • The Compare. Validator control compares a value in one control

Compare. Validator Control • The Compare. Validator control compares a value in one control with a fixed value or a value in another control. <asp: Compare. Validator ID="Compare. Validator 1“ runat="server" Error. Message="Compare. Validator"> </asp: Compare. Validator>

Regular. Expression. Validator • The Regular. Expression. Validator allows validating the input text by

Regular. Expression. Validator • The Regular. Expression. Validator allows validating the input text by matching against a pattern of a regular expression. • The regular expression is set in the Validation. Expression property.

Character Escapes Description b Matches a backspace. t Matches a tab. r Matches a

Character Escapes Description b Matches a backspace. t Matches a tab. r Matches a carriage return. v Matches a vertical tab. f Matches a form feed. n Matches a new line. Escape character.

Metacharacters Description . Matches any character except n. [abcd] Matches any character in the

Metacharacters Description . Matches any character except n. [abcd] Matches any character in the set. [^abcd] Excludes any character in the set. [2 -7 a-m. A-M] Matches any character specified in the range. w Matches any alphanumeric character and underscore. W Matches any non-word character. s Matches whitespace characters like, space, tab, new line etc. S Matches any non-whitespace character. d Matches any decimal character. D Matches any non-decimal character.

Quantifier Description * Zero or more matches. + One or more matches. ? Zero

Quantifier Description * Zero or more matches. + One or more matches. ? Zero or one matches. {N} N matches. {N, } N or more matches. {N, M} Between N and M matches.

Custom. Validator • The Custom. Validator control allows writing application specific custom validation routines

Custom. Validator • The Custom. Validator control allows writing application specific custom validation routines for both the client side and the server side validation. • The client side validation is accomplished through the Client. Validation. Function property. • The client side validation routine should be written in a scripting language, such as Java. Script or VBScript, which the browser can understand.

 • <asp: Custom. Validator ID="Custom. Validator 1" runat="server" Client. Validation. Function=. cvf_func. Error.

• <asp: Custom. Validator ID="Custom. Validator 1" runat="server" Client. Validation. Function=. cvf_func. Error. Message="Custom. Validator"> </asp: Custom. Validator>

<form id="form 1" runat="server"> <table style="width: 66%; "> <tr> <td class="style 1" colspan="3" align="center">

<form id="form 1" runat="server"> <table style="width: 66%; "> <tr> <td class="style 1" colspan="3" align="center"> <asp: Label ID="lblmsg" Text="President Election Form : Choose your president" runat="server" /> </td> </tr> <td class="style 3"> Candidate: </td> <td class="style 2"> <asp: Drop. Down. List ID="ddlcandidate" runat="server" style="width: 239 px"> <asp: List. Item>Please Choose a Candidate</asp: List. Item> <asp: List. Item>M H Kabir</asp: List. Item> <asp: List. Item>Steve Taylor</asp: List. Item> <asp: List. Item>John Abraham</asp: List. Item> <asp: List. Item>Venus Williams</asp: List. Item> </asp: Drop. Down. List> </td>

<td> <asp: Required. Field. Validator ID="rfvcandidate" runat="server" Control. To. Validate ="ddlcandidate" Error. Message="Please choose

<td> <asp: Required. Field. Validator ID="rfvcandidate" runat="server" Control. To. Validate ="ddlcandidate" Error. Message="Please choose a candidate" Initial. Value="Please choose a candidate"> </asp: Required. Field. Validator> </td> </tr> <td class="style 3"> House: </td> <td class="style 2"> <asp: Radio. Button. List ID="rblhouse" runat="server" Repeat. Layout="Flow"> <asp: List. Item>Red</asp: List. Item> <asp: List. Item>Blue</asp: List. Item> <asp: List. Item>Yellow</asp: List. Item> <asp: List. Item>Green</asp: List. Item> </asp: Radio. Button. List> </td> <asp: Required. Field. Validator ID="rfvhouse" runat="server" Control. To. Validate="rblhouse" Error. Message="Enter your house name" > </asp: Required. Field. Validator> </td> </tr>

<tr> <td class="style 3"> Class: </td> <td class="style 2"> <asp: Text. Box ID="txtclass" runat="server"></asp:

<tr> <td class="style 3"> Class: </td> <td class="style 2"> <asp: Text. Box ID="txtclass" runat="server"></asp: Text. Box> </td> <asp: Range. Validator ID="rvclass" runat="server" Control. To. Validate="txtclass" Error. Message="Enter your class (6 - 12)" Maximum. Value="12" Minimum. Value="6" Type="Integer"> </asp: Range. Validator> </td> </tr>

<tr> <td class="style 3"> Email: </td> <td class="style 2"> <asp: Text. Box ID="txtemail" runat="server"

<tr> <td class="style 3"> Email: </td> <td class="style 2"> <asp: Text. Box ID="txtemail" runat="server" style="width: 250 px"> </asp: Text. Box> </td> <asp: Regular. Expression. Validator ID="remail" runat="server" Control. To. Validate="txtemail" Error. Message="Enter your email" Validation. Expression="w+([-+. ']w+)*@w+([-. ]w+)*. w+([-. ]w+)*"> </asp: Regular. Expression. Validator> </td> </tr> <td class="style 3" align="center" colspan="3"> <asp: Button ID="btnsubmit" runat="server" onclick="btnsubmit_Click" style="text-align: center" Text="Submit" style="width: 140 px" /> </td> </tr> </table> <asp: Validation. Summary ID="Validation. Summary 1" runat="server" Display. Mode ="Bullet. List" Show. Summary ="true" Header. Text="Errors: " /> </form>

protected void btnsubmit_Click(object sender, Event. Args e) { if (Page. Is. Valid) { lblmsg.

protected void btnsubmit_Click(object sender, Event. Args e) { if (Page. Is. Valid) { lblmsg. Text = "Thank You"; } Else { lblmsg. Text = "Fill up all the fields"; }}

ASP. NET Page Life Cycle

ASP. NET Page Life Cycle

ASP. NET Page Life Cycle

ASP. NET Page Life Cycle

Pre. Init • Check the Is. Post. Back property to determine whether this is

Pre. Init • Check the Is. Post. Back property to determine whether this is the first time the page is being processed. • Create or re-create dynamic controls. • Set a master page dynamically. • Set the Theme property dynamically.

Init • This event fires after each control has been initialized. • Each control's

Init • This event fires after each control has been initialized. • Each control's Unique. ID is set and any skin settings have been applied. • Use this event to read or initialize control properties. • The "Init" event is fired first for the bottom-most control in the hierarchy, and then fired up the hierarchy until it is fired for the page itself.

Init. Complete • Until now the viewstate values are not yet loaded, hence you

Init. Complete • Until now the viewstate values are not yet loaded, hence you can use this event to make changes to the view state that you want to ensure are persisted after the next postback. • Raised by the Page object. • Use this event for processing tasks that require all initialization to be complete.

Load. View. State • Raised after the page loads view state for itself and

Load. View. State • Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance. • Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance. • Loads View. State: View. State data are loaded to controls. • Loads Postback data: Postback data are now handed to the page controls.

Load • The Page object calls the On. Load method on the Page object,

Load • The Page object calls the On. Load method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page. • This is the first place in the page lifecycle that all values are restored. • Most code checks the value of Is. Post. Back to avoid unnecessarily resetting state. • You may also call Validate and check the value of Is. Valid in this method. • You can also create dynamic controls in this method. • Use the On. Load event method to set properties in controls and establish database connections.

Control Post. Back Event(s) • ASP. NET now calls any events on the page

Control Post. Back Event(s) • ASP. NET now calls any events on the page or its controls that caused the Post. Back to occur. • Use these events to handle specific control events, such as a Button control's Click event or a Text. Box control's Text. Changed event. • In a postback request, if the page contains validator controls, check the Is. Valid property of the Page and of individual validation controls before performing any processing. • This is just an example of a control event. Here it is the button click event that caused the postback.

Load. Complete • Raised at the end of the event-handling stage. • Use this

Load. Complete • Raised at the end of the event-handling stage. • Use this event for tasks that require that all other controls on the page be loaded.

On. Pre. Render • Raised after the Page object has created all controls that

On. Pre. Render • Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls. • The Page object raises the Pre. Render event on the Page object, and then recursively does the same for each child control. The Pre. Render event of individual controls occurs after the Pre. Render event of the page. • Allows final changes to the page or its control. • This event takes place before saving View. State, so any changes made here are saved. • For example: After this event, you cannot change any property of a button or change any viewstate value. • Each data bound control whose Data. Source. ID property is set calls its Data. Bind method. • Use the event to make final changes to the contents of the page or its controls.

On. Save. State. Complete • Raised after view state and control state have been

On. Save. State. Complete • Raised after view state and control state have been saved for the page and for all controls. • Before this event occurs, View. State has been saved for the page and for all controls. • Any changes to the page or controls at this point will be ignored. • Use this event perform tasks that require the view state to be saved, but that do not make any changes to controls.

Render Method • This is a method of the page object and its controls

Render Method • This is a method of the page object and its controls (and not an event). • The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are necessary to properly display a control at the browser.

Un. Load • This event is used for cleanup code. • At this point,

Un. Load • This event is used for cleanup code. • At this point, all processing has occurred and it is safe to dispose of any remaining objects, including the Page object. • Cleanup can be performed on: – Instances of classes, in other words objects – Closing opened files – Closing database connections. • This event occurs for each control and then for the page. • During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream. • If you attempt to call a method such as the Response. Write method then the page will throw an exception.

XML in ASP. NET

XML in ASP. NET

Reading and Writing XML files in C# and ASP. Net • XML is very

Reading and Writing XML files in C# and ASP. Net • XML is very versatile and most commonly used data format in any applications we develop. • . Netframework has packed with a class called Xml. Text. Writer in System. XML namespace to create xml files dynamically.

Writing XML files in C# using System; using System. Xml; public partial class _Default

Writing XML files in C# using System; using System. Xml; public partial class _Default : System. Web. UI. Page { protected void Page_Load(object sender, Event. Args e) { using (Xml. Writer x. Wr = Xml. Writer. Create(Server. Map. Path("library. xml"))) { x. Write. Start. Document(); x. Write. Start. Element("Library"); x. Write. Start. Element("List"); // ADD FEW ELEMENTS. x. Write. Element. String("Book. Name", "Computer Architecture"); x. Write. Element. String("Category", "Computers"); x. Write. Element. String("Price", "125. 60"); x. Write. End. Element(); // CLOSE LIST. x. Write. End. Element(); // CLOSE LIBRARY. x. Write. End. Document(); // END DOCUMENT. // FLUSH AND CLOSE. x. Wr. Flush(); x. Wr. Close(); // SHOW A MESSAGE IN A DIV. div_xml. Inner. Text = "File created. "; } }

Read XML using Xml. Text. Reader <!DOCTYPE html> <head> <title>Read and Write Data in

Read XML using Xml. Text. Reader <!DOCTYPE html> <head> <title>Read and Write Data in XML Using Asp. Net</title> </head> <body> <form id="form 1" runat="server"> <div id="div_xml" runat="server"></div> <input id="View" value="View XML" type="button" onserverclick="View. XML" runat="server" /> </div> </form> </body> </html>

public void View. XML(object sender, Event. Args args) { if (File. Exists(Server. Map. Path("library.

public void View. XML(object sender, Event. Args args) { if (File. Exists(Server. Map. Path("library. xml"))) { xml. Inner. Text = ""; Xml. Text. Reader xl. Read = new Xml. Text. Reader (Server. Map. Path("library. xml")); while (xl. Read()) { xl. Read. Move. To. Element(); div_xml. Inner. Html = div_xml. Inner. Html + " " + xl. Read. Name + " " + xl. Read. Value; } xl. Read. Close(); xl. Read = null; } }