在 ASP.Net 中访问 C# 会话变量

Accessing C# session varible in ASP.Net

我正在尝试访问 aspx 中的会话变量,但我认为我的语法某处存在问题。

页面加载不足:

Session["UserName"]= username.Substring(8).ToString();  // This is ok as far as I know. 

问题出在 asp 位:

<asp:TextBox ID="ATextBox" runat="server" Text="<% Session["UserName"] %>" /> 
<asp:TextBox ID="ATextBox" runat="server" Text="<%# Session["UserName"] %>" /> 

我也尝试过上面的方法(一次一个),最后也是 .ToString()。但我不断收到错误消息:

Either The server tag is not well formed or the server tag contains % %

可能会有所不同的一件事是 TextboxGridView 内(它在 ContentTemplate 内)但不是边界域。

试试这个:

<asp:TextBox ID="ATextBox" runat="server" Text='<%# Session["UserName"] %>' /> 

不确定是否有帮助..