ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
XML for ASP.NET Developers
XML for ASP.NET Developers

Find Prices
Read Review
Read Review
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software

Print this article

Article Index . Remote Scripting in .NET - Part I
Remote Scripting in .NET - Part I
Jonathan Cogley
12/21/2002

Part I covers the comparison of Remote Scripting with Web Services, how to enable Remote Scripting to work in .NET and demonstrates a new Remote Scripting "test bed" that is available.

Introduction

Remote Scripting is a technology for ASP used to call server side code from a client browser without incurring the penalty of a round trip (GET or POST) to the server. Typically this is used to look up details by an identifier (for example, a product name by UPC or a book author by ISBN) while a user is completing a form. The .NET framework does not include built-in support for Remote Scripting on the server side but rather promotes Web Services as the preferred alternative. While Web Services have their advantages, they are not perfect for all purposes and Remote Scripting still has a place in our .NET toolbox.

For more information on Remote Scripting in ASP, see the MSDN Documentation.

Remote Scripting in .NET - Pros

  • Protocol is lightweight - uses HTTP GET
  • Uses an applet to make calls back to the server (works with any Java capable browser such as Internet Explorer, Mozilla, Netscape)
  • Takes advantage of legacy client-side code - this would make porting applications easier as you only need to port the server side layer

Remote Scripting in .NET - Cons

  • Remote Scripting uses it own non standard XML based protocol vs. Web Services which use the industry standard SOAP (Simple Object Access Protocol)
  • Remote Scripting in classic ASP has no built in "test bed" to allow you to test your server code directly and as many Remote Scripting developers know, it can be difficult to debug.
    (there *is* now a test bed in .NET for Remote Scripting!)

Remote Scripting in .NET

Follow these 7 steps to get Remote Scripting working and tested in your ASP.NET application.

1)Download the Thycotic.Web.RemoteScripting assembly which provides the replacement server side plumbing (source code is also available under the LGPL) from Thycotic Software Ltd.

2) Add a reference to this assembly to your ASP.NET project.

3) Add a Remote Scripting client. Choose one of the following:

  • Download the Thycotic msrsclient.js client.
    • Supports HTTP GET and POST
    • Only supports asynchronous calls
    • Doesn't use an applet and works in all browsers
  • Download the Microsoft Remote Scripting client or just copy it over from a classic ASP application. (You will need the rs.htm and rsproxy.class files in a _ScriptLibrary folder.)
    • Uses an applet - only works in Internet Explorer due to security issues in Netscape/Mozilla.
    • Supports synchronous, asynchronous and proxyobject calls.

4) Create a new Web Form that will contain the server-side methods you wish to call from the browser. Call it for example, MyServerSidePage.aspx and add the following code to the "code behind" page. Remove everything from the front page of the Web Form except for the Page directive. Remember to build your project when you are done!

In your code behind page (MyServerSidePage.aspx.cs)

using System;
using Thycotic.Web.RemoteScripting;

namespace TestRemoteScripting1 
{
  public class MyServerSidePage 
		: Thycotic.Web.RemoteScripting.RSPage 
  {
    [RemoteScriptingMethod(Description="Converts text to upper case.")]
    public virtual string ToUpperCase(string s) 
    {
      return s.ToUpper();
    }
      
    [RemoteScriptingMethod]
    public virtual DateTime GetServerTime() 
    {
      return DateTime.Now;
    }
  }
}

In your front page (MyServerSidePage.aspx)

<%@ Page language="c#" Codebehind="MyServerSidePage.aspx.cs" 
AutoEventWireup="false" 
Inherits="TestRemoteScripting1.MyServerSidePage" %>

5) Try out your server side page by viewing it in your browser. You will see a new "test bed" for Remote Scripting (provided by Thycotic.Web.RemoteScripting assembly) which allows you to test your server side code.



Example screenshot of the available "test bed" - modeled on the Web Service "test bed"

6) Create a Web Form with client side script to call your Remote Scripting method.
Note: Change the SCRIPT tags to enable the client you are using.

<%@ Page language="c#" Codebehind="SimpleClientPage.aspx.cs" 
AutoEventWireup="false" Inherits="SampleRemoteScripting.SimpleClientPage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
 <title>SimpleClientPage.aspx</title>
 <!-- Microsoft Remote Scripting client -->
 <SCRIPT LANGUAGE="JAVASCRIPT" SRC="/TestRemoteScripting1/_ScriptLibrary/rs.htm">
 </SCRIPT>
 <SCRIPT LANGUAGE="JAVASCRIPT">
 RSEnableRemoteScripting("/TestRemoteScripting1/_ScriptLibrary");
 </SCRIPT>
 <!-- Uncomment to use the Thycotic msrsclient.js client -->
 <!-- <SCRIPT LANGUAGE="JAVASCRIPT" SRC="msrsclient.js"></SCRIPT> -->
 <SCRIPT language="javascript">
 var aspObject;
 var serverURL = "MyServerSidePage.aspx";
 function ToUpperCase() {
   var result = RSExecute(serverURL,"ToUpperCase",document.myForm.stringInput.value);
   if (result.return_value) {
     document.myForm.stringOutput.value = result.return_value;
   }
 }
 </SCRIPT>
</head>
<body MS_POSITIONING="FlowLayout">
 <form id="myForm" name="myForm">
 Very simple client page.<br/><br/>
 Single text value: 
 <input type="text" name="stringInput" id="stringInput" value="hello">
 <a href="javascript:ToUpperCase()">ToUpperCase</a><br/>
 Result: <input type="text" name="stringOutput" id="stringOutput" value="">
 </form>
</body>
</html>

7) View the Web Form to try it for yourself.

Functionality
The Thycotic.Web.RemoteScripting assembly provides almost all of the functionality that was available in classic ASP. All input parameters to Remote Scripting methods must be of type string. It supports returning the following types: string, number (int, long, double, float), boolean, date (DateTime) and arrays of any of these types. The assembly supports both synchronous and asynchronous calls (these are actually implemented by the applet) and it also supports use of the RSGetASPObject function to return an object proxy for making your Remote Scripting calls. See the downloadable sample below for examples of using these features.

Contribute
To report bugs, contribute code for enhancements and to track progress of the assembly, please visit the Thycotic.Web.RemoteScipting Project Page.

Conclusion
Remote Scripting isn't a perfect technology but it has its place depending on the requirements of the application. The Thycotic.Web.RemoteScripting assembly is a simple add-in to ASP.NET and it brings Remote Scripting one step closer to Web Service functionality with the ability to test your Remote Scripting code using the built-in "test bed".

*WORKING DEMO* of Remote Scripting in .NET
Click here!

Related Downloads
Sample ASP.NET application using Remote Scripting

Related Links
Thycotic.Web.RemoteScripting Project
Microsoft Remote Scripting [MSDN]
Scripting Web Services [MSDN]
Remote Scripting in a .NET World
GNU Lesser General Public License

About the author
Originally from South Africa, Jonathan Cogley has worked as a software consultant in the UK and the USA. He is now based in Pittsburgh, Pennsylvania where he operates Thycotic Software Ltd, a company he started in 1996. His favorite languages are Perl, Java, JScript/Javascript and C#.


Copyright © 2002 Thycotic Software Ltd.
 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/21/2009 6:12:53 PM