如何使用ASP.NET中的会话在多个页面上检索值 [英] How to retrieve a value at multiple pages using session in ASP.NET

查看:63
本文介绍了如何使用ASP.NET中的会话在多个页面上检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用session方法在我的asp.net应用程序中的另一个页面检索字符串值。但我想在多个页面检索该值。我的意思是将有一个源(从我传递字符串的地方)和多个目的地(我将检索我的字符串值)。我怎么能这样做?请帮帮我。



我的尝试:



//在源页面

Session [Name] = TextBox1.Text;

Response.Redirect(4page1_index.aspx);



//在目的地页面



if(Session [Name]!= null)

{

s = Session [Name]。ToString();

Response.Write(s);

}



//但在这种情况下只有一个目的地,而我想将该字符串(在这种情况下从text1.text)显示到多个页面。

I am using session method to retrieve a string value at another page in my asp.net application. But i want to retrieve that value at multiple pages. I mean there will be one source (from where i am passing a string) and multiple destinations (where i will retrieve my string value). How i can do that? Please help me.

What I have tried:

//at source page
Session["Name"] = TextBox1.Text;
Response.Redirect("4page1_index.aspx");

//at destination page

if (Session["Name"] != null)
{
s = Session["Name"].ToString();
Response.Write(s);
}

//but in that case there is just one destination, while i want to show that string(from text1.text in that case) to multiple pages.

推荐答案

只需将代码复制并粘贴到需要的位置,或者将其转换为用户控件,只需将用户控件放在要输出的页面上即可。



如果你问如何在不修改这些页面的情况下将其添加到其他页面那么那就更麻烦了值得一提的是,你需要实现一个自定义响应过滤器,但如果你想这样做是因为你不能打扰更新你需要的页面就是错误的解决方案。
Simply copy and paste that code where it needs to go, or you could make it into a user control and simply place the user control on pages where you want the output.

If you're asking how you can add this to other pages without amending those pages then that would be more bother than it's worth really, you'd need to implement a custom response filter, but if you want to do that because you can't be bothered updating the pages you need this on that is the wrong solution.


存储在会话中的值,可以在整个应用程序中检索。



如果在第一页中存储了会话值。

例如:会话[KeyName] =AnyValue;



可以在应用程序的每个页面中检索该会话。



第二页:



String s =;

if(Session [KeyName ]!= null)

{

s = Session [KeyName]。ToString();

Response.Write(s);

}





第3页:



String s =;

if(Session [KeyName]!= null)

{

s = Session [ KeyName]。ToString();

Response.Write(s);

}



同如会话价值c被检索。
The value which stores in session and can be retrieved through out the application.

If in first page you store a value in session.
Ex: Session["KeyName"]="AnyValue";

That session can be retrieved in every page in the application.

In 2nd page:

String s="";
if (Session["KeyName"] != null)
{
s = Session["KeyName"].ToString();
Response.Write(s);
}


In 3rd page:

String s="";
if (Session["KeyName"] != null)
{
s = Session["KeyName"].ToString();
Response.Write(s);
}

Same as Session value can be retrieved.


这篇关于如何使用ASP.NET中的会话在多个页面上检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆