I am able to store the session variable in aspx page using the following way :
$(document).ready(function () {
var userName = "webruster";
'<%Session["UserName"] = "' + userName + '"; %>';
alert('<%=Session["UserName"]%>');
});
now when i am trying to retrieve the Session["UserName"]
i am unable to get that value in cs . For this i have a work around but want to know the reason why it is failing ?
Alternative way :
Declaring hidden Variable and Link button
$(document).ready(function () {
var userName = "webruster";
'<%Session["UserName"] = "' + userName + '"; %>';
var x = document.getElementById("<%=hdnsessionvalue.ClientID %>");
x.value = '<%=Session["UserName"] %>';
document.getElementById('<%= lnkButton1.ClientID %>').click();
});
So i am able to retrieve the value in onclick
event in server side.
My question : So why i am unable to retrieve the session value in cs using the first method (i.e without assigning to hidden variable)
I am able to store the session variable in aspx page using the following way :
$(document).ready(function () {
var userName = "webruster";
'<%Session["UserName"] = "' + userName + '"; %>';
alert('<%=Session["UserName"]%>');
});
now when i am trying to retrieve the Session["UserName"]
i am unable to get that value in cs . For this i have a work around but want to know the reason why it is failing ?
Alternative way :
Declaring hidden Variable and Link button
$(document).ready(function () {
var userName = "webruster";
'<%Session["UserName"] = "' + userName + '"; %>';
var x = document.getElementById("<%=hdnsessionvalue.ClientID %>");
x.value = '<%=Session["UserName"] %>';
document.getElementById('<%= lnkButton1.ClientID %>').click();
});
So i am able to retrieve the value in onclick
event in server side.
My question : So why i am unable to retrieve the session value in cs using the first method (i.e without assigning to hidden variable)
Share Improve this question asked Aug 27, 2015 at 5:13 Tummala Krishna KishoreTummala Krishna Kishore 8,2714 gold badges34 silver badges51 bronze badges 5- @Sac you mean on pastbacking ,its clearing the session value which was assigned ? – Tummala Krishna Kishore Commented Aug 27, 2015 at 5:18
- you can't set Session on client side. it is Server side State Management Technique.. You can set HiddenField Value and can get on Server Side .. codeproject./Questions/341573 – Sachin Commented Aug 27, 2015 at 5:19
-
@Sac but in alert using
alert('<%=Session["UserName"]%>')
i am able to see the session value .why am i able to see it on client side ? – Tummala Krishna Kishore Commented Aug 27, 2015 at 5:21 - @sac No i able to get the value in all browsers – Tummala Krishna Kishore Commented Aug 27, 2015 at 5:38
- Let us continue this discussion in chat. – Sachin Commented Aug 27, 2015 at 5:39
2 Answers
Reset to default 5If you mean 'client side java script' then you can't, at least not directly. The session data is stored on the server and client side doesn't see it without municating with server.
To access it can make an HTTP request and have server side modify or return the data.
Updated
Example
<script>
// get the variable
var data = JSON.Stringify(yourVariable);
// Make the ajax call
$.ajax({
type: "POST",
url: "aspPage.aspx/Method", //method we will call
contentType: "application/json; charset=utf-8",
data: {value: data }, //assign the 'data' values to 'value', its the data you want to send
dataType: "json",
success: function (result) {
alert('its working');
},
error: function (result) {
alert('something wrong');
}
});
</script>
on aspPage.aspx
[WebMethod]
public static void Method(string value)
{
sting val = value; // Do whatever you want to
}
You can not set or use Session from javascript directly as it is a Server Side State Management Technique. You should use Hidden Field for that purpose.
Set javascript variable to Hidden Field and in code behind, get the Hidden Field value and set it to your desired Session.
You can achieve it by:
Java Script
$(document).ready(function () {
var userName = "webruster";
var x = document.getElementById("<%=hdnsessionvalue.ClientID %>");
x.value = userName;
document.getElementById('<%= lnkButton1.ClientID %>').click();
});
Code Behind (CS)
protected void lnkButton1_Click(object sender, EventArgs e)
{
string test = hdnsessionvalue.Value;
Session["UserName"] = test ;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744257630a4565475.html
评论列表(0条)