ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Asp.net 页面间传值简记

Asp.net 页面间传值简记

原创 Linux操作系统 作者:kirinri 时间:2009-03-20 17:00:55 0 删除 编辑

Asp.net 页面间传值简记

1. URL传值
Page1:Context.Server.Transfer("Default2.aspx?RequestValue=---100---");
Page2:string str = Request["RequestValue"].ToString();

2. Session or Application 传值
Page1:Session["SessPage1"] = "---Session Value---";
Page2:str += Session["SessPage1"].ToString();

3. 使用Context.Handler获取上页面
Page1:Context.Server.Transfer("Default2.aspx?RequestValue=---100---");
Page2:_Default page1 = (_Default)Context.Handler;

4. 使用Context.Handler 传递实体类
可以使用此方法抽象所需要的内容,抽象为基页面,继承页面都能实现此方法获取前页面中的实体类。

具体内容:

Page1:

using System;

using System.Data;

using System.Configuration;

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 _Default : System.Web.UI.Page, IEntity.IEntity

{

    public System.Web.UI.WebControls.Label pubLab;

    private Entity.Entity ownerEntity;

 

    public Entity.Entity GetEntity

    {

        get

        {

            return ownerEntity;

        }

    }

 

    protected void Page_Load(object sender, EventArgs e)

    {

        pubLab = new Label();

        Label1.Text = "---Page1---";

        pubLab.Text = "---pubLab---";

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        // Session or Application

        Session["SessPage1"] = "---Session Value---";

 

        // Entity Object

        ownerEntity = new Entity.Entity();

        ownerEntity.ParC = "Default.aspx change value!";

 

        // Transfer Page to Default2.aspx

        Context.Server.Transfer("Default2.aspx?RequestValue=---100---");

        //Response.Redirect("Default2.aspx?RequestValue=---100---");

    }

 

    public string Setlab1

    {

        get { return this.Label1.Text; }

        set { this.Label1.Text = value; }

    }

}

 

Page2:

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 Default2 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        // Get Url Value

        string str = Request["RequestValue"].ToString();

        // Get Session Value

        str += Session["SessPage1"].ToString();

        // Get Page Control Value

        _Default page1 = (_Default)Context.Handler;

 

        str += "---Convert Handler---";

 

        str += page1.Setlab1.ToString();

 

        str += page1.pubLab.Text.ToString();

 

        lab.Text = str;

 

        Entity.Entity subEntity = null;

        IEntity.IEntity entityInterface = null;

        if( Context.Handler is IEntity.IEntity)

        {

            entityInterface = ( IEntity.IEntity )Context.Handler;

            subEntity = entityInterface.GetEntity;

        }

 

 

        Label1.Text = subEntity.ParA + subEntity.ParB + subEntity.ParC;

 

    }

}

 

实体类:

using System;

using System.Collections.Generic;

using System.Text;

 

namespace Entity

{

    public class Entity

    {

 

        // Test Paramater

        private string parA;

        private string parB;

        private string parC;

 

        public string ParA

        {

            get { return parA; }

            set { parA = value; }

        }

 

        public string ParB

        {

            get { return parB; }

            set { parB = value; }

        }

 

        public string ParC

        {

            get { return parC; }

            set { parC = value; }

        }

 

        public Entity()

        {

            this.ParA = "---Test A---";

            this.ParB = "---Test B---";

            this.ParC = "---Test C---";

        }

    }

}

 

接口类:

using System;

using System.Collections.Generic;

using System.Text;

 

namespace IEntity

{

    public interface IEntity

    {

        Entity.Entity GetEntity { get;}

    }

}

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12476590/viewspace-573251/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 有点意思!
请登录后发表评论 登录
全部评论

注册时间:2007-12-21

  • 博文量
    257
  • 访问量
    502565