Mastering Dreamweaver MX Databases
|
|
As we've pointed out, ASP.NET includes a set of validation server controls that provide an easy- to-use way to check HTML forms and controls for errors. Plus, you can create more complex validation schemes by adding multiple validation server controls to your form. However, let's limit our sample validation to just the RequiredFieldValidator server control.
You add the RequiredFieldValidator server control to a Web Form just as you add any other server control. Follow these steps:
-
In Dreamweaver MX, open your helloWorld.aspx file from your defined site.
-
Place the cursor next to the Enter button,
-
Click the More Tags button on the ASP.NET toolbar to open the Tag Chooser.
-
Highlight the ASP.NET Tags group, select the Validation server controls control, and choose ASP:RequiredFieldValidator as shown in Figure 14.5.Then click the Insert button.
Figure 14.5: Dreamweaver MX Tag Chooser for ASP.NET
As you can see in the dialog box shown in Figure 14.6, you need to set a number of Validation control parameters.
Let's take a look at the parameters.
ID Sets the ID the web server uses to monitor and control the validation server control.
Text Optional. If specified, sets the message to display if the validation of the targeted server control fails.
Display Offers you three behaviors to manage the display for this validation control:
-
None, the default, sets the validation server control to never display an error message but instead pass the error message to a ValidationSummary control. A ValidationSummary control gathers all error messages from all validation server controls and displays a summary of error messages.
-
Static sets the validation server control to display the validation error message in whatever layout position the validation server control currently occupies.
-
Dynamic sets the validation error message to dynamically allocate space on the page for the error messages. By dynamically allocating space, multiple validation controls can share the same space for error messages.
Initial Value The value initially associated with the watched server control
Control to Validate Targets a current server control to validate.
Error Message Sets the text of the error message to display if the validation fails.
Enable Client Script If checked, enables client-side validation.All right, let's set the validation control for the tbName text box. Follow these steps:
-
In the ID field, enter an ID for the RequiredFieldValidator server control.
-
In the Control To Validate text box, enter tbName.
-
In the Error Message text box, enter You must enter your Name!
-
Click the Enable Client Script check box, and click OK to insert the control into your form.
Listing 14.4 and Listing 14.5 show the generated code results in our VB.NET and C# examples.
Listing 14.4: HELLOWORLDVALIDATE-VB.ASPX
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <script runat="server"> Sub tbName_Change(Sender As Object, E As EventArgs) lblWelcome.Text = "Hello " + tbName.Text End Sub </script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Hello World Form</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <%= "Sample ASP.NET Script Page" %><form runat="server"> <label>Your Name:</label> <asp:textbox OnTextChanged="tbName_Change" runat="server" TextMode="SingleLine" /> <asp:button runat="server" Text="Enter" /> <asp:label runat="server" ></asp:label> <asp:requiredfieldvalidator ControlToValidate="tbName" ErrorMessage="You must enter your Name!" runat="server" /> </form> </body> </html>
Listing 14.5: HELLOWORLDVALIDATE-CSHARP.ASPX
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <script runat="server"> void tbName_Change(Object Sender, EventArgs E) { lblWelcome.Text = "Hello " + tbName.Text; } </script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Hello World Form</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form runat="server"> <label>Your Name:</label> <asp:textbox OnTextChanged="tbName_Change" runat="server" TextMode="SingleLine" /> <asp:button runat="server" Text="Enter" /> <asp:label runat="server" ></asp:label> <asp:requiredfieldvalidator ControlToValidate="tbName" ErrorMessage="You must enter your Name!" runat="server" /> </form> </body> </html>
Let's preview our sample validation script file in a web browser. As you can see in Figure 14.7, the web form appears the same as our original sample page. However, click the Enter button without a value in the text box. As Figure 14.8 show, ASP.NET kindly validates the text box and displays our error message.
|
|