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

ASP.NET页面间传值的几种方式

2015-05-08 13:58 435 查看
1。使用QueryString

使用QuerySting在页面间传递值已经是一种很老的机制了,这种方法的主要优点是实现起来非常简单,然而它的缺点是传递的值是会显示在浏览器的地址栏上的(不安全),同时又不能传递对象,但是在传递的值少而安全性要求不高的情况下,这个方法还是一个不错的方案。使用这种方法的步骤如下:

1,使用控件创建web表单(form)

2,创建可以返回表单的按钮和链接按钮

3,在按钮或链接按钮的单击事件里创建一个保存URL的字符变量

4,在保存的URL里添加QueryString参数

5,使用Response.Redirect重定向到上面保存的URL

下面的代码片断演示了如何实现这个方法:

源页面WebForm1.aspx.cs中的部分代码:

private void Button1_Click(object sender, System.EventArgs e)

{

string url;

url="WebForm2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;

Response.Redirect(url);

}

目标页面WebForm2.aspx.cs中的部分代码:

private void Page_Load(object sender, System.EventArgs e)

{

Label1.Text=Request.QueryString["name"];

Label2.Text=Request.QueryString["email"];

}

2。使用Session变量

使用Session变量是可以在页面间传递值的的另一种方式,在本例中我们把控件中的值存在Session变量中,然后在另一个页面中使用它,以不同页面间实现值传递的目的。但是,需要注意的是在Session变量存储过多的数据会消耗比较多的服务器资源,在使用session时应该慎重,当然了,我们也应该使用一些清理动作来去除一些不需要的session来降低资源的无谓消耗。使用Session变量传递值的一般步骤如下:

1,在页面里添加必要的控件

2,创建可以返回表单的按钮和链接按钮

3,在按钮或链接按钮的单击事件里,把控件的值添加到session变量里

4,使用Response.Redirect(或Server.Transfer)方法重定向到另一个页面

5,在另一个页面提取session的值,在确定不需要使用该session时,要显式清除它

下面的代码片断演示了如何实现这个方法:

源页面WebForm1.aspx.cs中的部分代码:

private void Button1_Click(object sender, System.EventArgs e)

{

//textbox1 and textbox2 are webform

//controls

Session["name"]=TextBox1.Text;

Session["email"]=TextBox2.Text;

Server.Transfer("WebForm2.aspx");

}

目标页面WebForm2.aspx.cs中的部分代码:

private void Page_Load(object sender, System.EventArgs e)

{

Label1.Text=Session["name"].ToString();

Label2.Text=Session["email"].ToString();

Session.Remove("name");

Session.Remove("email");

}

3.使用Server.Transfer

这个方法相比上面介绍的方法稍微复杂一点,但在页面间值传递中却是特别有用的,使用该方法你可以在另一个页面以对象属性的方式来存取显露的值,当然了,使用这种方法,你需要额外写一些代码以创建一些属性以便可以在另一个页面访问它,但是,这个方式带来的好处也是显而易见的。总体来说,使用这种方法是简洁的同时又是面向对象的。使用这种方法的整个过程如下:

1,在页面里添加必要的控件

2,创建返回值的Get属性过程

3,创建可以返回表单的按钮和链接按钮

4,在按钮单击事件处理程序中调用Server.Transfer方法转移到指定的页面

5,在第二个页面中,我们就可以使用Context.Handler属性来获得前一个页面实例对象的引用,通过它,就可以使用存取前一个页面的控件的值了

以下代码综合实现上述步骤过程的代码:

源页面WebForm1.aspx.cs中的部分代码:

把以下的代码添加到页面中

public string Name

{

get

{

return TextBox1.Text;

}

}

public string EMail

{

get

{

return TextBox2.Text;

}

}

然后调用Server.Transfer方法

private void Button1_Click(object sender, System.EventArgs e)

{

Server.Transfer("WebForm2.aspx");

}

目标页面代码:

在WebForm2.aspx中务必在第一句话添加

<%@ Reference Page="~/WebForm1.aspx" %>或

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

然后在WebForm2.aspx.cs中添加如下。

private void Page_Load(object sender, System.EventArgs e)

{

//create instance of source web form

WebForm1 wf1;

//get reference to current handler instance

wf1=(WebForm1)Context.Handler;

Label1.Text=wf1.Name;

Label2.Text=wf1.EMail;

}

如果在调试的过程中遇到错误.就到C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files中把新建的网站名的文件夹删掉.(要先关掉Visual Studio开发环境再删)

4.使用@PreviousPageType指令

这个指令是.net 2.0中的一个新指令,用于处理ASP.NET 2.0提供的跨页面传送新功能.用于批定跨页面的传送过程起始于哪个页面.包含两个属性:

TypeName:设置回送时的派生类名

VirtualPath:设置回送时所传送页面的地址.

如下示例:

源页面WebForm1.aspx中有一个TextBox,ID为txtName.在WebForm1.aspx.cs中设置一个属性:

public TextBox Name

{

get{return this.txtName;}//返回一个控件对象

}

在目标页面的设计文件中(WebForm2.aspx)的最上方加上:

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

然后就能引用WebForm1.aspx中定义的属性了.

在WebForm2.aspx.cs中可以有如下引用形式(假设WebForm2.aspx中有一个ID为lblName的Label):

lblName.Text="Hello"+PreviousPage.Name.Text+"<br />";

5.利用某些控件的PostBackUrl属性

示例:仍然是源页面WebForm1.aspx和目标页面WebForm2.aspx.

WebForm1.aspx中的部分代码:

<asp:Button ID="btnPostBack" Runat="server" Text="PBButton"></asp:Button>

<asp:TextBox ID="txtName" Runat="server" ></asp:TextBox>

<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>

WebForm2.aspx.cs中的部分代码:

protected void Page_Load(object Sender,System.EventArgs e)

{

TextBox txtName;

Calendar calendar1;

txtName=(TextBox)PreviousPage.FindControl("txtName");

calendar1=(Calendar)PreviousPage.FindControl("Calendar1");

Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();

}

使用这种方法存在一个问题:如果在没有单击那个按钮之前,也就是未处理WebForm1.aspx之前,有人请求了WebForm2.aspx,该怎么办?这就需要在WebForm2.aspx中的代码处理之前加一个判断.使用IsCrossPagePostBack属性,这与IsPostBack属性很相似,它允许检查请求是否来自WebForm1.aspx.如下:

protected void Page_Load(object Sender,System.EventArgs e)

{

if(PreviousPage.IsCrossPagePostBack)

{

TextBox txtName;

Calendar calendar1;

txtName=(TextBox)PreviousPage.FindControl("txtName");

calendar1=(Calendar)PreviousPage.FindControl("Calendar1");

Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();

}

else

{

Response.Redirect("WebForm1.aspx");

}

}

6. 使用Cookie对象变量

  这个也是大家常使用的方法,与Session一样,是对每一个用户而言的,但是有个本质的区别,即Cookie是存放在客户端的,而session是存放在服务器端的。而且Cookie的使用要配合ASP.NET内置对象Request来使用。

a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e)

{

HttpCookie cookie_name = new HttpCookie("name");

cookie_name.Value = Label1.Text;

Reponse.AppendCookie(cookie_name);

Server.Transfer("b.aspx");

}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)

{

string name;

name = Request.Cookie["name"].Value.ToString();

}

7. 使用Application 对象变量

  Application对象的作用范围是整个全局,也就是说对所有用户都有效。其常用的方法用Lock和UnLock。

a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e)

{

Application["name"] = Label1.Text;

Server.Transfer("b.aspx");

}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)

{

string name;

Application.Lock();

name = Application["name"].ToString();

Application.UnLock();

}

相关例子:

/article/6149111.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: