Server Controls Validation Controls A validation control or

  • Slides: 32
Download presentation
Server Controls Validation Controls A validation control (or validator) determines whether the data in

Server Controls Validation Controls A validation control (or validator) determines whether the data in another web control is in the proper format, before the data is processed. When the XHTML for our page is created, the validator is converted into Java. Script that performs the validation. Java. Script is a scripting language that enhances the functionality and appearance of web pages and is typically executed on the client. Because some clients disable or do not support scripting, ASP. NET validation controls can function on the client, on the server or both.

Server Controls Validation Controls Rich, declarative validation Validation declared separately from input control Extensible

Server Controls Validation Controls Rich, declarative validation Validation declared separately from input control Extensible validation framework Supports validation on client and server Server-side validation is always done Prevents users from spoofing Web Forms

Server Controls Validation Controls <asp: Required. Field. Validator> Ensures that a value is entered

Server Controls Validation Controls <asp: Required. Field. Validator> Ensures that a value is entered <asp: Range. Validator> Checks if value is within minimum and maximum values <asp: Compare. Validator> Compares value against constant, another control or data type <asp: Regular. Expression. Validator> Tests if value matches a predefined pattern <asp: Custom. Validator> Lets you create custom client- or server-side validation function <asp: Validation. Summary> Displays list of validation errors in one place

Server Controls Validation controls are derived from System. Web. UI. Web. Controls. Base. Validator,

Server Controls Validation controls are derived from System. Web. UI. Web. Controls. Base. Validator, which is derived from the Label control Validation controls contain text which is displayed only if validation fails Text property is displayed at control location Error. Message is displayed in summary

Server Controls Validation controls are associated with their target control using the Control. To.

Server Controls Validation controls are associated with their target control using the Control. To. Validate property <asp: Text. Box id=Text. Box 1 runat=server /> <asp: Required. Field. Validator id="Req 1" Control. To. Validate="Text. Box 1" Text="Required Field" runat=server /> Can create multiple validation controls with the same target control

Server Controls Validation Controls Page. Is. Valid indicates if all validation controls on the

Server Controls Validation Controls Page. Is. Valid indicates if all validation controls on the page succeed void Submit_click(object s, Event. Args e) { if (Page. Is. Valid) { Message. Text = "Page is valid!"; } }

Server Controls Validation Controls Display property controls layout Static: fixed layout, display won’t change

Server Controls Validation Controls Display property controls layout Static: fixed layout, display won’t change if invalid Dynamic: dynamic layout None: no display; can still use Validation. Summary and Page. Is. Valid Type property specifies expected data type: Currency, Date, Double, Integer, String

Server Controls Validation Controls Can force down-level option Only server-side validation <% @ Page

Server Controls Validation Controls Can force down-level option Only server-side validation <% @ Page Language="c#" Client. Target="Down. Level" %>

Server Controls Validation Controls Demo: Validation. Controls 1. aspx Demonstrates each type of validation

Server Controls Validation Controls Demo: Validation. Controls 1. aspx Demonstrates each type of validation control

Server Controls Validation Controls The code-behind file validates the information again in case the

Server Controls Validation Controls The code-behind file validates the information again in case the client has Java. Script disabled. The submission of a form sends its data to the server and causes the current page to be requested again is called a postback. The Is. Post. Back property of class Page determines whether the page is being loaded due to a postback. The current Page’s Validate method validates the information as specified by the validation controls in the Web Form. 10

Server Controls Validation Controls Use the Is. Valid property of class Page to check

Server Controls Validation Controls Use the Is. Valid property of class Page to check whether all the validators succeeded. You should always call method Validate before using property Is. Valid. When data is posted to the web server, the form’s data becomes accessible to the web application through the properties of the various web controls. 11

Server Controls Validation Controls Examining the Client-Side XHTML for a Web Form with Validation

Server Controls Validation Controls Examining the Client-Side XHTML for a Web Form with Validation If a validation control’s Enable. Client. Script property is True, the validator performs client-side validation as the user edits the Web Form. You do not need to be able to create or even understand the Java. Script validation code—the validators are converted to working Java. Script by ASP. NET. The Enable. View. State attribute determines whether a web control’s current state is remembered each time a postback occurs. 12

Server Controls Validation Controls The default value, True, indicates that the control’s state at

Server Controls Validation Controls The default value, True, indicates that the control’s state at the last postback is retained. A hidden input called __VIEWSTATE stores the controls’ data as an encoded string so the server can determine whether it has changed. Performance Tip Setting Enable. View. State to False reduces the amount of data passed to the web server with each request. 13

Validation Controls Exercise

Validation Controls Exercise

Validation Controls Exercise

Validation Controls Exercise

Validation Controls Exercise

Validation Controls Exercise

Validation Controls Exercise

Validation Controls Exercise

Master Pages Creating a Master Page The master page defines the elements we want

Master Pages Creating a Master Page The master page defines the elements we want to appear on each page. A master page is like a base class in a visual inheritance hierarchy. The master page contains placeholders for custom content created in each content page. To create a master page, right click the location of the website in the Solution Explorer and select Add New Item…. Select Master Page and specify Bug 2 Bug. master as the file name. Master pages have the file-name extension. master and, like Web Forms, can optionally use a code-behind file to define additional functionality. Leave the box labeled Place code in a separate file unchecked and click Add to create the page.

Master Pages The markup for a master page is almost identical to that of

Master Pages The markup for a master page is almost identical to that of a Web Form. A master page contains a Master directive, which specifies that this file defines a master page using the indicated Language for any code. Code that would usually be placed in a code-behind file can be placed in a script element. Next, set the title of the page to Bug 2 Bug. The master page contains two Content. Place. Holder controls for content that will be defined by a content page.

Master Pages At this point, you can edit the master page in Design mode

Master Pages At this point, you can edit the master page in Design mode as if it were an ASPX file. • The Content. Place. Holder control appears as a rectangle with a purple outline indicating the control’s type and ID. • Using the Properties window, change the ID of this control to body. Content.

Master Pages Place the cursor to the left of Content. Place. Holder and select

Master Pages Place the cursor to the left of Content. Place. Holder and select Table > Insert Table. In the Insert Table dialog, set Rows to 2 and Columns to 1. In the Layout section, specify a Cell padding of 0 and a Cell spacing of 0. Set both the width and height of the table to 100 percent. Make sure that the Size value in the Borders section is 0. Click OK to create a table that fills the page and contains two rows. Change the valign property of the bottom table cell to top and drag the Content. Place. Holder into this cell. Set the Height of the top table cell to 130. Add an Image control named header. Image with its Image. Url property set to the bug 2 bug. png file.

Master Pages Creating a Content Page Right click the master page in the Solution

Master Pages Creating a Content Page Right click the master page in the Solution Explorer and select Add Content Page. Rename the Default. aspx to Content. Page. aspx, then open it in Source mode The Page directive indicates the Master. Page. File that is used as a starting point for this new page’s design. The Title property specifies the title that will be displayed in the web browser’s title bar when the content page is loaded. This value, which we set to Create a New User, replaces the value (i. e. , Bug 2 Bug) set in the title element of the master page. Because Create. New. User. aspx specifies Bug 2 Bug. master as the page’s Master. Page. File, it implicitly contains the contents of the master page.

Master Pages Adding a Create. User. Wizard Control to a Content Page Create. New.

Master Pages Adding a Create. User. Wizard Control to a Content Page Create. New. User. aspx is the page in our website that allows first-time visitors to create user accounts. To provide this functionality, we use a Create. User. Wizard control. Place the cursor inside the Content control in Design mode and double click Create. User. Wizard in the Toolbox to add it to the page. Open the Create. User. Wizard Tasks smart-tag menu and click Auto Format. Select the Professional color scheme. When the user clicks the Create User button, ASP. NET verifies that all the form’s requirements were fulfilled and attempts to create the user account. (we will use this next week) In Create. New. User. aspx, the Page directive indicates that this content page inherits content from Bug 2 Bug. master.

Creating Controls ASP. NET provides two ways to create your own server -side controls

Creating Controls ASP. NET provides two ways to create your own server -side controls User Controls: Essentially a mini. aspx file Custom Controls: You derive a class from System. Web. UI. Control

Creating Controls User Controls User controls simplify the reuse of code and UI components

Creating Controls User Controls User controls simplify the reuse of code and UI components within a Web application A user control is a user-defined Web server control with an. ascx extension Contains HTML, but not the <HTML>, <BODY>, or <FORM> tags Enables full encapsulation Supports nested controls Separate code namespace Separate code language Can partition work across multiple developers Great way to reuse work across multiple pages and applications

Why User Controls? Reuse user interface and code Control 1. as cx Application A

Why User Controls? Reuse user interface and code Control 1. as cx Application A Page 1. as px Page 2. as px Application B Page 3. as px

Adding a User Control Registers user control for use on a page Use the

Adding a User Control Registers user control for use on a page Use the @ Register directive to include a user control in an ASP. NET Page <%@ Register Tag. Prefix="demo" Tag. Name="valid. Num" Src="numberbox. ascx" %> Insert the user control in a Web Form <demo: valid. Num id="num 1" runat="server"/> Use Get and Set properties of the user control num 1. p. Num = 5; //uses Set x = num 1. p. Num; //uses Get

Example User Control Create Before. User. Control. aspx Add 2 Textbox controls with Required.

Example User Control Create Before. User. Control. aspx Add 2 Textbox controls with Required. Field. Validator and Range. Validator Add a Button that adds the values in these textboxes and displays the sum in a Label Create a Web User Control numberbox. ascx Add a Textbox controlswith Required. Field. Validator and Range. Validator Create After. User. Control. aspx Register the user control numberbox. ascx Add 2 numberbox controls Add a Button that adds the p. Num properties in these numberboxes and displays the sum in a Label

Creating Controls Programmatic Use of User Controls Page. Load. Control(string source) Dynamically instantiates a

Creating Controls Programmatic Use of User Controls Page. Load. Control(string source) Dynamically instantiates a user control Create an instance: Control numbox 1 = Page. Load. Control("numberbox. ascx"); Insert into the control hierarchy: my. Panel. Controls. Add(foo);

Creating Controls Custom Controls A class that you create Derived from System. Web. UI.

Creating Controls Custom Controls A class that you create Derived from System. Web. UI. Control using System; using System. Web. UI; public class My. Control : Control { protected override void Render(HTMLText. Writer w) { w. Write(“<h 1>Control output</h 1>”); } }

Creating Controls Custom Controls Must implement Render() method Can expose properties, methods and events

Creating Controls Custom Controls Must implement Render() method Can expose properties, methods and events Should maintain state Should handle postback data Can generate client-side script to do postback Should handle child controls Render them Handle events Can expose and implement templates Can handle data binding

Creating Controls Custom Controls vs. User Controls Custom Controls Good for application-specific UI Good

Creating Controls Custom Controls vs. User Controls Custom Controls Good for application-specific UI Good for reuse, encapsulate common UI Easy to build Can be more complex to build Less flexibility, performance, designer support Total flexibility, better performance, and designer support No template support Can support templates