`
yangyou230
  • 浏览: 1639668 次
文章分类
社区版块
存档分类

ASP.NET页面传值方式

 
阅读更多

ASP.NET页面传值方式

方式1:

在接收页 的html代码里加上一行: <%@ Reference Page = "WebForm1.aspx" %>

WebForm1 fp=(WebForm1)Context.Handler;
this.TextBox1.Text=fp.name; //name 是第一页的public变量


Context 提供对整个当前上下文(包括请求对象)的访问。您可以使用此类共享页之间的信息。

方式2:GET方式
在发送页
public int sum=0;

int i =int.Parse(this.TextBox1.Text)*2;

Server.Transfer("WebForm2.aspx?sum="+i);

接收页
this.TextBox1.Text=Request["sum"].ToString();
or this.TextBox1.Text=Request.Params["sum"].ToString();
this.TextBox1.Text=Request.QueryString["sum"];


方法3:全局变量

发送页:
Application["sum"]=this.TextBox1.Text;
Server.Transfer("WebForm2.aspx");

接收页:
this.TextBox1.Text=(string)Application["sum"];

Application实质上是整个虚拟目录中所有文件的集合,如果想在整个应用范围内使用某个变量值,Application对象将是最佳的选择



方法4:

发送页:
1.定义静态变量: public static string str="";
2. str=this.TextBox1.Text;
Server.Transfer("webform2.aspx");
接收页:
1.引入第一页的命名空间:using WebApplication1;
2 this.TextBox1.Text=WebForm1.str;

另外三种最常见的传值

ASP.NET WEB FORMS 给开发者提供了极好的事件驱动开发模式。然而这种简单的应用程序开发模式却给我们带来了一些小问题,举个例子,在传统的ASP应用程序中,你能够通过POST方法很容易的把一个值或多个值从一个页面传送到另一个页面(request()/request.form()/request.querystring()),用同样的方法在ASP.NET中实现有点麻烦。在这里,通过其他方式来解决这种情形。ASP.NET为我们提供了三种方式,一种是可以通过用QueryString来传送相应的值,再一种是通过session变量来传送相应的值,还有就是通过Server.Transfer方法来实现。

  一、使用Querystring

  Querystring是一种非常简单的传值方式,其缺点就是会把要传送的值显示在浏览器的地址栏中,并且在此方法中不能够传递对象。如果你想传递一个安全性不是那么太重要或者是一个简单的数值时,用此方法最好不过了。下面通过一个小例子来完成传值工作,步骤如下:
1、创建一个web form
2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2
3、为button按钮创建click事件
代码如下:
private void button_click(object sender,System.EventArgs e)
{
string url;
url="webform2.aspx?name="+TextBox1.Text + "&Email="+TextBox2.Text;
Response.Redirect(url);
}
4、新建一个目标页面命名为webform2
5、在webform2中放置两个Label1,Label2
在webform2的Page_Load中添加如下代码:
private void Page_Load(object sender,System.EventArgs e)
{
Label1.Text=Request.QueryString["name"];
Label2.Text=Request.QueryString["Email"];
}
运行,即可看到传递后的结果了。

二、使用Session变量

使用Session变量传值是一种最常见的方式了,此中方式不仅可以把值传递到下一个页面,还可以交叉传递到多个页面,直至把Session变量的值removed后,变量才会消失。举个例子看看:
1、创建一个web form
2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2
3、为button按钮创建click事件
代码如下:
private void button_click(object sender,System.EventArgs e)
{
Session["Name"]=TextBox1.Text;
Session["Email"]=TextBox2.Text;
Response.Redirect("webform2.aspx");
}
4、新建一个目标页面命名为webform2
5、在webform2中放置两个Label1,Label2
在webform2的Page_Load中添加如下代码:
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");
}
运行,即可看到传递后的结果了。

三、使用Server.Transfer

虽然这种方法有点复杂,但也不失为一种在页面传值的方式。
举个例子看看:
1、创建一个web form
2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2
3、为button按钮创建click事件
代码如下:
private void (object sender,System.EventArgs e)
{
Server.Transfer("webform2.aspx");
}
4、创建过程来返回TextBox1,TextBox2控件的值代码如下:
public string Name
{
get {return TextBox1.Text;}
}
public string Email
{
get{return TextBox2.Text;}
}
5、新建一个目标页面命名为webform2
6、在webform2中放置两个Label1,Label2
在webform2的Page_Load中添加如下代码:
private void Page_load(object sender,System.EventArgs e)
{
//创建webform的实例
webform1 wf1;
//获得实例化的句柄
wf1=(webform1)Context.Handler;
Label1.Text=wf1.Name;
Label2.Text=wf1.Email;
}

常见的就是这三种方法。

上面方法一中的传值问题

问:在asp中页面间传值使用url?方式 例如 "index.asp?aaa=华东五市+水乡六日游”
因为字符串“华东五市+水乡六日游”中间有加号,与asp系统字符串连接符关键字冲突,传过去的变量aaa的值就会变成“华东五市”,请问大家有什么解决办法?
解决:

1.server.urlencode("华东五市+水乡六日游")

2.URLEncode
The URLEncode method applies URL encoding rules, including escape characters, to a specified string.

Syntax
Server.URLEncode( string )

Parameters
string
Specifies the string to encode.
Example
The following script

<%Response.Write(Server.URLEncode("http://www.microsoft.com")) %>

produces the output

http%3A%2F%2Fwww%2Emicrosoft%2Ecom

3.index.asp?aaa=华东五市%2B水乡六日游

4.自己定义一些特殊字符对应 “+”,“*”,“#”
例如,Replace(string,"+","CODE01")

然后到另一个页面在用Replace(string,"CODE01","+")换回来即可了 自己定义一些特殊字符对应 “+”,“*”,“#”
例如,Replace(string,"+","CODE01")

然后到另一个页面在用Replace(string,"CODE01","+")换回来即可了

在页面间传递引用

有不少文章讨论怎么在两个页面间传递引用,如果你页面上的对象设置为public的话,你就可以传递他们在页面间。

在页面间传递是使用
Server.Transfer替代Response.Redirect就可以。
例子:

-------------In Page A codebehind:

public class PageA : System.Web.UI.Page
{
public System.Web.UI.WebControls.TextBox TextBox1;
public System.Web.UI.WebControls.Button Button1;

// standard page code (Page_Load, etc)
// ....
// ....

private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("b.aspx");
}
}


-------------In Page B codebehind:


private void Page_Load(object sender, System.EventArgs e)
{
PageA myAPage = Context.Handler as PageA;
string textboxFromPageA = myAPage.TextBox1.Text;
}

因为当使用Server.Transfer时所有的对象(A and B)在服务器上当时都是活动的,你可以引用任何东西。

稍微修改一下b.aspx(uestc95 提供):

Page myPage =(Page) Context.Handler;
string textboxFromPageA;
textboxFromPageA = ((TextBox)myPage.FindControl("TextBox1")).Text;


这样在A.aspx中就可以正常的使用protected类型的了

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics