科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道应用软件Web程序中网页间数据传递方法小结

Web程序中网页间数据传递方法小结

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文中,向你展示了几种从一个网页向另一个网页传递数值的几种方法

作者:子夜 来源:天极网 2007年10月17日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
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

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

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章