Friday, May 27, 2005

.Net Remote Script Request

Have you ever wanted an easy way to call a server side method from javascript? If so this class may be just what you need.

I have created a simple and easy to use method for accomplishing this task. All you have to do is have your webform inherit from the remote script page, make your call using a simple javascript object (will be included automatically), and when you execute the request in javascript the OnRemoteScriptRequest event will be raised in your server side code, you then process the request and write the result out on the response stream. The result will be returned by the javascripts execute method.

This .Net class library uses a combination of C# code and javascript to accomplish the task in an easy to use manner. The call uses Microsoft.XMLHTTP, Msxml2.XMLHTTP or XMLHttpRequest objects (depending on the browser) to do the underlying call back to the server. Because of this it will work in all recent IE and Mozilla based (Firefox, Netscape, Mozilla, etc.) browsers.

Try It Here!

Here it is step by step
  1. Include a reference to RemoteScript.dll
  2. Have your webform inherit from the remote script page:
      public class WebForm1 : RemoteScript.Pages.RemoteScriptPage
  3. Add the event handler in the InitializeComponent method
      this.OnRemoteScriptRequest += new RemoteScript.Pages.RemoteScriptEventHandler(RemoteScriptTest_OnRemoteScriptRequest);
  4. Add the event handler method
      private void RemoteScriptTest_OnRemoteScriptRequest(object sender, ...
  5. After you add your code to the event handler you will be able to call it from javascript using the RemoteScriptRequest javascript object, Example:

      objRS = new RemoteScriptRequest();
      strReturnValue = objRS.Execute();


Donload the source code and a sample webform here

You can view the documentation here: html - chm






2 Comments:

At 6/13/2005 10:43 AM , Anonymous said...

This is pretty cool, but I have to ask, how do you avoid having the viewstate flushed after a remote script request? I'm using this in production code because it works quite nicely, but it seems to be erasing my viewstate.

 
At 6/13/2005 1:35 PM , quamtar said...

I guess I don't fully understand the question. The viewstate is not available on the remote script call because the form has not been submitted. Only the parameters that you pass as part of the RemoteScriptRequest javascript object will be available. However, your viewstate on the main page should not be affected by the call.

 

Post a Comment

Links to this post:

Create a Link

<< Home