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.
Related
I recently wrote a PHP wrapper class for a new API we are using and have been asked to setup a demo which makes use of it. Certain features can be called directly from PHP, however things such as performing actions on button clicks requires I make use of JS/AJAX.
As I already have an instance of the object in my main PHP file, can I pass this as a parameter to JS and then pass it through Ajax to my handler or is it necessary to establish two separate instances?
The closest thing to doing what you want that I can think of would be copying the PHP object to a JavaScript variable via json_encode and then passing that variable back to your PHP code during the AJAX event via the data parameter. PHP code doesn't persist in the way that you seem to be describing - once a page has been requested by a browser, your PHP code for that page is done, there are no variables persisted by the server after that point.
I have control
LogInOut.ascx
which i use on different pages *.aspx
I need to add logic on button click.
I need to make ajax call to contol's method but i don't know how to get access to it.
What i try : LogInOut.ascx/validateUser, /validateUser, /Controls/LogInOut.ascx/validateUser, but always get error in ajax call
*( in debug mode i never was in this method, so problem in javascript).
You can't call a page method declared within an ASCX user control, use an ASMX web service.
http://forums.asp.net/t/1423555.aspx
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.
I'm using pyfacebook on the backend and javascript on the client side. Now if I want to pass a variable to the javascript from pyfacebook. how would I go about doing that, any ideas?
You can't pass a variable directly, as JavaScript is running on the client (browser), and Python is running on the server.
You could make a XHR (AJAX) request from JavaScript to the server which would then return your values back to JS (JSON could be used here).
Or you could put a hidden field to your markup that would have the value in it's "value" attribute. You could then read that with JavaScript.
ps: your question really isn't related to pyfacebook but Python (or any other server side technology) in general and that has been covered here many many times.
I'm trying to connect to a webapi at a location that looks like this from inside my js jquery file.
example.com/?var=input
Is there a simpler way to do it than an ajax call?
I would use AJAX for this, but I guess you could open a hidden IFRAME with the URL set to the page you want to connect to (not sure why you would do this though!).
Maybe use a JavaScript library like JQuery to make life easier?
If the data you're trying to access is returned as JSON then you can get around the browser security problems.
Here is a JQuery example where a request is made to Flickr.com from JQuery.com:
docs.jquery.com/Ajax/jQuery.getJSON
You may be run into cross domain issues if you do it with an ajax call.
Call the web-api from serverside, it would be the most appropriate way.