Implementing cross-page submissions in ASP.net 2.0

Source: Internet
Author: User

In the ASP.net 1.X version, the page is submitted to itself, and it is not convenient to specify the destination page to be submitted. For example, a button in firstpage.aspx can only be committed to firstpage.aspx, not to secondpage.aspx. Most of the time, the way the ASP.net 1.X works has led to a lot of restrictions on how we develop. Familiar with asp/jsp/php friend probably very unaccustomed, because used to submit the way suddenly cannot use, although also has solves this problem the method (wants to understand the reader to be able to go to the Microsoft website to watch webcast), but the process is too cumbersome, inconvenient. To our delight, ASP.net 2.0 has a simple way of submitting across pages.

A simple example

First look at the following code, the button in the firstpage.aspx can be submitted to the specified page by specifying the PostBackUrl property:

Firstpage.aspx

<%@ Page language= "C #"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd  
<script runat= "Server"
Public String UserName {
Get {
return this.txtName.Text;

}
protected void Button1_Click (object sender, EventArgs e)
{
Label1.Text = "postback from self. Your Name is: "+ txtname.text;  
}
</script>
<meta content= "Text/jscript" http-equiv= "Content-script-type"/>
<title>first Page</title>
<body>
<form id= "Form1" runat= "Server"
<div>
Your Name:
<asp:textbox id= "txtname" runat= "Server"/>
<asp:label id= "Label1" runat= "Server" enableviewstate= "False"/><br/>
<br/>
<asp:button id= "ButtoN1 "runat=" server "text=" postback to Same Page "onclick=" button1_click "/><br/>
<br/>
<asp:bu Tton id= "Button2" runat= "server" text= "postback to Second Page" postbackurl= "~/secondpage.aspx"/><br/>
&L T;/div>
</form>
</body>

Secondpage.aspx

<%@ Page Language="C#" %>
<%@ PreviousPageType VirtualPath="~/FirstPage.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
 this.Label1.Text = "Your Name is : " + PreviousPage.UserName;
}
</script>
<title>Second Page</title>
<body>
<form id="form1" runat="server">
<div>
<p><asp:Label ID="Label1" runat="server"> </asp:Label>&nbsp;</p>
</div>
</form>
</body>

Open firstpage.aspx, enter the content and press the "postback to Second page" button to submit, the page will be submitted to secondpage.aspx, the input will also be displayed on the secondpage.aspx. Note the Button2 new PostBackUrl attribute in Firstpage.aspx, as well as the @previouspagetype instruction in secondpage.aspx. These new additions are part of the proposed ASP.net 2.0 cross page submission.

You may find that the target page is open in the window of the source page when you use the Cross page submission feature of ASP.net 2.0. But sometimes we need to open the target page in a new window, which can be done by modifying the <form> properties in the source page. As shown in the following code:

<form id="MainForm" Target="_blank" runat="server">

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.