How to display session value in ASP text box

The main question, but there is no such question in Stack Overflow (for ASP.NET)

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

I did it a week ago, now something is wrong. It should be easy. I also tried <% =%>, it didn't work either. Placing a single quote around "<%%>" gives a binding error. Help

+6
source share
3 answers

What I did was pulled out a text box in C # code and set its text value for the session. Example:

 txtUserName.text = Session["UserName"]; 

Use it in one of the functions that checks the session values ​​or you can use the page_load function (the default function for each page)

+5
source

I usually hide implementation details from aspx code with a property:

.cs file

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

.aspx

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

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

I always forget the syntax for the embedded code, this information may be useful, I think.

+1
source

Source: https://habr.com/ru/post/893354/


All Articles