PostBackUrl跨页传值

来源:互联网 发布:gb软件 编辑:程序博客网 时间:2024/06/05 18:00

页面和页面之间传参数可以通过url,也可以通过form来传值,今天总结的是另外一种,跨页传参。

服务器Button有一个PostBackUrl属性,这个是用来回传的,直接写代码算了。

一、请求页面:

HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ResponseUrl.aspx.cs" Inherits="MyWeb.PostBackDemo.ResponseUrl" %>

<!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>请求页面</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Button PostBack Demo
    </div>
    <div>
        <asp:Button ID="btPostBack" runat="server" Text="Button" PostBackUrl="~/PostBackDemo/RequestUrl.aspx" /></div>
    </form>
</body>
</html>

CS:

 

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

namespace MyWeb.PostBackDemo
{
    public partial class ResponseUrl : System.Web.UI.Page
    {
        public string ResponseString = string.Empty;
        public Customer MyCustomer;
        protected void Page_Load(object sender, EventArgs e)
        {
            ResponseString = "Hello World";
            MyCustomer = new Customer();
            MyCustomer.CustomerName = Page.Title;
            MyCustomer.CustomerAddress = Request.Url.AbsoluteUri;
            MyCustomer.CustomerAge = DateTime.Now.Second;
        }
    }
}

Customer类CS:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DoMain
{
    public class Customer
    {
        private string _customerName;
        private int _customerAge;
        private string _customerAddress;
        public string CustomerName {
            get { return _customerName; }
            set { _customerName = value; }
        }
        public int CustomerAge {
            get { return _customerAge; }
            set { _customerAge = value; }
        }
        public string CustomerAddress {
            get { return _customerAddress; }
            set { _customerAddress = value; }
        }  
    }
}

 

响应页面:

HTML

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequestUrl.aspx.cs" Inherits="MyWeb.PostBackDemo.RequestUrl" %>
<%@ PreviousPageType VirtualPath="~/PostBackDemo/ResponseUrl.aspx" %>
<!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>响应页面</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

 

CS:

 

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

namespace MyWeb.PostBackDemo
{
    public partial class RequestUrl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (PreviousPage != null)
            {
                Customer MyCustomer = PreviousPage.MyCustomer;
                Response.Write(PreviousPage.ResponseString);
                Response.Write("<br //>");
                Response.Write(MyCustomer.CustomerName);
                Response.Write("<br //>");
                Response.Write(MyCustomer.CustomerAddress);
                Response.Write("<br //>");
                Response.Write(MyCustomer.CustomerAge.ToString());
            }
        }
    }
}

 

关键的地方就是:

响应页面的HTML中的<%@ PreviousPageType VirtualPath="~/PostBackDemo/ResponseUrl.aspx" %>

OK,就这么简单

如果请求页面比较大,就比较耗能,如果页面不是很大的话,我觉得用这个还是蛮爽的,特别是对于那种带步骤的,分一,二,三等几步走的逻辑时,用这个比较爽。

原创粉丝点击