ASP.NET 2.0 Tips(1):跨页提交

在ASP.NET 1.x的时候,很多朋友可能需要进行跨页提交的处理,也就是从页面A能够提交到页面B,甚至不同的Control其目标处理页面也各不相同。尤其是从ASP/JSP/PHP转过来的开发人员,可能更有这种需求。但很不幸,在ASP.NET 1.x的时候,处理这种跨页请求是十分丑陋的,需要非常多的“技巧化”处理。

在ASP.NET 2.0的时候,对于跨页提交已经有了非常合理的解决方案,以下就是一个示例。

SourcePage.aspx: 请注意Button1的PostBackUrl属性设置

![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedBlockStart.gif)![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ContractedBlock.gif)<%...@ Page Language="C#" %> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedBlockStart.gif)![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ContractedBlock.gif)<script runat="server">... ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/InBlock.gif) public string YourName ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ContractedSubBlock.gif) ...{ ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/InBlock.gif) get ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ContractedSubBlock.gif) ...{ ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/InBlock.gif) return this.TextBox1.Text; ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedSubBlockEnd.gif) } ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedBlockEnd.gif) } ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)</script> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)<html xmlns="http://www.w3.org/1999/xhtml" > ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)<head runat="server"> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <title>Untitled Page</title> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)</head> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)<body> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <form id="form1" runat="server"> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <div> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <asp:Label ID="Label1" runat="server" Text="请输入您的姓名" Width="183px"></asp:Label> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <asp:Button ID="Button1" runat="server" Text="提交" PostBackUrl="~/TargetPage.aspx" /></div> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) </form> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)</body> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)</html> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) **TargetPage.aspx**:请注意PreviousPageType的属性设置
![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedBlockStart.gif)![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ContractedBlock.gif)<%...@ Page Language="C#" %>
![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedBlockStart.gif)![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ContractedBlock.gif)
<%...@ PreviousPageType VirtualPath="~/SourcePage.aspx" %> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedBlockStart.gif)![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ContractedBlock.gif)<script runat="server">... ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/InBlock.gif) ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/InBlock.gif) protected void Page_Load(object sender, EventArgs e) ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ContractedSubBlock.gif) ...{ ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/InBlock.gif) this.Label1.Text = PreviousPage.YourName; ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/ExpandedBlockEnd.gif) } ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)</script> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)<html xmlns="http://www.w3.org/1999/xhtml" > ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)<head runat="server"> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <title>Untitled Page</title> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)</head> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)<body> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <form id="form1" runat="server"> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <div> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) <asp:Label ID="Label1" runat="server" ></asp:Label> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) </div> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif) </form> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)</body> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)</html> ![](http://blog.joycode.com/wp-content/uploads/images/OutliningIndicators/None.gif)
OK,就通过这么简单的两个属性设置,就可以非常方便的得到跨页提交的特性。当然,您也可以根据您自己的需求,比如每个Control需要提交到不同的页面来进行更加复杂的设置。