loading ...
loading...

2007-08-08 | ASP.NET 2.0中的跨页面提交

分享

ASP.NET 1.X 版本中,页面都是提交到自己本身,并不能方便的指定需要提交的目的页面。例如FirstPage.aspx中的button只能提交到FirstPage.aspx,而不能提交到SecondPage.aspx。很多时候,ASP.NET 1.X这样工作方式使我们的开发方式受到不少限制。熟悉ASP/JSP/PHP的朋友大概很不习惯,因为以前经常使用的提交方式突然无法使用,虽然也有解决这个问题的方法(演示Webcast),可是过程太烦琐,不甚方便。令我们高兴的是,ASP.NET 2.0中有了跨页面提交的简单方法。

简单的例子

首先看看下面的代码,FirstPage.aspx中的Button通过指定PostBackUrl属性可以提交到指定的页面:

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>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<meta content="text/JScript" http-equiv="content-script-type" />
    
<title>First Page</title>
</head>
<body>
  
<form id="form1" runat="server">
    
<div>
      
<h3>The Frist Page</h3>
        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:Button ID="Button2" runat="server" Text="Postback to Second Page" PostBackUrl="~/SecondPage.aspx" /><br />
    
</div>
  
</form>
</body>
</html>

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>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Second Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<h1>This is the Second Page</h1>
        
<p><asp:Label ID="Label1" runat="server"></asp:Label>&nbsp;</p>    
    
</div>
    
</form>
</body>
</html>
打开FirstPage.aspx,输入内容并按下“Postback to Second Page”按钮提交,页面就会提交到SecondPage.aspx,输入的内容也会显示在SecondPage.aspx上。注意FirstPage.aspxButton2新增的PostBackUrl属性,还有SecondPage.aspx中的@PreviousPageType指令。这些新增的内容,正是ASP.NET 2.0 中的跨页提交的方案的组成部分

读取源页面的信息

ASP.NET 2.0中,Button控件有个新增的属性PostBackUrl,用来设置需要提交的目标页面。因为只要指定Button控件的PostBackUrl属性就可以提交到其它页面,我们可以在页面中使用多个控件配置其PostBackUrl的属性,提交到不同的页面。当然也可配置多个页面提交到同一个页面。

    在跨页面提交之后,通常我们需要从源页面中读取控件的信息(即由浏览器发送的信息),以及源页面的公共属性。

分享 分享 |  评论 (1) |  阅读 (?)  |  固定链接 |  类别 (技术交流) |  发表于 09:45  | 最后修改于 2007-08-08 10:03
搜狐博客温馨提示:搜狐博客官方不会要求参加活动的各位博友缴纳任何的手续费用。请勿轻信留言、评论中的中奖信息,更不要拨打陌生电话及向陌生帐户汇款,谨防受骗!识别更多网络骗术,请 点击查看详情
正在读取评论信息...
您还未登录,只能匿名发表评论。或者您可以 登录 后发表。
 
  *中国人爱国心,搜狗输入法爱国主题皮肤下载>>
表  情:
加载中...
回复通知: 同时用小纸条通知对方该回复