Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have one problem,i need to pass value from aspx page to html page using session.I don't want pass value using url query string,

For example,i have login.aspx page and Welcome.html,in this if use login i need to pass username to welcome page.When login time can store in session or public variable and in welcome page retrieve and show in welcome page input box.

Note:I don't want url query string like Welcome.html?Username=aaaa

Pls reply asap

Regards
Aravind
Posted

Want to get value of session variable in html page[^]
Quote:
As a HTML page is served as is, with no code running on the server, it cannot access the session. You'd need to make it an ASP.NET page, or a PHP page, or whatever other framework your other pages are running in. Then you can access the Session, the same as with your other pages.
 
Share this answer
 
Add the value you want to pass to the HTML page in cookies and access it using client side script
 
Share this answer
 
Comments
Aravindba 2-Oct-14 1:05am    
How to pass value from aspx to html page using cookies ?
How to pass value from aspx page and retrieve in html page
This is how you add cookies to response using ASP.NET C#

HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);


And read the cookie using Javascript as below
C#
    var ca = document.cookie.split(';');
    //Access value of font and color by using ca[0] and ca[1] respectively
}
 
Share this answer
 
Comments
Aravindba 2-Oct-14 1:31am    
Hi thank u for ur reply,i need each time i will pass different values,so each time need new values.what above above code,i till appeand new vlaue with old cookies.so i need to pass new cookies each time,Can we use like session variable,bcz if pass session value form one page it will pass new value only.
Aravindba 7-Oct-14 3:47am    
Hi Actually we dont know when user logout,so need to maintain cookies upto user logout.once logout need to clear,but in ur code need to mention cookie clear time also.
Rohit Kukreti 8-Oct-14 4:28am    
Hi, Just saw your comment. When you say you want cookie clear time, do you mean cookie expiration, Which means once the time elapses, cookie would be cleared and user needs to,login again. I have already entered the expiry logic
myCookie.Expires = DateTime.Now.AddDays(1d);
Aravindba 8-Oct-14 21:20pm    
Thank u for ur reply,for example i explain here login and html page,but what actually we need after login user click file name form grid view(this grid view have lot of rows more than 100),so based on grid view file name click i need to store that file name in session and pass to html page.user can click more than one file name after login,so i need to pass latest file name to session and pass to html page.
Note:i am not pass file name quickly,may it take 2 mins or more than 2 mins,then how here give cookies expire time ?
Rohit Kukreti 9-Oct-14 9:50am    
If you look at the code, I have already given an expiry of 1 day. So you can set it the same way to whatever you want
myCookie.Expires = DateTime.Now.AddDays(1d);
You can get the session value in html page using ajax call i will give one example try like this you will get good result

In Default.aspx page write like this under button click event am storing session value

C#
protected void btnSession_Click(object sender, EventArgs e)
      {
          Session["Getvalue"] = "Test html";
          Response.Redirect("~/HTMLPage1.htm");
      }


and i wrote one Web Method in Default.aspx page like
C#
[WebMethod]
 public static string GetSessionValue()
 {
    return  HttpContext.Current.Session["Getvalue"].ToString();
 }


Next in html page write an ajax method in document.ready method

$(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "../Default.aspx/GetSessionValue",
            data: "",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                $('#sessionvalue').html(msg.d);
            },
            async: false,
            error: function (msg) {                
                    AlertDialog("Failed to load session value.");
            }
        });
    });


for displaying session am using Htmlpage in that am taking div tag


Here in html page you will get session value
 
Share this answer
 
Comments
Aravindba 2-Oct-14 3:49am    
Hi thank u for ur reply,but i can get successful result.Pls check my code
This is Default2.aspx page load event.
Session("print") = False

This is in Default2.aspx webmethod
<webmethod()>
Public Function GetSessionValue() As String
Return HttpContext.Current.Session("print").ToString()
End Function

And this is in html page
$(document).ready(function () {
$.ajax({
type: "POST",
url: "viewe.aspx/GetSessionValue",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
},
async: false,
error: function (msg) {
alert(msg);
//AlertDialog("Failed to load session value.");
}
});
});

Always go to error function.
Samatha Reddy G 2-Oct-14 4:51am    
Hey here you are not redirecting to html page
Samatha Reddy G 2-Oct-14 4:52am    
Where did you write response.redirect(~/abc.htm'); once you try as it is you will the solution
Aravindba 2-Oct-14 6:04am    
In Default2.aspx page i will use iframe,in that i call html page,so in Default2.aspx page load event i will pass html page to iframe.
This is code

Session("print") = False
viewframe.Attributes("src") = "viewe.html"
I am not sure
but have you tried this?

XML
<script type="text/javascript">
    var someSessionVariable = '@Session["SomeSessionVariable"]';
</script>


Get the variable in javascript variable and dynamically show it to the user on html page.
 
Share this answer
 
Comments
Aravindba 2-Oct-14 6:09am    
i will add in html page like this
var someSessionVariable = '@Session("print")';
alert(someSessionVariable);
in aspx page is Session("print") = False
when i pass aspx session to html page,in alert msg show as it is @Session("print")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900