Cross page postback

来源:互联网 发布:php socket 长连接 编辑:程序博客网 时间:2024/05/01 01:31
 
        在ASP.NET 1.X 版本中,页面都是提交到自己本身,并不能方便的指定需要提交的目的页面。如果想实现跨页面提交,会比较麻烦。下面这个例子是用frame技术可以在ASP.NET 1.X实现跨页面提交提交的功能。

 Frame.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title>Untitled Page</title>
</head>

<frameset cols="50%,*">

<noframes>
<body>
you brownser do not support Frame!
</body>
</noframes>
<frame  name="left" src="Left.aspx"/>
<frame name="right" src="Right.aspx" />

</frameset>


</html>

left.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Left.aspx.cs" Inherits="DevSummary_Left" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server" method="post" target="right">
    
<div>
        
<table>
            
<tr>
                
<td style="width: 84px">
                    Name1
</td>
                
<td style="width: 228px">
                    
<asp:TextBox ID="TextBox1" runat="server" Width="218px"></asp:TextBox></td>
            
</tr>
            
<tr>
                
<td style="width: 84px">
                    Name2
</td>
                
<td style="width: 228px">
                    
<asp:TextBox ID="TextBox2" runat="server" Width="219px"></asp:TextBox></td>
            
</tr>
            
<tr>
                
<td style="width: 84px">
                
</td>
                
<td style="width: 228px; text-align: right">
                    
<asp:Button ID="Button1" runat="server" Text="Button" /></td>
            
</tr>
        
</table>
    
    
</div>
    
</form>
</body>
</html>

right.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Right.aspx.cs" Inherits="DevSummary_Right" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<table>
            
<tr>
                
<td style="width: 100px; height: 21px;">
                    Name1
</td>
                
<td style="width: 177px; height: 21px;">
                    
<asp:Label ID="Label1" runat="server" Text="Label" Width="185px"></asp:Label></td>
            
</tr>
            
<tr>
                
<td style="width: 100px">
                    Name2
</td>
                
<td style="width: 177px">
                    
<asp:Label ID="Label2" runat="server" Text="Label" Width="183px"></asp:Label></td>
            
</tr>
        
</table>
    
    
</div>
    
</form>
</body>
</html>

 

 
       到asp.net2.0的时候,微软意识到跨页面提交对于开发人员的重要性,提供了对跨页面提交的直接支持。用户可以通过指定提交Button/LinkButton(。。)PostUrl来指定提交页面的URL,而目标页面你可以使用Page. PreviousPage 得到前一个页面的引用,这样页面上任何一个control的状态,你可以通过下面的三种方式获得提交的数据:
  • [1]  Page. PreviousPage.FindControl(id)
  • [2] 在目标页加入PreviousPageType页面指令,并指定VirtualPath,这样可以把PreviewPage作为一个强类型的页面来访问,如:string name=PrevioursPage.Name

                       <%@ PreviousPageType VirtualPath="~/Tricks/Page1.aspx" %>

  • [3] 在目标页加入PreviousPageType页面指令,指定TypeName,这样也可以把PreviewPage作为一个强类型的页面来访问,如:string name=PrevioursPage.Name但这种情况下必须注意的是PersonInfoPage 必须派生自System.Web.UI.Page

                   <%@ PreviousPageType TypeName="PersonInfoPage" %>

Page1.aspx codebehind :

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Tricks_Page1 : System.Web.UI.Page
{


    public string Name
    {
        get
        {
            return TextBox1.Text;
        }
    }
    public string Age
    {
        get
        {
            return TextBox2.Text;
        }
    }

    public bool IsMarried
    {
        get
        {
            return chkMarried.Checked;
        }
    }

  }

 

Pag2.aspx code behind :

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Tricks_Page3 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (PreviousPage!=null)
        
{
            
this.Label1.Text = PreviousPage.Name;
            
this.Label2.Text = PreviousPage.Age;
            
this.CheckBox1.Checked = PreviousPage.IsMarried;
            
this.CheckBox1.Enabled = false;
        }


    }

}

 

注意:Pag2.apsx是含有页面指令的

<%@ PreviousPageType VirtualPath="~/Tricks/Page1.aspx" %>

      对于通过PreviousPageType 页面指令来访问,需要注意:

     [1] 由于ASP.NET中的每个页面类所包含的子控件对应的是protected成员所以您不能直接通过 PreviousPage引用来访问源页面中的控件,而先需要将源页面中需要被访问的属性公开出

     [2]使用方法一的时候,若页面是包含有 Master页面的话,(即是一个Content Page ),下面的代码是无法拿到PreviousPageControl (Data)的:

  string str= Request.Form["txtData"]; //it does't work

  TextBox t = (TextBox)PreviousPage.FindControl("txtData");

正确的因该是:

 string str = Request.Form["ctl00$ContentPlaceHolder1$txtData"];  //OK
TextBox t = (TextBox)PreviousPage.Form.FindControl("ContentPlaceHolder1").FindControl("txtData"); //OK
           原因是由于ContentPage在render之前会把MasterPage和并(可以把MasterPage理解为ContentPage的一个Control),因此你的control id因该是:ctl00$ContentPlaceHolder1$id
   [ctl00 is the form element, and contentPlaceHoder1 is the id of the contentPlaceHolder]这一点,我可以从页面Trace output的结果是很容易看出来的:


     [3]如果要判断是否为跨页面提交,可以对目标页面的PreviousPage属性返回的引用页面的IsCrossPagePostBack属性进行判断。注意,如果当前页面不是跨页面提交的目标页面,则其PreviousPage属性为空