I want to set session variable using javascript/jquery in my apsx page.
Afterwards,I want to use that session variable in other page using jquery.
Can anyone help me for this?
Thanks,
Priya
You cannot set directly with javascript you need server side code for it. you can make an ajax call and pass parameter and set session there.
You can set the session variable value in hidden field when page is initially loaded like this:
<asp:HiddenField ID="HDSessionValue" runat="server" Value'<%#Session["CustomerID"]'/>
Now you can easily get the value from jquery or javascript like this:
$(document).ready(function(){
var SessionValue=$('#HDSessionValue').val();
alert(SessionValue);
});
You have two cases here ,
1) If you want to set the value of Session in client side on the page load it self you can use the hidden field to pass on the client side .
2) If you want to set the value on the client side without going to back to post back , you can opt for the AJAX method
you can assign the value of session variable to JS variable but to assign a value to session variable you need something like ajax working on server side
Related
I have a variable which is updated on every page shift, but I want to store the value in the first call for good somehow.
The variable is e.g
$sizeOfSearch = $value['HotelList']['#activePropertyCount'];
First time the page loads it's 933, on next page the same value is retrieved but it's now different e.g 845. This goes on page for page.
What I want is to store 933 for good. So I can show this number on every page.
Can I somehow store the first time this value is retrieved ? (I get the value via REST request)
Sessions maybe or ?
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers. These will either be a built-in save handler provided by default or by PHP extensions (such as SQLite or Memcached); or can be custom handler as defined by session_set_save_handler(). The read callback will retrieve any existing session data (stored in a special serialized format) and will be unserialized and used to automatically populate the $_SESSION superglobal when the read callback returns the saved session data back to PHP session handling.
So, on every page make sure to start it with:
<?php
session_start();
Then, you set the value like this:
if(!isset($_SESSION['name'])) {
$_SESSION['name'] = $sizeOfSearch;
}
Whenever you need the retrieve the value use this:
print $_SESSION['name'];
This session will keep store the variable as long as you don't destroy it. Code for destroying a session:
session_destroy();
I am trying to create Javascript Session variables that will read information from one form and pass these variables to another form.
I can assign strings to these Session variables but i cannot assign values from Form elements to these variables
function SetUserName()
{
<% var text1= "Charles";%>
<%Session["UserName"] =text1;%>
var session_value='<%=Session["UserName"]%>';
}
</script>
This is the Javascript on the first page which works and on a button click i call another page where i can display this Session Variable.
function LoadUserName()
{
var username = '<%= Session["UserName"] %>';
alert(username);
}
This gets called on load of second page. How can i pass values from HTML elements to the Session Variable?
For Example Say i have a Text Box
<asp:TextBox ID="Address_Box" runat="server" TextMode="MultiLine"
MaxLength="200" ></asp:TextBox>
How can I send the text from this box to the session variable?
Your code:
var session_value='<%=Session["UserName"]%>';
does not "access the ASP.Net session from javascript".
With this code you do generate (during the processing of the request) some text that will be sent to the browser. This text contains the string that results from the expression Session["UserName"].
Once this text arrives at the browser it is interpreted as javascript. This javascript has no knowledge of any session values or the fact that it was (partly) generated.
You need to find some way to send data from the browser back to the server (using postback or ajax, for instance) before it can be stored in Session.
You would have to put this somewhere in your c# code.
Session["UserName"] = Address_Box.Text;
For a more complete answer I'd have to see more of your code. For a better solution you'd have to explain what you're trying to achieve
i have a page 'index.html' with a jquery load method like this:
$(document).ready(function(){
var user = 10;
$('#content").load('profile.html', { id: user });
});
My issue is how to get this variable id in the page 'profile.html'.
Thank you.
By supplying data to the load() call, you're sending that data via a POST request to the server.
As such, on the server side if you want to use that variable, you need to access the variable id from the POST data. Without knowing what language you're using for your server side, I can't help you further.
I try to pass my JS variable into razor, my script fragment:
select: function (event, ui) {
var docID = ui.item.DoctorCode;
#{
string org_code = _unitOfWork.Doctors.GetById("").OrganizationCode;
}
doctorOrgLabel.text('#org_code');
}
In GetById() method i want to pass JS variable docID. I'd appreciate any help!
I try to pass my JS variable into razor
This sentence makes strictly no sense at all.
Razor is a view engine used by the ASP.NET MVC framework running on the server to produce some HTML template.
javascript on the other hand is a client side language running on the client. Once the HTML template is rendered to the client and javascript starts to execute there's no longer such notion as Razor.
If you want to pass some javascript variable to the server you have a couple of options:
make an AJAX call to the server
set the value of this variable in some hidden field inside a form and submit this form
use window.location.href to redirect to the server and passing the variable as query string parameter
store the javascript variable in a cookie which will be sent to the server on subsequent requests
I need to enable a script which is present in a .net master page, in only selected html pages that use this master page. can this be achieved by declaring a variable in javascript of html pages where I need this script and set the variable to some value, so that I can enable the scripts in master page only when variable is NotNullorEmpty? Does some one know if this works. If so how to get javascript variable in html, in to .net master page ?
There are 2 ways I can think of.
The first if to assign the variable to a hidden input field. Which can be accessed in the Request.Form.
html
<input type="hidden" value="your value" name="Hidden1" id="Hidden1"/>
javascript
document.getElementById('Hidden1').value = "an other value";
C#
var myValue = Request.Form["Hidden1"];
The second is saving the variable into a cookie which can be accessed in Request.Cookies.
var myValue = Response.Cookies["Cookie1"].Value;
The most flexible form of this that I have seen is to render a JSON object directly to the page as script (adding any other script you might need also), then making the javascript code fill in a hidden field with JSON data on form submit for the server to parse.
Here are the basic steps:
Create an empty hidden field for the server to read when the client submits, but send it to the client empty.
Create a string which is the script and JSON data you need to send to the client. (Use the JavaScriptSerializer .NET class.)
Fill a LiteralControl with the script tag and its contents. (This works well in the OnPreRender step, but you can do it elsewhere.)
Include JSON2.js (it's all over the web - get it from a trusted source)
Write javascript code to fill the hidden field on submit, using JSON2.stringify() to push any javascript object into that field.
On the server, you can again use JavaScriptSerializer to read the values from the submitted object.
What you are basically wanting to do is test for javascript.
There are a few ways to do this, but I would use both a server-side and client-side cookie.
In your global.asax's onbeginrequest, test for a server-side cookie of "js-test-s"
Cookie Exists, check for "js-test-c"
"js-test-c" Cookie Exists, JS enabled
"js-test-c" No Cookie, JS disabled
Cookie doesn't exist...
Set the "js-test-s" cookie
output a simple html document...
[html]
[head]
[meta refresh tag set to 1 second /]
[script to set "js-test-c" cookie, then force refresh /]
[/head]
[body][/body]
[html]
Browser will refresh the request with one or both cookies set.
WARNING: You may want to store the original request details, and redirect to another location in case cookies are disabled.
I might be not understanding the question correctly but as far as I can tell the easiest way to do this is by creating a property on the master page and then setting it on all pages that need to execute the script:
In Master page
public bool EnableScript
{
set
{
if (value == true)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "startup", "alert('test');", true);
}
}
}
In all pages that need to run javascript that use the current master page:
protected void Page_Load(object sender, EventArgs e)
{
this.Master.EnableScript = true;
}
If you have to output your JavaScript on every page, even on the once that don't execute it (why?) then just add it as a function and then inside your property add a code to call that function, and set your property to true on all pages that require JavaScript to execute.