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?
String a = Login1. UserName; Session["user"] = a; Response. Redirect("~/Home. aspx");
By default, session variables last until the user closes the browser. So; Session variables hold information about one single user, and are available to all pages in one application. Tip: If you need a permanent storage, you may want to store the data in a database.
You can't access Session directly in JavaScript.
We can create the session by writing session_start() and destroy the session by using session_destroy(). You can access the session variable by writing $_session[“name”]. Let us understand how the session works from the following examples. Example 1: In the following, you can create the session by entering the name.
Use a code block:
Hello <%:Session["user"]%>
Or if before .NET 4.0:
Hello <%=Server.HtmlEncode(Session["user"])%>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With