How to call a .cs method from a JavaScript function? - javascript

I need to call a .cs method from JavaScript function; how can I do that?
Thanks

You need to do post back to the server then catch this post back on Pageload event then execute the cs function.
check the following article:
Understanding the JavaScript __doPostBack Function

You need to postback to the server by either submitting an HTML form or using JavaScript. If you're not in a browser, it might be more of a challenge because I don't think the Windows Scripting Host includes anything that can call the web by default. In which case you could use something like cURL instead.

Related

Using POST to send data back to objective c

I am attempting to send data back to my objective c program from javascript.
I found a very helpful question here that got me started, but I quickly ran into a problem: the post seems to be failing.
I also verified syntax at this helpful site.
I know this because I update the contents of a div on my page before and after the post call and I only see the update pre-post. I also have a breakpoint at the beginning of shouldStartLoadWithRequest and it's not firing.
Here's my simple javascript code:
function updateAction(obj) {
document.getElementById("status").innerHTML="sending:"+obj;
$.post("http://actionupdate", {"data":obj});
document.getElementById("status").innerHTML="sent";
}
So.. my question: what am I missing? What's wrong with my post?
From your syntax, I guess you are using jquery? The UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method does not seem to get called for ajax requests, which is what $.post() does. You have to use a normal post instead.
You may find UIWebViewDelegate not monitoring XMLHttpRequest? helpful.

Executing JavaScript from the server code in asp.net

Here is what I need to do: I need to execute some server side code after a user clicks on a button, THEN I need to execute some client side code. OnClientClick gets invoked BEFORE the onClick; and what I need is the opposite.
Is there a way to do this? It looks like I will have to execute JS code from the server code.
You could use RegisterStartupScript in the code behind of your app.
This would place the javascript code you need to be called in the body.onload event.

ASP.NET MVC + Javascript (calling c# code with javascript variables)

is it possible to call c# functions with javascript parameters (from javascript)?
i.e.
<script>
function someFunction()
{
var data = "123";
var test ="<% AppHelper.GetSomething("+data+"); %>";
}
</script>
Thank You !
No, because ASP.NET code is run and compiled before you get to the javascript. If you want to communicate with the server using javascript, you need to use something like AJAX.
Or you might have a look at server side javascript, to see if that's something you can use.
You could do it but you would have to use an ajax call to hit a URL endpoint which will execute an action. Once the action has been triggered you can do what ever you want in c#.
What you want to do exactly because with jQuery it's very easy to do a call to a controller that return some HTML or data that you can use in javascript. I could give you an exemple if you give more details of what you are trying to do.

Call from JavaScript to server-side code in JSF

I'm looking for an easy way to call a bean's method that will take no parameters and return a string in JSF. The thing that I don't really need is that the method returns an action result and then uses the whole JSF life-cycle to do get me to another view. I need to do that from JavaScript so that I can put together some client-side parts of the application and going over the A4J part of RichFaces has brought me nothing so far.
So here's the scenario again in a step-by-step form:
from JS issue a GET on some address
on the server process that GET and return JSON or HTML (basically a string)
once the request is sent back to the client I want to be able to process it further with JS.
Thanks!
Use a4j:jsFunction and the data attribute.
So roughly you want something like:
<button onclick="callBackend();">Go</button>
<a4j:jsFunction name="callBackend" action="#{myBean.someMethod}" data="#{myBean.someString}" oncomplete="handleResponse(data);"/>
<script>
function handleResponse(response) {
alert(response);
}
</script>
Damo: can you explain why it might only work for the first time the method callBackend is executed? I'm experiencing a strange behavior that the first call succeeds and the next calls are just blocked. I see the server-side code being executed but some strange result is being sent back to the browser (something like the _viewstate and those kind of things).

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