您的位置:首页 > 编程语言 > ASP

asp.net页面传值的几种方法大全

2010-10-13 23:33 489 查看
这些方法都是在网上搜到的,现在只是临时写一下,过段时间再修改。

testA.aspx后台代码:

using System;
using System.Web;
namespace WebApplication1
{
public partial class testA : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// PassValueByCookie();
// Response.Redirect("TestB.aspx");
// PassValeByContext();
PassValueByApplication();

Server.Transfer("TestB.aspx");
}
/// <summary>
/// 使用Server.Transfer方法
/// </summary>
private void PassValueByRedirect()
{
Response.Redirect("TestB.aspx?A=b&AA=12&AAA=45");
}
/// <summary>
/// 使用Cookie对象变量
/// </summary>
private void PassValueByCookie()
{
HttpCookie cookie_name = new HttpCookie("name");
cookie_name.Value = "12";
Response.AppendCookie(cookie_name);
}
/// <summary>
///  Context
/// </summary>
public void PassValeByContext()
{
string Str = "Hello World";
this.Context.Items.Add("Text", Str);
}
/// <summary>
/// 使用Application 对象变量
/// </summary>
private void PassValueByApplication()
{
Application["name"] ="Hello!";
}
public string Name
{
get { return Label1.Text; }
}

}
}


TestB.aspx后台代码:

using System;
namespace WebApplication1
{
public partial class TestB : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// GetValueByQueryString();
//GetValueByCookie();
// GetValueByPageFlow();
// GetValueByContext();
GetValueByApplication();
}
/// <summary>
///
/// </summary>
private void GetValueByQueryString()
{
string a = Request.QueryString["A"].ToString();
string aa = Request.QueryString["AA"].ToString();
string aaa = Request.QueryString["AAA"].ToString();
}
private void GetValueByCookie()
{
string name = Request.Cookies["name"].Value.ToString();
}
private void GetValueByPageFlow()
{
testA newWeb= Context.Handler as testA;;   //实例a窗体
string name;
name = newWeb.Name;
}
private void GetValueByContext()
{
string a= this.Context.Items["Text"].ToString();//HttpContext接收数据
}
private void GetValueByApplication()
{
string name;
Application.Lock();
name = Application["name"].ToString();
Application.UnLock();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: