你好,欢迎访问远方教程PC版!
广告位招租
网站首页 >> ASP教程 >> ASP网络编程 >> 文章内容

ASP网络篇:4.8 Web程序中网页间数据传递方法小结(第2页)

[日期:2014-11-09]   来源:远方教程  作者:远方教程   阅读:3546次[字体: ] 访问[旧版]
 捐赠远方教程 

Session Variables

接下来我们看一看session变量,这些变量由服务器来处理。第一个响影一从客户端传递到服务器,Sessions就创建了,并且当用户关闭浏览器窗口或者一些异常操作发生,session就会结束。给你一些可以使用session变量来传递数值的方法。在下面你看到为用户创建的Session和 “Name”是关键字,也如知名的Session关键字一样,关键字被赋给TextBox数值。

// Session Created

Session["Name"] = txtName.Text;

Response.Redirect("WebForm5.aspx");

// The code below shows how to get the session value.

// This code must be placed in other page.

if(Session["Name"] != null)

Label3.Text = Session["Name"].ToString();

Application Variables

有时,我们需要存取来自网页任何地方的数值。因为那样,可以使用Application变量。这里有一小段代码,这段代码显示如何做到那些事情。一旦创建Application变量并且为其赋值,在应用程序的任何地方都可以重新得到它的值。

// This sets the value of the Application Variable

Application["Name"] = txtName.Text;

Response.Redirect("WebForm5.aspx");

// This is how we retrieve the value of the Application Variable

if( Application["Name"] != null )

Label3.Text = Application["Name"].ToString();

HttpContext

可以使用HttpContext从网页中重新得到数值。通过使用方法的属性获得那些数值。既然它们易于编写代码和修改,使用属性是一种好方法。在你的第一个网页中,制造一个属性,这个属性可以返回TextBox的值。

public string GetName

{

get { return txtName.Text; }

}

我们使用Server.Transfer来将此控件发送到一个新网页。注意:Server.Transfer仅仅将此控件传递到新的网页并且不重新定位该网页,这意味着你会看到在URL中旧网页的地址。简单地在“Server.Transfer”按钮单击事件,并且增加下列代码。

Server.Transfer("WebForm5.aspx");

现在,让我们定位网页,数值就传递到该网页上,在这种情况下使用的该网页是“webForm5.aspx”。

// You can declare this Globally or in any event you like

WebForm4 w;

// Gets the Page.Context which is Associated with this page

w = (WebForm4)Context.Handler;

// Assign the Label control with the property "GetName" which returns string

Label3.Text = w.GetName;

Special Note

特别注意 与你看到的一样,从一个网页向别一网页传递数值时有不同的方法。每一个方法有它自己的优点也有其缺点。所以,当你传递数值时,选择好你所需要的所以你就会有一种好方法,这种方法对你是最为可行的。

第1页 第2页
图片展示
 
相关评论
站长推荐