1。使用QueryString
使用QuerySting在页面间传递值已是一种很老的机制了,这种方法的主要优势是实现起来很是简单,然而它的缺点是传递的值是会显示在浏览器的地址栏上的(不安全),同时又不能传递对象,可是在传递的值少而安全性要求不高的状况下,这个方法仍是一个不错的方案。使用这种方法的步骤以下:
1,使用控件建立web表单(form)
2,建立能够返回表单的按钮和连接按钮
3,在按钮或连接按钮的单击事件里建立一个保存URL的字符变量
4,在保存的URL里添加QueryString参数
5,使用Response.Redirect重定向到上面保存的URL
下面的代码片段演示了如何实现这个方法:
源页面WebForm1.aspx.cs中的部分代码:web
- 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指令cookie
这个指令是.net 2.0中的一个新指令,用于处理ASP.NET 2.0提供的跨页面传送新功能.用于批定跨页面的传送过程起始于哪一个页面.包含两个属性:session
TypeName:设置回送时的派生类名ide
VirtualPath:设置回送时所传送页面的地址.网站
以下示例:this
- 源页面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属性url
示例:仍然是源页面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();
- }
转载出处:http://blog.csdn.net/MicrosoftCenterOfHN/archive/2009/05/12/4170404.aspx