5. Create a message page----data validation control and add data
Related introduction:
Data correctness is required for the website. In order to more easily verify whether the data entered by users is correct, ASP.NET provides programmers with data validation controls. The data validation controls provided by ASP.NET include. (Note: In order to display the code, all the following codes have spaces after "<" and before ">". We apologize for the inconvenience!):
Control | function |
RequiredFieldValidator | checks whether there is data input in an input field. |
RangeValidator | checks whether the data entered in an input field is within a specific range. |
CompareValidator | Test whether an input field is equal to, not equal to, greater than, not less than, or less than |
ValidationSummary | lists all controls that failed the test |
RegularExpressionValidator | Check whether a column conforms to the regular expression rules |
CustomValidator | custom validation rules |
STEP 1 Design page <BR>We first use a table to design a rough framework. Since the COMMENTS table in the database contains
![](https://images.downcodes.com/u/info_img/2009-05/31/08201image002.jpg)
[Figure 5-1 Database framework]
COMMENT_ID (automatic number), FIRST_NAME (text), LAST_NAME (text), TELEPHONE (text), EMAIL (text), SUBMIT_DATE (date/time), COMMENTS (notes), ANSWERED (Boolean value) and other fields. According to the needs, we designed the page as shown below in the site, named reg.aspx.
![](https://images.downcodes.com/u/info_img/2009-05/31/08202image004.jpg)
[Figure 5-2 initial page]
Among them, COMMENT_ID can be automatically added by ACCESS, and ANSWERED does not need to be added by the user. The default value of SUBMIT_DATE is set to Now(), and ACCESS will automatically write the date of the added record.
In the previous ASP page, we can pass
to set up input boxes to enter data. ASP.NET has a textbox control to accept data. The syntax is as follows:
< asp:textbox id=”…” runat=”server” other attributes/ >
In DreamweaverMX you can click
Shortcut button to add textbox control. In the pop-up dialog box, set the ID, text mode (dialog box type), tool tip (the prompt when the mouse is hovering over the control), Layout (style design), and Style Information (used to set the input text format and textbox border). format) to set the control. The picture below is the settings dialog box of textbox.
![](https://images.downcodes.com/u/info_img/2009-05/31/08204image008.jpg)
[Figure 5-3 textbox settings dialog box 1]
![](https://images.downcodes.com/u/info_img/2009-05/31/08205image010.jpg)
[Figure 5-4 textbox settings dialog box 2]
![](https://images.downcodes.com/u/info_img/2009-05/31/08206image012.jpg)
[Figure 5-5 textbox settings dialog box 3]
Since the data item COMMENT requires a large amount of text to be entered, a textbox with multiple input lines needs to be used. Just set the text mode to MultiLine and enter the desired number of rows (Rows).
In order to generate the added event, a button is also needed. Click the Forms tab and click the "button" button to add.
Use of STEP2 data validation
Since First_name, Last_name, telephone, and email must be entered, in order to prevent the user from forgetting to enter, you can add the RequiredFieldValidator control to verify the control. Since the control will display content that fails the verification at its location in the event of validation errors, the data validation control should be placed at the appropriate location.
The control syntax for RequiredFieldValidator data validation is as follows:
< asp:RequiredFieldValidato other attributes runat="server" >Error message</ asp:RequiredFieldValidator >
or
< asp:RequiredFieldValidator ErrorMessage="Error message" Other attributes runat="server"/ >
After clicking More Tags, select Validation Server of ASP.NET Tags and you can see that there are different types of data validation controls to choose from. We need to add the validation control asp:RequiredFieldValidator behind the First_name input box.
In the RequiredfieldValidator control settings window, Control to Validate is the ID of the input control associated with this data validation control, and Error Message is the message displayed when data validation fails. At the same time, we can select the required style by setting layout and style Information.
![](https://images.downcodes.com/u/info_img/2009-05/31/08207image014.jpg)
[Figure 5-6 Validation control selection]
![](https://images.downcodes.com/u/info_img/2009-05/31/08208image016.jpg)
[Figure 5-7 RequiredfieldValidator control settings dialog box]
At the same time, add another RequiredFieldValidato data validation control using the same method behind the Last_Name input box. Now we can save and preview the page. When no data is entered in the First_name and Last_name input boxes and the Submit button is clicked, a verification failure message will appear. As shown in Figure 5-8.
But many times we need to verify valid data. For example, the postal code must be 6 digits, and the entered email address needs to be in a specific form. Here you need to use RegularExpressionValidator to verify the validity of the data. In this registration page, both telephone and email can use this control for verification.
![](https://images.downcodes.com/u/info_img/2009-05/31/08209image018.jpg)
[Figure 5-8 RequiredfieldValidator dialog box]
![](https://images.downcodes.com/u/info_img/2009-05/31/082010image020.jpg)
[Figure 5-9 Email setting properties]
Similar to the method of adding the RequiredfieldValidator validation control, click "More tags.." to choose to add the data validation control. The difference is setting the Validation Expression. Since the phone needs to enter 7-10 digits, it needs to be set as follows:
[0-9]{7,10}
Its syntax is as follows:
[]: Used to define acceptable characters. For example, az means that lowercase 'a'-'z' are acceptable characters, a-zA-Z means that all letters are acceptable, and 0-9 means that all letters are acceptable. number.
{}: used to define the number of characters that must be entered, {7,10} means that 7-10 characters can be entered, {0,} means that 0-unlimited characters can be accepted.
'.': Indicates entering any character. .{0,} means that 0-infinitely any number of characters are acceptable.
|: Indicates OR (or), for example, [A-Za-z]{3}|[0-9]{3} means that 3 English letters or 3 numbers can be accepted
(): To facilitate reading, strings containing the | symbol are usually enclosed in (). For example ([A-Za-z]{3}|[0-9]{3}).
: If it contains special symbols such as [], {}, (), |, etc., . must be added before these strings.
The following are some of the more commonly used examples:
Email: .{1,}@.{1,}/..{1}
Telephone (including area code): ([0-9]{3,4}))[0-9]{7,8}
Although there is no guarantee that the user input is the actual data, the validation control can ensure that the format is correct.
![](https://images.downcodes.com/u/info_img/2009-05/31/082011image022.jpg)
[Figure 5-10 Preview page]
As for the CompareValidator control, its properties are set as follows:
Control to Compare | sets the control it is compared to |
Control to Validate | sets the control it is associated with |
Value to Compare | sets the compared value |
Operator | sets the comparison relationship ( equal to, not equal to, greater than, greater than or equal to, less than, less than or equal to ) |
Type | Comparison data type |
Error Message | displays information |
![](https://images.downcodes.com/u/info_img/2009-05/31/082012image024.jpg)
[Figure 5-11 CompareValidator settings dialog box]
The setting method of CustomValitor is similar to other controls, but the handwritten function OnServerValidate (in Events) is required to verify the data.
![](https://images.downcodes.com/u/info_img/2009-05/31/082013image026.jpg)
[Figure 5-12CustomValitor settings dialog box]
For example
< asp:CustomValidator id="CusValid" runat="server" ControlToValidate=Control name OnServerValidate="TheFunction" >Error message</ asp:CustomValidator >
<script language=”vb” runat=”server” >
Function TheFunction(sender as object,,value as string) as Boolean
……..
return…
End function
</ /script >
This control will call the TheFuncion function. If false is returned, an error message will be reported.
RangeValidator control
![](https://images.downcodes.com/u/info_img/2009-05/31/082014image028.jpg)
[Figure 5-12 RangeValidatorr setting dialog box]
You can set the value range through Mininum Value and Maxinum Value. At the same time, you can set the comparison type through Type, such as "string", "Integer", etc. Other settings are similar to those for other components.
There is also a component: ValidationSummary. Among them, HeaderText sets the header text, and DisplayMode sets the display mode. Its specific properties are as follows:
Attribute value | meaning |
List BulletList SingleParagraph | ErrorMessage is displayed in separate lines. ErrorMessage is displayed in separate lines. ErrorMessage is displayed in the same line. |
![](https://images.downcodes.com/u/info_img/2009-05/31/082015image030.jpg)
[Figure 5-13 ValidationSummary property setting dialog box]
Attached is the source code of the main program segments:
< table width="75%" height="594" border="1" cellpadding="1" cellspacing="0" bordercolor="#999999" >
<tr>
< td width="22%" height="22" >First Name< /td >
< td width="38%" >< asp:textbox BackColor="#CCCCCC" ForeColor="#0033FF" ID="first" runat="server" TextMode="SingleLine" ToolTip="Please input the first name" MaxLength ="40" BorderWidth="1" width="200"/ >< /td >
< td width="40%" >< asp:requiredfieldvalidator BackColor="#CCCCCC" ControlToValidate="first" ErrorMessage="RequiredField" ForeColor="#FF0000" ID="validname" runat="server" / > < /td >
< /tr >
<tr>
< td height="22" >Last Name< /td >
< td >< asp:textbox BackColor="#CCCCCC" ForeColor="#0033FF" ID="Lastname" runat="server" TextMode="SingleLine" Width="200" ToolTip="Please input the last name" BorderWidth= "1"/ >< /td >
< td >< asp:requiredfieldvalidator BackColor="#CCCCCC" ControlToValidate="Lastname" ErrorMessage="RequiredField" ForeColor="#FF0000" ID="vailLast" runat="server" / > </ /td >
< /tr >
<tr>
< td height="22" >Telephone< /td >
< td >< asp:textbox BackColor="#CCCCCC" BorderWidth="1" ForeColor="#0066FF" ID="telephone" runat="server" TextMode="SingleLine" ToolTip="Please input your telephone" Width=" 200" / >< /td >
< td > < asp:regularexpressionvalidator BackColor="#CCCCCC" ControlToValidate="telephone" ErrorMessage="7-10 numbers required" ForeColor="#FF0000" ID="vailtel" runat="server" ValidationExpression="[0-9 ]{7,10}" / > < asp:requiredfieldvalidator BackColor="#CCCCCC" ControlToValidate="telephone" ErrorMessage="RequiredField" ForeColor="#FF0000" ID="vailtel2" runat="server" / >< /td >
< /tr >
<tr>
< td height="22" >Email< /td >
< td >< asp:textbox BackColor="#CCCCCC" BorderWidth="1" ForeColor="#0033FF" ID="email" runat="server" TextMode="SingleLine" ToolTip="Please input your E-mail" width ="200"/ >< /td >
< td >< asp:regularexpressionvalidator BackColor="#CCCCCC" ControlToValidate="email" ErrorMessage="Invaliate Email address" ForeColor="#FF0000" ID="valiemail" runat="server" ValidationExpression=".{1,}@ .{3,}" / > < asp:requiredfieldvalidator BackColor="#CCCCCC" ControlToValidate="email" ErrorMessage="Required Field" ForeColor="#FF0000" ID="valiemail2" runat="server" / > < /td >
< /tr >
<tr>
< td height="22" colspan="3" >< div align="center" >Comments< /div >< /td >
< /tr >
<tr>
< td height="188" colspan="3" >< div align="center" >
< asp:textbox BackColor="#CCCCCC" BorderWidth="1" ForeColor="#0033FF" ID="Comments" Rows="16" runat="server" TextMode="MultiLine" ToolTip="Please input the Comments" width ="400"/ >
< /div >
< div align="center" > < br >
< /div >< /td >
< /tr >
<tr>
< td height="71" colspan="3" >< div align="center" >
< asp:validationsummary BackColor="#CCCCCC" DisplayMode="BulletList" ForeColor="#FF0000" HeaderText="Incorrect content includes:" ID="valSum" runat="server" ToolTip="Error" / >
< /div >< /td >
< /tr >
<tr>
< td height="22" colspan="3" >< input type="submit" name="Submit" value="Submit" >
</ /td >
< /tr >
< /table >
The style is as follows:
![](https://images.downcodes.com/u/info_img/2009-05/31/082016image032.jpg)
[Figure 5-14 Preview]
STEP 3 Data addition
As for the function of adding data, it is relatively simple. We can set the function of adding data through the wizard and let DreamweaverMX automatically add the code. Click the Server Behaviors tab in Application, and then click the + button to select Insert Record (Figure 5-15).
In the pop-up dialog box, you must first determine the connected data source. If it is not found in the list, you can click the Define button to set the data source (Figure 5-16). Insert into table is used to set the table to which data needs to be added. In Columns, the text box can be matched with the corresponding data source and the data type can be set. On success, Go To sets the page to jump to if data is added successfully. On Failure, Go To can set the page to jump to if adding data fails. At the same time, we can also choose Display Debugging Information On Failure to set the error message to be displayed when adding data fails.
![](https://images.downcodes.com/u/info_img/2009-05/31/082017image034.jpg)
[Figure 5-15 Select Insert Record]
![](https://images.downcodes.com/u/info_img/2009-05/31/082018image036.jpg)
[Figure 5-16 Data addition settings]
Let's look at the added code:
<MM:Insert
runat="server"
CommandText='< %# "INSERT INTO COMMENTS (COMMENTS, EMAIL, FIRST_NAME, LAST_NAME, TELEPHONE) VALUES (?, ?, ?, ?, ?)" % >'
ConnectionString='< %# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_location") % >'
DatabaseType='< %# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_location") % >'
Expression='< %# Request.Form("MM_insert") = "form1" % >'
CreateDataSet="false"
SuccessURL='< %# "index.htm" % >'
FailureURL='< %# "reg.aspx" % >'
Debug="true"
>
<Parameters>
< Parameter Name="@COMMENTS" Value='< %# IIf((Request.Form("Comments") < > Nothing), Request.Form("Comments"), "") % >' Type="WChar" />
< Parameter Name="@EMAIL" Value='< %# IIf((Request.Form("email") < > Nothing), Request.Form("email"), "") % >' Type="WChar" />
< Parameter Name="@FIRST_NAME" Value='< %# IIf((Request.Form("first") < > Nothing), Request.Form("first"), "") % >' Type="WChar" />
< Parameter Name="@LAST_NAME" Value='< %# IIf((Request.Form("Lastname") < > Nothing), Request.Form("Lastname"), "") % >' Type="WChar" />
< Parameter Name="@TELEPHONE" Value='< %# IIf((Request.Form("telephone") < > Nothing), Request.Form("telephone"), "") % >' Type="WChar" />
< /Parameters >
< /MM:Insert>
MM: Insert is the label used by Dreamweaver to add the database, and Parameter is the value used to specify the parameter.
The previous code is used to specify the database link and the page to jump to after the addition is successful. If the addition fails, an error message will be displayed, as well as the associated table.
These are developed by MacroMedia itself and require support from some components of Macromedia. They are different from the standard code format of many ASP.net on the Internet today. Please do not misunderstand the standard code format of ASP.net based on this code when referring to the code. You can refer to www.gotdotnet.com for the standard code format. If you want to write standard code, you can use WebMatrix, download address: http://www.asp.net/webmatrix/download.aspx?tabindex=4