C# 如何从 ASP.NET 4.0 中的会话获取值并将其存储到文本框/标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14656699/
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
How to get a value from a session in ASP.NET 4.0 and store it to a textbox/ label
提问by vasilev21
i must to a project for school ,i must build an asp.net (4) site and i need to get a value from a session variable and to store it in an textbox or label what ever. I've tried with textbox until now........and i got stuck.
我必须为学校项目,我必须建立一个 asp.net (4) 站点,我需要从会话变量中获取一个值并将其存储在文本框或标签中。到目前为止,我一直在尝试使用文本框......但我被卡住了。
My session variable declare on page Default.aspx:
我的会话变量在页面 Default.aspx 上声明:
Session["user"] = username;
My textbox code on Home.aspx page :
我在 Home.aspx 页面上的文本框代码:
<div class="content"><!--D2-->
<form id="form1" runat="server">
<div class="logged"><!--D3-->
<p class="i_am">You are logged in as:<asp:TextBox ID="txt_loggedas" runat="server" ></asp:TextBox>
</p>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LogOut">[Log Out]</asp:LinkButton>
I tried answers from this link: how to display a session value in an ASP textbox, but i didn't succeed x-(
我尝试了这个链接的答案:如何在 ASP 文本框中显示会话值,但我没有成功 x-(
采纳答案by Ricardo Souza
On your Default.aspx.cs file, you can get the value of the session like this:
在您的 Default.aspx.cs 文件中,您可以像这样获取会话的值:
txt_loggedas.Text = Session["user"].ToString();