Click here to Skip to main content
12,755,765 members (37,570 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads


195 bookmarked
Posted 15 Mar 2004

Remote Scripting

, 25 Apr 2005
Use client-side JavaScript to remotely invoke methods in ASP.NET pages.
<!-- Remote Scripting Demo -->
<%@import namespace='System.Data' %>

<script language='C#' runat='server'>
	void Page_Load(object sender, EventArgs e)
		// If it was called, invoke the remote method and get back to the client
		if (AMS.Web.RemoteScripting.InvokeMethod(Page))
		// Load the drop downs
		DataSet ds = GetDataSet();

		// The salary type can be loaded once and saved in the View State		
		if (!IsPostBack)
			type.DataSource = ds.Tables["SalaryType"];

		// The salary amounts should always be loaded since they can be changed remotely
		amount.DataSource = new DataView(ds.Tables["SalaryAmount"], "type='" + type.Value + "'", null, DataViewRowState.CurrentRows);
		// Since DataBind clears the selection, we need to set it again based on the request
		amount.Value = Request["amount"];		

	DataSet GetDataSet()
		// Use the session to improve performance
		DataSet ds = Session["DataSet"] as DataSet;
		if (ds == null)
			ds = new DataSet();
			Session["DataSet"] = ds;
		return ds;
	void submit_Click(object sender, EventArgs e)
		// Show the selected values
		message.InnerHtml = "type = '" + type.Value + "'<br>amount = '" + amount.Value + "'";

	// This is the remote method; the chosen type is passed as its parameter.
	public string UpdateAmounts(string type)
		DataSet ds = GetDataSet();
		StringBuilder result = new StringBuilder();
		// Format the option tags into one string to be passed to RS.ReplaceOptions.
		foreach (DataRow row in ds.Tables["SalaryAmount"].Select("type='" + type + "'"))
			result.Append("<option value='" + row[0] + "'>" + row[1] + "</option>");
		return result.ToString();


<script language='JavaScript' src='scripts/rs.js'></script>
<script language='JavaScript'>

	// This triggers the call to the remote method to reload the amounts
	function type_onchange()
		window.status = "Loading the Amounts...";
		var type = document.form.type;		
		RS.Execute("rsDemo.aspx", "UpdateAmounts", type.options[type.selectedIndex].value, updateAmounts);

	// This is the callback invoked after the server responds.
	function updateAmounts(options)
		RS.ReplaceOptions(document.form.amount, options);		
		window.status = "";	


	<title>Remote Scripting Demo</title>
<body bgcolor='#fbedb5'>
<form id='form' runat='server'>

			<td width='1%'>Type:</td>
			<td><select id='type' dataValueField='value' dataTextField='text' runat='server' onchange='type_onchange()' /></td>
			<td><select id='amount' dataValueField='value' dataTextField='text' enableViewState='false' runat='server' /></td>
			<td><br><input type='submit' id='submit' value='Submit' onserverclick='submit_Click' runat='server'></td>
			<td><br><small><span id='message' runat='server'>(to show that the values are preserved)</span></small></td>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alvaro Mendez
Web Developer
United States United States
I've done extensive work with C++, MFC, COM, and ATL on the Windows side. On the Web side, I've worked with VB, ASP, JavaScript, and COM+. I've also been involved with server-side Java, which includes JSP, Servlets, and EJB, and more recently with ASP.NET/C#.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170217.1 | Last Updated 25 Apr 2005
Article Copyright 2004 by Alvaro Mendez
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid