How to use HTML tables in ASP

Source: Internet
Author: User
Tags end html tags http post http request include numeric value table definition valid
Using the ASP Request object, you can create a simple yet powerful script to collect and process HTML table data. In this topic, you will not only learn how to create basic table-processing scripts, but you will also get some useful techniques for validating tabular information on WEB servers and user browsers.

  About HTML Tables

HTML tables are the most common method of collecting web information, and are the arrangement of special HTML tags that provide user interface controls on a Web page. text boxes, buttons, and check boxes are typical controls that enable users to interact with a Web page and submit information to a Web server.

The following HTML example produces a table in which the user can enter a name, age, and include a button that submits the information to the WEB server. The table also contains an implied control (which is not displayed by the Web browser) that you can use to pass additional information to your Web browser.

<form method= "POST" action= "myfile.asp" >
<input type= "text" name= "FirstName" >
<input type= "text" name= "LastName" >
<input type= "text" name= "age" >
<input type= "hidden" name= "UserStatus" value= "new" >
<input type= "Submit" value= "Enter" >
</FORM>

  Working with ASP input tables

When a table submits information to a Web server, the user's Web browser requests the. asp file specified in the HTML <form > tag's ACTION attribute (in the previous example, the file is called myfile.asp). The. asp file contains processing table values, such as A script that displays a result table or queries information from a database.

The value of an HTML table can be collected in three ways using an. asp file

static. The HTM file can contain a table that sends its numeric value to an. asp file. The. asp file can create a table that sends its information to another. asp file.. asp files can create a table that contains its information, that is, a file containing the form.

The first two methods work the same way, and when the table interacts with the gateway program, it can include reading and responding to user-selected commands in addition to ASP.

Creating an. asp file that contains a table definition and transmits information to itself is more complex, but it is a powerful way to process tables. This process is demonstrated in validation form input.

  Get form Input

The ASP Request object provides two sets of collections that greatly simplify the task of retrieving tabular information attached to a URL request.

  QueryString Collection

The QueryString collection gets the value that is passed to the WEB server as text following the question mark at the requested URL. By using the HTTP get method or by manually adding a table's value to the URL, the table's value can be appended to the requested URL.

For example, if the previous table example uses the Get method (ACTION = "get") and the user types Jeff, Smith, and 30, the following URL request is sent to the server:

  Http://scripts/Myfile.asp?firstname=Jeff&lastname=Smith&age=30&userstatus=new

Myfile.asp contains the following table processing scripts:

Hello, <%= request.querystring ("FirstName")%> <%= request.querystring ("LastName")%>.
You are <%= request.querystring (' age ')%> years old.
<%
If request.querystring ("userstatus") = "New User" then
Response.Write "This is your" the "This is" a visit to the Web site! "
End If
%>

In this case, the Web server will return the following text to the user's Web browser:

Hello, Jeff Smith. You are are years old. This is your the visit to this Web site!

The QueryString collection has an optional parameter that you can use to access one of the multiple values displayed in the request body. You can also use the Count property to calculate the number of occurrences of a particular type of value.

For example, a table containing a multiple-item list box can submit the following request:

  Http://list.asp?food=apples&food=olives&food=bread

You can also count multiple values using the following command:

Request.QueryString ("Food"). Count

If you want to display the types of multiple values, list.asp should include the following script;

<%total = Request.QueryString ("Food"). Count%>
<%for i = 1 to total%>
<%= request.querystring ("Food") (i)%> <BR>
<%Next%>

The above script will show:

Apples
Olives
Bread
Form Collection

When you use an HTTP get method to pass a long, complex table value to a WEB server, you may lose information. Most WEB servers tend to strictly control the length of the URL query string so that lengthy table values that are routed using the Get method are truncated. If you need to send a large amount of information from a table to a WEB server, you must use the HTTP POST method. This method is used to send tabular data in the body of an HTTP request, and the number of characters sent can be infinitely numerous. You can also use the Form collection of the ASP Request object to retrieve values sent by the POST method.

The form collection stores numeric values in the same way as the QueryString collection. For example, if a user fills a table with a long string of names, you can retrieve the names with the following script:

<% for i = 1 to Request.Form.Count%>
<% =request.form ("names") (i)%>
<% Next%>

  Validate Form Input

A good table-handling script should verify that the information in the input table is valid before processing the data. The validation script can verify that the type of information the user entered into the table is correct. For example, if your Web site contains a table that allows users to calculate financial information, you need to verify that the user did enter numeric information instead of text before processing the results.

A handy way to validate form input is to create a table that delivers information to itself. In this case, the. asp file contains a table that can get information. For example, the following script verifies that a user has entered a value in the Age table field by passing information to itself:

<% If IsNumeric (Request.QueryString ("age")) then%>
<p>hello, your age are <%=request.querystring ("age")%>
<%else%>
<p>please Enter a numerical age.
<%end If%>

<form method= "POST" action= "verify.asp" >
Name: <input type= "text" name= "name" >
Age: <input type= ' text ' name= ' age ' >
<input type= "Submit" value= "Enter" >
</FORM>

In this example, the script is also in the same verify.asp file that contains the table. The table transmits information to itself by specifying verify.asp in the ACTION attribute.

You can also create client script to verify that the user has entered valid information. Verifying user input on a Web browser can also reduce network traffic on the Web server, in addition to prompting the user to make table entry errors more quickly. The following script runs on the user's Web browser and authenticates the user information before submitting the information to the Web server.

<script language= "VBScript" >
<!--
Sub Btnenter_onclick
Dim theform
Set theform = Document.myform
If IsNumeric (TheForm.Age.Value) Then
Theform.submit
Else
Msgbox "Please enter a numerical age."
End If
End Sub
-->
</SCRIPT>

<form method= "POST" name= MyForm action= "myfile.asp" >
Name: <input type= "text" name= "name" >
Age: <input type= ' text ' name= ' age ' >
<input type= "button" Name= "Btnenter" value= "Enter" >
</FORM>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.