ASP.NET跨页面传值技巧总结

news/2024/5/19 9:01:21
注:
转贴自千山暮雪: http://fan2k.itpub.net/ 以及:
关于页面传值的方法,引发了很多讨论。看来有很多人关注这个,我就我个人观点做了些总结,希望对大家有所帮助。

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. 使用Session变量

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

a.aspx的C#代码

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

b.aspx中C#代码

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

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

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;
name = newWeb.Name;
}
补充1
Server.Transfer的方法(VB)
源页:
Public Property test()
        Get
            Return "OKfe"
        End Get
        Set(ByVal value)
        End Set
    End Property
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Server.Transfer("BB.aspx", True)
    End Sub
目标页:
Partial Class BB
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.Context.Handler Is sender Then
            Dim P As Object = Me.Context.Handler
            Response.Write(P.test)
        End If
    End Sub
 End Class
6 asp.net刷新父页面时不出现“重试”对话框  
注:转贴自http://www.cnblogs.com/tanshiyang82/archive/2007/05/29/494976.html
以前常用opener.location.reload()来刷新父页面,但会出现“重试”对话框,用opener.location=opener.location不会出现重试对话框,但在有些场合行不通(用过的人都知道)。
偶然想到了.net 的_doPostBack
下面把方法告诉大家:
在父页面中加一个按钮,在该按钮的事件上写上绑定数据的代码(其实大多数父页面都会有搜索按钮之类的),设该按钮的名称为btnSearch。
设在弹出窗体中有一个按钮为btnSave,在该按钮的代码最后加上Response.Write("<script>window.opener.__doPostBack('btnSearch。','');window.close()</script>");

就这样,搞定。
:)
注:该方法不一定在任何场合有效,根据你的实际情况,这仅仅起个抛砖引玉的作用。

http://www.niftyadmin.cn/n/3970911.html

相关文章

血液本体:血液学领域的本体(The Blood Ontology-An Ontology in the Domain of Hematology)

说明&#xff1a;由于我近期工作涉及生物本体&#xff0c;然而国内有关本体的时新文章较少&#xff0c;因此翻译ICBO2011会议的部分论文&#xff0c;与本体领域的研究者和爱好者分享。本人翻译水平有限&#xff0c;如果大家英文水平不错&#xff0c;还是推荐直接看原文。 摘要&…

实用网页技术(javascript+CSS)

注&#xff1a;这些示例都兼容IE,FF 1 超酷ToolTip 2 导航条 二级CSS横向 CSS横向下拉 竖向三级弹出 横向三级弹出 横向三级弹出(向上) 3 滚动图片 4 对联广告 5 类新浪多新闻分类 6 仿GOOGLE IG 拖拽页块

一句代码杀死IE6

<!--[if IE 6]><script src"http://letskillie6.googlecode.com/svn/trunk/letskillie6.zh_CN.pack.js"></script><![endif]-->

c#接收电子邮件类

using System; using System.Net.Sockets; using System.Net; using System.Security.Cryptography; using System.IO; / // 类名&#xff1a;Pop3 // // 功能&#xff1a;接收电子邮件 // …

asp.net 打包成安装程序

1. 的.net&#xff0c;用鼠标右击你的工程(解决方案)&#xff0c;选择add new project(添加新建项目)。2.选择setup and deployment projects&#xff08;安装和部署项目&#xff09;的 web setup project(web安装项目)。&#xff08;注意setupproject的存放路径。通常默认&…

“您未被授权查看该页,您不具备使用所提供的凭据查看该目录或

“您未被授权查看该页,您不具备使用所提供的凭据查看该目录或页的权限” -- 解决办法在配置IIS的时候&#xff0c;如果安全稍微做的好一些。就会出现各式各样的问题。比如&#xff0c;常见的访问网页会弹出用户名密码的登陆界面&#xff0c;或者是访问某种页面比如html,asp没事…

验证码图片生成

下面是Vb的 gif.aspx ---------------------------- <% import namespace"system"%> <% import namespace"system.io"%> <% import namespace"system.drawing"%> <% import namespa…