views:

8561

answers:

8

Lets say on my page I have this function:

 function ReturnFoo(bar)
 {
   return bar.toString() + "foo";
 }

Now, I would like to have this called from ASP .NET, hopefully with the ASP .NET AJAX framework, as I am already using it in this codebase (I have already spent the 100k, might as well use it).

Also, I would like to get back the output that is returned from this function and then assign it to a variable created on the server side. And this is restricted to ASP .NET 2.0

Thanks.

A: 

I don't see how you expect server-side code to call a client-side function???

AJAX calls are from the client-side to the server-side!

Matt
Matt, I know this can be done with Silverlight, but I wanted to know if it was possible with asp .net without Silverlight. With silverlight I can grab the DOM directly, I didn't know if that was possible in asp .net.
RyanKeeter
A: 

I have done this before by using a text control marked as hidden with autopostback on change. Then, the function sets the text of that control which triggers a postback.

To call the function from the server side, simply register some startup script and it will run when the page renders.

BoltBait
A: 

If your intention is to reuse some functions in both server side and client side, I would suggest to write that function in server side (aspx or asmx) and call it from client side (JavaScript) using AJAX. Not sure whether you are looking for this.

Vijesh VP
A: 

Any time you transition between server-side and client-side, it's like the user clicked the refresh button on their browser in terms of performance.

Is that really what you want to do, since your proposed scenario would mean at least two post-backs.

What is the purpose of this? It would be better if you could keep this at the client.

Joel Coehoorn
+1  A: 

Yeah, this is a difficult one. Using MS Ajax will help you out a bit. You'll need to push in code from the server to call this function upon page load and assign the return value to a hidden field that can be accessed by the server on post back.

I must say that this solution sucks, but I don't know another way. Hopefully, someone will have a better solution.

Craig Wilson
A: 

Hey Ryan,

In the first answer, Matt mentioned that this is not possible (to execute client-side code while still on the server). And that's true.

Your comment about silverlight is not really accurate. Silverlight can't call client-side code from the server. Silverlight runs on the client just like JavaScript, so that's a client-to-client call.

Can you clarify the scenario a bit... here's what I think you're asking:

private void Page_Load()
{
    string someValue = EXECUTE_JS_ON_CLIENT_AND_GET_RESULT();

    // do some stuff here while still on the server...
}

If this is what you want to do, it's not possible. While on the server, you can't communicate with the client.

Is that what you meant?

Thanks, -Timothy

Timothy Khouri
+1  A: 

Write simply where you have to call javascript

Response.Write( " alert('hello');")

This works in some cases (and you also have to Response.Write the surrounding <script> tags), but I've run into issues where ASP gives me errors when I try to write to the client page when a Post-Back is involved.
Ogre Psalm33
A: 

I would check out JSON.Net (combined with a library [jQuery is also free]) they're open source solutions that makes these problems a lot easier.

Chris Missal