Click here to Skip to main content
12,065,756 members (27,313 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Stats

563K views
6.2K downloads
194 bookmarked
Posted

Remote Scripting

, 25 Apr 2005
Use client-side JavaScript to remotely invoke methods in ASP.NET pages.
RemoteScriptingBinaries.zip
scripts
RemoteScripting
bin
AMS.Web.RemoteScripting.dll
RemoteScriptingSourceAndBinaries.zip
AMS.Web.RemoteScripting.chm
AMS.Web.RemoteScripting.dll
<!-- 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))
			return;
				
		// 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"];
			type.DataBind();			
		}

		// 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);
		amount.DataBind();
		
		// 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();
			ds.ReadXml(MapPath("rsDemo.xml"));
			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>

<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 = "";	
	}

</script>

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

	<b>SALARY</b>
	<br>
	<table>
		<tr>
			<td width='1%'>Type:</td>
			<td><select id='type' dataValueField='value' dataTextField='text' runat='server' onchange='type_onchange()' /></td>
		</tr>
		<tr>
			<td>Amount:</td>
			<td><select id='amount' dataValueField='value' dataTextField='text' enableViewState='false' runat='server' /></td>
		</tr>
		<tr>
			<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>
		</tr>
	</table>
	
</form>
</body>
</html>

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.

License

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

Share

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...

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