Transferring value of javascript variable to asp variable - javascript

I've designed a javascript function which returns out an integer value (and before anyone says, no this cannot be done with asp or any server side scripting, at least to my knowledge). And I need the contents of the variable containing the integer value produced to be transferred into an asp variable so that I may do some server side scripting with it. Does anyone have any idea as to how to do this? I'm tearing my hair out over this!

Well, the preferred method to change server side status (variables) in the browser without reloading the whole page would be to use AJAX.
It's not necessary, but using jQuery, you could just call get:
// we are in javascript
var my_value = 42;
$.get("set_my_value.asp?to=" + my_value);
By this, you can change some data on the server side (e.g. as Session variable or in a database):
' classic ASP page (set_my_value.asp)
dim my_value
my_value = Request.Querystring("to")
Session("MyValue") = my_value

I don't think there's a way to do it directly. You can populate a hidden input in a form with the javascript value. Then on posting the form you can use the value using request("MyHiddenField")

You could try to do this via Cookies.
Your javascript function will store value in some Cookie.
In ASP code you reading Cookie.
This cookie could be specific for each user/session, just do not forget to send Cookie name from ASP to JavaScript function.

Related

what is the difference with "test" javascript variable and "#test" variable can i convert var javascript to "#test"

I've seen a servlet between the JSP and the database that is implemented similar this page. the important part for me is this:
param p1fd = new param();
p1fd.setVal(request.getParameter("formDDL"));
where the parameter in jsp code is referenced as "#formDDL"
the problem is that I have formDDL as a javascript variable as such:
var formDDL;
How can I convert the javascript variable to #formDDL?
Is there any other way to I pass this variable to Expression Language written for inserting in mySQL code?
The scriptlet part of the JSP is executed on the server side prior to it being returned to the client. Therefore you can't change its value after the page is sent to the client. You would have to have a preliminary page that sets the value of formDDL in javascript and then call into the server to request the page with the formDDL included as a request parameter or POST data. Basically the same approach that was done in the page you linked.
If you didn't want to have to fully reload the page to change the value of formDDL then you would have to take a completely different approach and look into using Ajax.
Also be mindful of SQL injection attacks when following a similar approach to the one you linked to.

Set property value

I have a user control. I set a property for that user control like:
public string SelectedValue
{
get { return _selectedValue; }
set { _selectedValue = value; }
}
I need to assign a value to property with "SelectedValue" using Javascript. How can I do that?
You can't set a C# property from a client-side (js). You may use ajax to do some work, but you simply can't manipulate server-side code.
If you want to know the longer explanation, see how asp.net actually works, it's lifecycle etc. Simple way of putting it would be like this:
A user sends a request to the server using his browser. The server receives it, creates a requested page and instantiates needed classes etc. Then it's gets parsed and sent to the client as html (and other resources of course, like images, css...). The instantiated page class CAN'T be accessed and modified afterwards by the client, because it's already flushed by the server. Every request creates a new instance. There's no way of modifying c# with js anyway but its vice versa is possible. Can you imagine what it would be like, if you could use some js to modify C# on a remote server? users can hack your side by changing properties.
Assuming you are using C#, and assuming you have access to some object that has the property SelectedValue, you can do that like this:
var value = "<%# YourObject.SelectedValue%>";
or, if you are working with Razor and MVC:
var value = "#YourObject.SelectedValue";
If you want to send this to server side code, you need to do as Zaheer Ahmed said, ajax or save the value in a cookie.
But there are so many assumptions, maybe you can post some more code and give some more details.

Will aspx not see changes made by javascript?

I am changing the contents of a span with javascript. I can see that it has changed with view source.
When I try to get the contents in a c# function right after, it still has the old value.
My guess is that asp is just seeing the html that it sent the client, and that javascript is changing the client html of which asp cannnot see?
this was my only guess on how to explain it. Would this be correct?
Thanks.
Unless what you are changing support POSTing the values back to the server, then your changes will be discarded. <span>'s do not get POSTed to the server.
If you change the value of INPUT's however, then the server will be able to see those new values on the nest page submission POST, provided they are runat=server.
ASPX is SERVER SIDE. Javascript is CLIENT SIDE. The two do not mix unless you make a call back to a webservice, or something similar.
Your <span> change is only on the client, and the server is not aware of it. Look into AJAX as a solution if you wish to communicate interaction back to the server.
Yes, your guess is correct!
Javascript sees only the client side and ASP.NET only the server side. If you make some change on the client side and you want to server side to be aware of that change, you can make use of AJAX.
See Ajax for more info.
In .NET you can use a WebService, a Controller, or a Page for receiving the ajax call.
add runat="server" to your span. this will cause it to get picked up by viewstate and the new value will be available during postbacks.
edit: according to jrummel and testing by mikemanne, a span will not post back even when decorated with runat=server.
I would suggest using an input (as others suggest) instead of a span if you want the value consistently available on the server.

Can JavaScript variables be easily modified maliciously?

I am setting up a quiz that uses boolean variables for correct/incorrect and then passes those variable values to a PHP script via Ajax for processing and storing in a database.
How easily could someone override the values set by my code with after finding the var names with "view source"?
Yes.
You should send the answers to the server and let the server grade the quiz.
They can do it easily using Chrome/Firebug Console by issuing a Javascript command over there like
var your_var_name = 60;
You must have backend synchronization also to prevent this.
for testing purpose you can use firefox add on firebug or chrome's developer tool kit. Change your javascript variable using inspect element and than perform action of button which posts your data. On server side you should make sure that posted variable must be any of variable that is in option of answer of that question.

How to get value of hiddenfield in another form

iIhave value in hdnField in form1.aspx . I assign a value to hdnfield in javascript .I want to get that value in aspx.vb in another form, form2.aspx. How can I accomplish this?
If your Form1.aspx submits to Form2.aspx, then you have atleast a few ways to access the value of form fields (including Hidden fields):
The Request.Form property exposes a NameValueCollection containing all submitted form field names as Keys and their values as Values. You could use the syntax Request.Form["fieldName"] to access the value.
If this is ASP.NET 2+ and you used the Cross-page posting technique, you will be able to access field values in the previous page using the PreviousPage property of the Page.
If you use Server.Transfer, you can access values using the Current HttpContext.
If you need more info, you should take a look at Passing values between pages in ASP.NET.
I think your concept of session is wrong. Session is a server-side object, and javascript runs on the client, so you can not directly assign that value to a session. You can, instead, use some AJAX to send it to the server and then add code in the server so the value is assigned.
Hm... you have to think about the difference between serverside and clientside first...
You cant directly access changes you made in the client on the serverside, because you once send a request to server, as a response you get the site diplayed in your browser. As soon as you recieve the request, the server is finished and cant access the site anymore. Its like a letter you send away. As soon as you put it into the mailbox, you cant make changes anymore.
But you could post a new request to the server and add POST or GET peremeters. These can be accessed by the server. The way you send the request doesnt matter... you can send a request using AJAX or simply reload the page.

Categories

Resources