Use Session variable in html

I get a session variable from a login form and then redirect to another page:

    String a = Login1.UserName;
    Session["user"] = a;
    Response.Redirect("~/Home.aspx");

In Home.aspx I wish to display in a div something like ” Hello -Session["user"] ” . How can I get the session var and use it in the html code?

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

Use a code block:

Hello <%:Session["user"]%>

Or if before .NET 4.0:

Hello <%=Server.HtmlEncode(Session["user"])%>

Method 2

I was actually trying a lot to get my session value from a page to another html in asp.net.

However some syntactical error was always acting as an hindrance.

In order to solved it, I’ve created a web page and there I initiated the session in the code behind and directed that to the other page.

Note, this is a .html page, so now in the body I used

Hello <%=Session["UserID"]%>

instead of

Hello <%:Session["user"]%>

(which wasn’t working) and it fetched the value from my .cs to my .html file.


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