how to display a session value in an ASP textbox

A basic question but there is no such question on Stack Overflow (for 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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

.cs file

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

.aspx

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

Method 2

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

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)

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x