C# 从页面到页面传递会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19668500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing session variable from page to page
提问by Troy Bryant
I'm wondering what is my issue on passing a variable from page to page using asp.net session.
我想知道在使用 asp.net 会话从页面到页面传递变量时我的问题是什么。
I've stripped the code down to just one text box to see whats going on. I'm just trying to take the value of a text box and display it on a confirmation page. When the button is clicked it transfers me to the second page but there label is blank. Yes my post back url is pointing to the second page.
我已将代码精简为一个文本框以查看发生了什么。我只是想获取文本框的值并将其显示在确认页面上。单击按钮时,它会将我转移到第二页,但标签为空白。是的,我的回贴网址指向第二页。
Here is the button click:
这是按钮点击:
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session["name"] = name;
}
Here is the page load of the second page:
这是第二页的页面加载:
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = (string)(Session["name"]);
}
Unless I've been looking at this to long and missed something. I've already read "How to: Read Values from Session State"from MSDN.
除非我一直在看这个并且错过了一些东西。我已经从 MSDN 中阅读了“如何:从会话状态中读取值”。
采纳答案by dotnetcanuck
You say that you've set the PostBackUrl to your second page. If you're going to do it that way, you need to use Page.PreviousPage to get access to your textbox. But this is the easiest way:
您说您已将 PostBackUrl 设置为您的第二页。如果你打算这样做,你需要使用 Page.PreviousPage 来访问你的文本框。但这是最简单的方法:
Firstly, leave the PostBackUrl alone. Setting the PostBackUrl to your second page means that you're telling the SECOND PAGE to handle your button click, not the first page. Hence, your session variable never gets set, and is null when you try to pull it.
首先,不要管 PostBackUrl。将 PostBackUrl 设置为您的第二页意味着您告诉 SECOND PAGE 处理您的按钮点击,而不是第一页。因此,您的会话变量永远不会被设置,并且在您尝试拉动它时为空。
This should work for ya.
这应该对你有用。
And yes, you can also do this with a QueryString, but if its something that you don't want the user to see/edit, then a Session variable is better.
是的,您也可以使用 QueryString 执行此操作,但是如果您不希望用户看到/编辑它,那么 Session 变量会更好。
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session["name"] = name;
Response.Redirect("PageTwo.aspx");
}
Then in the second page (You don't REALLY need the ToString()):
然后在第二页(你真的不需要 ToString()):
protected void Page_Load(object sender, EventArgs e)
{
if (Session["name"] != null)
{
lblName.Text = Session["name"].ToString();
}
}
EDIT-- Make sure that your button click actually gets fired. Someone can correct me wrong on this, as I do most of my work in VB.NET, not C#. But if you don't specify the OnClick value, your function won't get called.
编辑- 确保您的按钮点击实际上被触发。有人可以纠正我的错误,因为我的大部分工作都是在 VB.NET 中完成的,而不是 C#。但是,如果您不指定 OnClick 值,则不会调用您的函数。
<asp:Button ID="Button1" runat="server" Text="Click Me!" OnClick="submit_Click" />
回答by slugster
The code you have posted looks fine, so your problem is probably with setup.
您发布的代码看起来不错,所以您的问题可能与设置有关。
Check this link ASP.NET Session State Overviewand pay particular attention to the sections on Cookieless SessionIDsand Configuring Session State.
检查此链接ASP.NET 会话状态概述,并特别注意有关Cookieless SessionID和配置会话状态的部分。
回答by Humpy
I don't think you added the session. This is how I have done mine.
我认为您没有添加会话。这就是我如何完成我的。
First Page
第一页
protected void btnView_Click(object sender, EventArgs e)
{
foreach (ListItem li in lbxCheckDates.Items)
{
if (li.Selected == true)
{
lblMessage.Text = "";
string checkDate = lbxCheckDates.SelectedItem.Text;
Session.Add("CheckDates", checkDate);
Page.ClientScript.RegisterStartupScript(
this.GetType(), "OpenWindow", "window.open('Paystub.aspx','_newtab');", true);
}
}
}
Second Page
第二页
protected void Page_Load(object sender, EventArgs e)
{
string checkDate = (string)(Session["CheckDates"]);
//I use checkDate in sql to populate a report viewer
}
So with yours, I think you need..
所以对于你的,我认为你需要..
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session.Add("Name", name);
}
I think what you have in the second page should work, but if it doesn't, add ToString() to it like..
我认为您在第二页中的内容应该有效,但如果无效,请将 ToString() 添加到其中,例如..
lblName.Text = (string)(Session["name"]).ToString();
Let me know if this helps!
让我知道这是否有帮助!
回答by Gary Walker
Are you doing a redirect after setting the session variable on the first page, if so you it will not work correctly (unless you know the trick). Checkout this articleon making it work. Basically, the way to make this work is to the overload redirect method.
在第一页上设置会话变量后,您是否在进行重定向,如果是这样,您将无法正常工作(除非您知道诀窍)。查看这篇文章,让它工作。基本上,使这项工作的方法是重载重定向方法。
Response.Redirect("~/newpage.aspx", false);
The false parameter prevents .net from terminate processing on the existing page (that actually writes out the session state)
false 参数阻止 .net 终止对现有页面的处理(实际上写出会话状态)
回答by Deepak Kumar
For first page, use this:
对于第一页,请使用:
protected void Button1_Click(object sender, EventArgs e)
{
string value = TextBox1.Text;
Session["value"] = value;
Response.Redirect("~/Sessionpage.aspx");
}
回答by Deepak Kumar
For Second Page
对于第二页
protected void Page_Load(object sender, EventArgs e)
{
if (Session["value"] != null)
{
Label1.Text = Session["value"].ToString();
}
else
{
Label1.Text = "Sorry,Please enter the value ";
}
}
回答by Amna Riaz
You can use Server.Transfer()
instead of Response.Redirect()
您可以使用Server.Transfer()
代替Response.Redirect()