This question already has answers here:
Pass javascript variable to Codebehind
(2 answers)
Closed 7 years ago.
I'm developing a small app and I have a question about posting variables between WebPage.aspx and WebPage.aspx.cs .
I've got the variable from WebPage.aspx.cs to WebPage.aspx like this:
WebPage.aspx.cs code:
public string AdresKlienta = string.Empty;
public string AdresPizzerii = string.Empty;
WebPage.aspx code:
var origin2 = '<%= AdresPizzerii %>';
var destinationA = '<% = AdresKlienta %>';
On WebPage.aspx I use javascript function that contains variables that I use do some work and I create another variable that I would like to post back (WebPage.aspx -> WebPage.aspx.cs).
I've been searching on the Internet for the solution, but found nothing. I'll appreciate any help.
Posting back a variable simply involves putting it in a form element somewhere. So let's say you create a form element:
<input type="hidden" name="someElement" id="someElement" />
Then in your JavaScript code you would set the value of that element:
document.getElementById('someElement').value = someValue;
When the form containing that element posts to a server-side resource, that resource can then access that value:
var someValue = Request.Form["someElement"];
The point is that in order for server-side code to receive any values from client-side code, those values need to be included in a request to the server of some kind. Whether it's a form POST, a query string value on a GET request (from a redirect or clicking a link), an AJAX request in the background, etc. The client-side code needs to make a new request to the server-side code in order to send it that value.
You can use hidden field, using javascript you can enter a value to this field and can be accessed from your code behind also.
Related
I am trying to get strings that I have put in my session variables to another aspx webform. I am getting it inside my JS script and I would like to know how to go about it. I have tried the following but it doesnt seem to work. Please anyone guide me a bit.
Home.aspx.cs
HttpContext.Current.Session["InstitutionID"] = BankID;
HttpContext.Current.Session["InstitutionName"] = txtBankName.Text.Trim();
Index.aspx
$(document).ready(function () {
var param1var ='<%= Session["InstitutionID"]%>';
var param2var = '<%= Session["InstitutionName"]%>';
console.log('param1', param1var);
console.log('param1', param2var);
........
})
Are you using System.Web.SessionState.HttpSessionState.Session object to get the session object data in the Javascript? That is how I use it in one of my WebForms projects. Hope it helps.
HttpContext.Current.Session[Constants.SessionStateVariables.LoginFailed] = false;
var loginIsFailed = '<%=Session[XYZProcet.Common.Constants.SessionStateVariables.LoginFailed]%>';
This question already has an answer here:
How to set the .net Public Property value from Javascript?
(1 answer)
Closed 8 years ago.
At page load in code behind, the setting of session value working fine. But from the ajax call, it gives the error message as Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.
SessionData is Public Property SessionData(sessionVariableName As String) As String
Codebehind:
SessionData("UserRole") = "Supervisor"
I achieved by adding EnableSession:=True in the webmethod
am having Sample.aspx and sample.js both are in different directory.
how can i get the Sample.aspx page session values in sample.js file ?
i cant get the value for the following types
function session() {
alert('<%=Session.getAttribute("hdn_CheckedData")%>');
alert(document.getElementbyId("hdn_CheckedData").value);
alert('<%=Session["CheckedData"]%>');
alert('<%=Session("CheckedData")%>');
alert('<%=Session["CheckedData"].ToString()%>');
alert('<%=Session("CheckedData").ToString()%>');
};
CheckedData - is the session
hdn_CheckedData - is the hiddenfield
i tried both of it.
is it possible then help me pls.....
Hiddenfiled, session, viewstate or anything............
One simple solution is to declare the session variables just before the load of your javascript file and inside the aspx page. Eg on sample.aspx you have
<script>
var sessionCheckData = "<%=session["CheckedData"].ToString()%>";
</script>
<script type="text/javascript" src="sample.js"></script>
and on sample.js you have
function session() {
alert(sessionCheckData);
};
Similar answer: How to get asp.net client id at external javascript file
You can save the session data to a hidden field on your page. After that you have direct access to that field in you js file.
Or you can declare a session variable as Aristos has proposed. That would more straightforward actually.
I would write a generic handler and use JSONP to pass any data to external Javascript.
Please take a look at here, here and here.
Since it is not cross domain, JSON should also work.
You have to assign session values to hiddenfield
after that you can use that values
Hello guys I have this problem of getting lastGuestInsertedId from C# cookies in a code behind file to another web form that retrieve lastGuestInsertedId values by jquery and then show the value by alerting. Any ideas?
C# codes
HttpCookie GuestCookie = new HttpCookie("GuestCookie");
GuestCookie["Key"] = Convert.ToString(lastGuestInsertedId) ;
GuestCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(GuestCookie);
JQuery codes
var guestkey = $.cookie("GuestCookie")
alert(guestkey);
$.cookie is not part of default jQuery. You need some jQuery plugin to do that.
To check if you actually have cookies on client side - alert(document.cookie).
I already found this on this website.
how to pass session variable in href
But what I want is vice versa. and I can't get it done..
I need some correct syntax..
is it ......
..../home.aspx?<%Session["egSession"]=1%>
or
..../home.aspx?=<%Session["egSession"]=1%>
or
..../home.aspx?<%=Session["egSession"]=1%>
But i believe all of the above are wrong.. coz none of them are working..
Tkz..
Session.Add("egSession", 1) will add 1 to the session cookie egSession.
You could also probably be sure it doesn't already exist by doing so:
Session.Remove("egSession");
Session.Add("egSession", 1);
To get the querystring value from the address you would (code behind do)
var value = Request["egSession"];
So that means you could do:
Session.Remove("egSession");
Session.Add("egSession", Request["egSession"]);
Hope that helps!
** UPDATE **
If you can't touch the .cs files, you can do this in the ASPX-file, by wrapping your code in <% ... code goes here ... %>
if the new session variable value is know, e.g. 1 in your sample setting it can be done anywhere
<% Session["egSession"]=1; %>
if you want to pass it through as a query parameter do this:
..../home.aspx?egSession=<%=Session["egSession"]%>
The point is, you need a name for the value, i.e. egSession but you may call it what ever you want.
However, if you alldready know the value you can simply do:
..../home.aspx?egSession=1
From what I am understanding you want something like this:
APage
Take Me home
Home.aspx.cs: the code behind page, in say the OnPageLoad Event
Session["egSession"] = Request.QueryString["egSession"];
Home.aspx
<div>Session: <% =Session["egSession"] %></div>
<div>Query String: <% = Request.QueryString["egSession"] %></div>
If you are trying to to it all in one I would try the following:
APage.aspx.cs
Create a public method (change the type of the input parameter if needed)
public string SessionMagic(object input)
{
Session["egSession"] = input;
return Session["egSession"].ToString();
}
APage.aspx
A Link
*UPDATE: *
If you can not update the .cs files you can add server side code in the aspx page, not great practice but it can be done. Encapsulate the code in script tags with the run a server attribute set. E.g.:
<script runat="server">
public string ServerSideFunction(string input)
{
Session["egSession"] = Request.QueryString["egSession"];
public string SessionMagic(object input)
{
Session["egSession"] = input;
return Session["egSession"].ToString();
}
}
</script>
you can use javascript with cookie to store the value that you want to set into Session.
eg:- url- url.apsx/egValue=1 read this url using javascript. Put the key and value into cookie.
Then you can read cookie value from server and put it into session.
hope it helps.