Learn how ASP2.0 delivers information to other Web pages

Source: Internet
Author: User
Tags html form include mail variables reference
Web page

A standard HTML form (form Element) allows you to pass and send data to another page or application by using the form element. In ASP.net 1.x, the Web page uses the delivery mechanism to submit page data to the page itself. For ASP.net 2.0, its functionality is extended to allow for cross page submissions. This week let's explore this new feature.

Traditional approaches

For ease of comparison, I'd like to take a minute to review the old way the page passes data. The HTML table element has an action attribute that specifies which resources (called Resources, a Web page, a script, a program, and so on) are used to process the submitted data on the server side. The following code is a sample.

<body>

<form name= "Frmsample" method= "post" action= "Target_url" >

<input type= "text" Name= "FullName" id= "FullName"/>

<input type= "button" name= "Submit" value= "Submit"/>

</form>

</body>

The value entered in the Text field (name is FullName) is presented to the page or program specified by the action attribute of the form element. For asp.net developers, even the use of standard HTML forms is extremely rare.

Asp. NET developers face the task of passing data information from one Web page to another, the scope of the method selection is extremely broad. They include conversational variables (session variables), cookies, querystring variables, caching (Web page caching), and even server.transfer methods, but ASP.net 2.0 offers another option.

asp.net 2.0 A further approach is provided

When designing asp.net 2.0, Microsoft realized the need to cross data across pages. With this awareness, a PostBackUrl property is added to the button (button) control for the asp.net. It allows you to indicate where the form and the above data are sent (that is, determined by the URL value specified by the PostBackUrl property) when the user submits it. Generally speaking, cross-page delivery is the process by which the client uses JavaScript in the background to deliver.

 <%@ Page language= "vb"%> 
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
<title>cross Postback Example</title>
<form id= "FrmCrossPostback1" method= "post" runat = "Server"
<asp:label id= "lblname" runat= "Server" text= "Name:" ></ASP:LABEL>
<asp:textbox Id= "txtname" runat= "server" ></asp:textbox><br/>
<asp:label id= "Lble-mailaddress" Server "text=" e-mail: "></asp:label>
<asp:textbox id=" txte-mailaddress "runat=" Server "></asp: TEXTBOX><BR/>
<asp:button id= "btnsubmit" runat= "Server" text= "Submit" postbackurl= " Crosspostback2.aspx "/>
</form></body>

The ASP.net page in has two text fields (name and e-mail, respectively), and a button (button) for submitting data. The PostBackUrl attribute of this submit button is specified as another page so that the data can be sent to that page when the form is submitted. Note: In this example, the form element uses post[2 to submit the form when it is submitted by setting the method property, but this is not necessary because all cross postback (cross-page postings) Use the Post method according to the design.

Use previous page

Asp. NET page is loaded by a call to a cross-page posting, the IsPostBack property of the object above it is not triggered. However, there is a property called PreviousPage (Previous page) that enables you to access and use pages that apply a cross-page posting.

Each time a cross-page request occurs, the PreviousPage property of the current page saves the page references that promote delivery. The PreviousPage property will not be initialized if the page is not generated from a cross-page posting, or if the page is in a different program group.

You can check the PreviousPage object to determine whether the page's load is the result of a cross-page posting. If the value is null, then a normal load, not a null value, indicates that the page is from a cross-page posting. In addition, page classes (Page Class) also contain a method called Iscrosspagepostback, which is specifically designed to determine whether a page is a cross-page delivery result.

Once a cross-page post has been identified, you can access the control on the call page (calling page) by PreviousPage the FindControl method of the object. The following code is the second page in our example, which is called by the page listed earlier.

<%@ Page language= "vb"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title>cross Postback Example 2</title>
<script language= "VB" runat= "Server" >
Sub Page_Load ()
If not (Page.previouspage are nothing) Then
If not (Page.iscrosspagepostback) Then
Response.Write ("Name:" + CType (Previouspage.findcontrol ("Txtname"), TextBox). Text + "<BR>")
Response.Write ("E-mail:" + CType (Previouspage.findcontrol ("txte-mailaddress"), TextBox). Text + "<BR>")
End If
End If
End Sub
</script></body>

This page first determines whether it is invoked by a cross-page posting. If so, the values from the calling page are accessed through the FindControl method, and the controls obtained by using this method are converted to TextBox controls, and then the contents of their text (text) properties are displayed.

You can convert the entire PreviousPage object to a page type that triggers a cross-page posting. This method allows you to access the page's global attributes (public properties) and methods. Before I give an example of this technique, it is necessary for me to rewrite the first example to include some global properties. The following code is the first manifest that adds two properties that are used to access the domain values.

<%@ Page language= "vb"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title>cross Postback example</title>
<script language= "VB" runat= "Server" >
Public ReadOnly Property Name
Get
Return Me.txtName.Text
End Get
End Property
Public ReadOnly Property E-mailaddress
Get
Return Me.txte-mailaddress.text
End Get
End Property
</script><form id= "FrmCrossPostback1" method= "POST" runat= "Server" >
<asp:label id= "Lblname" runat= "Server" text= "Name:" ></asp:Label>
<asp:textbox id= "txtname" runat= "Server" ></asp:textbox><br/>
<asp:label id= "lble-mailaddress" runat= "Server" text= "e-mail:" ></asp:Label>
<asp:textbox id= "txte-mailaddress" runat= "Server" ></asp:textbox><br/>
<asp:button id= "btnsubmit" runat= "Server" text= "Submit" postbackurl= "crosspostback2.aspx"/>
</form></body>

Now that the properties have been built, you can easily access them. Be wary that the PreviousPage object of the page class must be converted to the correct type in order to properly access its properties. This can be accomplished by converting it into an object of the appropriate page class.

<%@ Page language= "vb"%>
<%@ Reference page= "~/crosspostback1.aspx"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title>cross Postback Example 3</title>
<script language= "VB" runat= "Server" >
Sub Page_Load ()
Dim Cpppage as Crosspostback1_aspx
If not (Page.previouspage are nothing) Then
If not (Page.iscrosspagepostback) Then
If (Page.PreviousPage.IsValid) Then
Cpppage = CType (previouspage, crosspostback1_aspx)
Response.Write ("Name:" + cpppage.name + "<br>")
Response.Write ("E-mail:" + cpppage.e-mailaddress)
End If
End If
End If
End Sub
</script></body>

Illustrates this by defining a reference to the calling page in the header of the page so that the reference type can be used in code. With this reference, the actual vb.net code uses the CType function to convert the PreviousPage object to the appropriate type. After that, those attributes can be used as code demonstrations.

About the use of the PreviousPage object IsValid method in the above list here's a reminder: the IsValid property of the previous page guarantees that it has passed all legitimate validation tests before it can be manipulated.

Summarize

There are many applications for passing data parameters between Web pages, including maintaining personal user information. Ancestral Web solutions, like using querystring and cookies, allow you to easily point to another page from one page when submitting occurs.

ASP.net 1.1 provides a good support for these methods in addition to providing additional methods, but ASP.net 2.0 relies on a cross-page delivery to make a significant progress. It makes it easier for a Web page to process data from another page. When you develop your next ASP.net 2.0 program, take advantage of this new concept.



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.