WebForm1 fp=(WebForm1)"/>

net(c#)跨页面传值方法总结_毕业设计aspnetc

其他工作总结 时间:2020-02-28 06:14:15 收藏本文下载本文
【www.daodoc.com - 其他工作总结】

net(c#)跨页面传值方法总结由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“毕业设计aspnetc”。

.net(c#)跨页面传值方法总结(2011-04-25 11:03:24)转载标签: it 方式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来传送相应的值,再一种是通过seion变量来传送相应的值,还有就是通过 Server.Transfer方法来实现。

一、使用Querystring

Querystring是一种非常简单的传值方式,其缺点就是会把要传送的值显示在浏览器的地址栏中,并且在此方法中不能够传递对象。如果你想传递一个安全性不是那么太重要或者是一个简单的数值时,用此方法最好不过了。下面通过一个小例子来完成传值工作,步骤如下:

1、创建一个web form2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox23、为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、新建一个目标页面命名为webform25、在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”];} 运行,即可看到传递后的结果了。

二、使用Seion变量

使用Seion变量传值是一种最常见的方式了,此中方式不仅可以把值传递到下一个页面,还可以交叉传递到多个页面,直至把Seion变量的值removed后,变量才会消失。举个例子看看:

1、创建一个web form2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox23、为button按钮创建click事件 代码如下:

private void button_click(object sender,System.EventArgs e){ Seion[“Name”]=TextBox1.Text;Seion[“Email”]=TextBox2.Text;Response.Redirect(“webform2.aspx”);}

4、新建一个目标页面命名为webform25、在webform2中放置两个Label1,Label2 在webform2的Page_Load中添加如下代码:

private void Page_Load(object sender,System.EventArgs e){ Label1.Text=Seion[“Name”].ToString();Label2.Text=Seion[“Email”].ToString();Seion.Remove(“Name”);Seion.Remove(“Email”);} 运行,即可看到传递后的结果了。

三、使用Server.Transfer

虽然这种方法有点复杂,但也不失为一种在页面传值的方式。举个例子看看:

1、创建一个web form2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox23、为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、新建一个目标页面命名为webform26、在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.daodoc.com”))%>

produces the output

http://www.daodoc.com

3.index.asp?aaa=华东五市+水乡六日游 4.自己定义一些特殊字符对应 “+”,“*”,“#” 例如,Replace(string,“+”,“CODE01”)

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

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

在页面间传递引用

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

在页面间传递是使用

Server.Transfer替代Response.Redirect就可以。例子:

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

public cla 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类型的了

###

1.使用QueryString变量

QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中。如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。但是对于传递数组或对象的话,就不能用这个方法了。下面是一个例子: a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e){ string s_url;s_url = “b.aspx?name=” + Label1.Text;Response.Redirect(s_url);}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e){ Label2.Text = Request.QueryString[“name”];}

2.使用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();}

3.使用Seion变量

想必这个肯定是大家使用中最常见的用法了,其操作与Application类似,作用于用户个人,所以,过量的存储会导致服务器内存资源的耗尽。a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e){ Seion[“name”] = Label.Text;}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e){ string name;name = Seion[“name”].ToString();}

4.使用Cookie对象变量

这个也是大家常使用的方法,与Seion一样,其是什对每一个用户而言的,但是有个本质的区别,即Cookie是存放在客户端的,而seion是存放在服务器端的。而且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();}

5.使用Server.Transfer方法

这个才可以说是面象对象开发所使用的方法,其使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方法是完全面象对象的,简洁有效。a.aspx的C#代码 public string Name { get{ return Label1.Text;} } private void Button1_Click(object sender, System.EventArgs e){ Server.Transfer(“b.aspx”);}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e){ a newWeb;//实例a窗体

newWeb =(source)Context.Handler;string name;

下载net(c#)跨页面传值方法总结word格式文档
下载net(c#)跨页面传值方法总结.doc
将本文档下载到自己电脑,方便修改和收藏。
点此处下载文档

文档为doc格式

    热门文章
      整站推荐
        点击下载本文