Set property value - javascript

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.

Related

Send values from django server to client?

I would like to give some values to my client (to use them in javascript) from my django server.
I know that I can use cookies, but the problem is that they are kept for all request whereas I need them only once when the client loads for the first time (it is not really a problem).
I could also pass those values to the template to render some html tags with the "data-value-name" properties set the the values (for example <body data-name="joe" data-id="123456">)
Both solutions work fine, but is there a better method?
What I usually do is to create a <script> element where I render the values directly to JavaScript objects stored in local or global variables.
If you have a lot of values you can also consider implementing a view that returns JSON which is requested via ajax from the client.

Transferring value of javascript variable to asp variable

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.

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.

i18n in javascript using .properties file

I'm developing a web application with JSF, so I use a Java Properties File (i.e. resources_es_CO.properties) to localize strings for the application and this is working OK.
Mi question is: how can I call localized strings in this kind of files from my javascript validations so alerts show those localized strings to user?
Thanks in advance.
What I do is to send the messages out as part of the page, dropped into hidden <span> tags with "id" values made from the property names.
Alternatively, you could write an Ajax-called action and fetch the properties dynamically.
To do an ajax callback, you'd have to implement a server-side action that would understand something like the property key. The server would just apply the localization (ie look up the property in the locale associated with the session) and then return the string. Alternatively, you could implement a service that'd return a whole set of properties, maybe on a per-form basis, or grouped according to some convention of property names (like, "return all properties that start with 'validation.addressForm'")
The simplest case would look something like this with jQuery:
$.get('/fetchProperty', { property: 'firstNameMissing' }, function(propValue) {
$('#errMsg').text(propValue);
}, "text/plain");
Other frameworks provide similar ajax tools, or you could do the XMLHttpRequest yourself.
you could go to server with an ajax call and send alert texts from server to client and show it. or you could put messages to your page when your jsp's being rendered. both is ok. if you can change language without refreshing the page you probably want to make ajax call. if you can not , putting messages in javascript variables will be easier

Set web part personalizable property from JavaScript

I have defined a personalizable property on a web part and I would like to modify its value from JavaScript. Is it possible?
You would have to use AJAX to send a message to the server. There's no way to set it from JavaScript without going to the server.
The AJAX could be as simple as a PageMethod call though.

Categories

Resources