如何在ASP文本框中显示会话值

时间:2021-06-25 01:48:39

A basic question but there is no such question on Stack Overflow (for ASP.NET)

一个基本问题,但Stack Overflow上没有这样的问题(对于ASP.NET)

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

I did this a week ago, now something is wrong. It should be simple. I also tried <%= %>, it did not work either. Putting a single quote around '<% %>' gives binding error. Help

我一周前做过这个,现在出了点问题。应该很简单。我也试过<%=%>,它也没用。在“<%%>”周围添加单引号会产生绑定错误。救命

3 个解决方案

#1


8  

I normally hide the implementation details from the aspx code with a property:

我通常使用属性隐藏aspx代码中的实现细节:

.cs file

public string UserName { get { return Session["UserName"]; } }

.aspx

<asp:TextBox ID="txtUserName" runat="server" Text='<%= UserName %>' >

#2


5  

What I did was pulled the text box in C# code and set it text value to the session. Example:

我所做的是在C#代码中拉出文本框并将其文本值设置为会话。例:

txtUserName.text = Session["UserName"];

Use it in one of the function which checks the session values or you can use in page_load function (the default function for every page)

在检查会话值的函数之一中使用它,或者您可以在page_load函数中使用它(每个页面的默认函数)

#3


1  

Now I think your code should look like <asp:TextBox ID="txtUserName" runat="server" Text='<%# Session["UserName"] %>' >

现在我认为您的代码应该看起来像 ">

I always forget the sintax for inline code, this information could be helpfull I think.

我总是忘记内联代码的sintax,我认为这些信息可能会有所帮助。

#1


8  

I normally hide the implementation details from the aspx code with a property:

我通常使用属性隐藏aspx代码中的实现细节:

.cs file

public string UserName { get { return Session["UserName"]; } }

.aspx

<asp:TextBox ID="txtUserName" runat="server" Text='<%= UserName %>' >

#2


5  

What I did was pulled the text box in C# code and set it text value to the session. Example:

我所做的是在C#代码中拉出文本框并将其文本值设置为会话。例:

txtUserName.text = Session["UserName"];

Use it in one of the function which checks the session values or you can use in page_load function (the default function for every page)

在检查会话值的函数之一中使用它,或者您可以在page_load函数中使用它(每个页面的默认函数)

#3


1  

Now I think your code should look like <asp:TextBox ID="txtUserName" runat="server" Text='<%# Session["UserName"] %>' >

现在我认为您的代码应该看起来像 ">

I always forget the sintax for inline code, this information could be helpfull I think.

我总是忘记内联代码的sintax,我认为这些信息可能会有所帮助。