2011-11-08 39 views
3

我的情景,如何在两个或多个aspx页面之间传递会话变量?

我使用asp.net 2.0。我有网站,它创建一个唯一的ID,它被插入到数据库中,并显示在不可见的文本框中。现在我需要将此ID发送到下一页。我用过会话(“MemberId”)= Txtnewid.Text。它不工作当我分配给变量字符串时,它显示零值。请帮帮我 。在此先感谢

回答

3

您不需要将值存储在文本框中。所有你需要做的就是获取id并在第一次创建时在会话中插入它;在同一页面或网站中的其他任何后续请求,您可以通过访问这个ID:

string id = Session["MemberId"] as string; 

或者在VB语法:

dim id as String = Session("MemberId") 
2

假设C#的代码隐藏,设置会话变量如: -

 Session["MemberId"] = "MemberId"; 

拿回来进入下一个页面; -

if (Session["MemberId"] != null) 
    { 
    textBox1.Text = "Successfully retrieved " + (string)Session["MemberId"]; 
    } 

阅读有关ASP.NET Session State

2

有不同的方法可以将值从一个页面传输到另一个页面。最常见的方法是

  1. 会议
  2. 查询字符串

1.aspx.cs //第一页

Guid uniqueid = new Guid(); 

//Above code line will generate the unique id 

string s_uniqueid = uniqueid.ToString(); 

// Convert the guid into string format 

Session.Add("MemberId",s_uniqueid); 

// Store the string unique id string to session variable so far called MemberId 

2.asp.cs //第二页

string s_MemberId = Session["MemberId"].ToString(); 
Now you can use this string member id for any other process. 

使用查询字符串,如果你正在使用asp.net AJAX开发应用的值从一个页面转移到另一个 ,那么你需要使用Response.Redirect方法还有Server.Transfer的

像 1.aspx。 CS //首页

Guid uniqueid = new Guid(); 

//Above code line will generate the unique id 

string s_uniqueid = uniqueid.ToString(); 

如果你愿意,s_uniqueid使用加密

Response.Redirect("2.aspx?uid=" +s_uniqueid+ ""); 

2.asp.cs //第二页

string ss_uniqueid = Request.QueryString["s_uniqueid"]; 

然后用另一个进程

相关问题