Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 436
Alex Avatar asked Dec 26 '10 11:12

Alex


People also ask

How do you pass a session variable in HTML?

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

How do you use session variables?

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.

Can I access session variables in JavaScript?

You can't access Session directly in JavaScript.

How do you register a variable in a session?

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.


2 Answers

Use a code block:

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

Or if before .NET 4.0:

Hello <%=Server.HtmlEncode(Session["user"])%>
like image 72
Oded Avatar answered Oct 03 '22 23:10

Oded


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.

like image 35
avinava basu Avatar answered Oct 04 '22 00:10

avinava basu