Click here to Skip to main content
15,896,606 members
Articles / Web Development / HTML

Remote Scripting in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.22/5 (10 votes)
28 Aug 2013CPOL9 min read 61.2K   733   37  
A remote scripting system performs remote server function calls from the browser level. This allows to save the time necessary to completely resend and build a page due to a query working somewhere in the background and supplying small amounts of data at a time.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace JSRS
{
	public class Cars : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DropDownList ddlMake;
		protected System.Web.UI.WebControls.DropDownList ddlModel;
		protected System.Web.UI.WebControls.DropDownList ddlType;
		protected System.Web.UI.WebControls.Button btnCheck;
		protected System.Web.UI.WebControls.Label lblMake;
		protected System.Web.UI.WebControls.Label lblModel;
		protected System.Web.UI.WebControls.Label lblType;

		private DataSet ds
		{
			get
			{
				if (Session["ds"] != null)
					return (DataSet)Session["ds"];
				else
				{
					ds = new DataSet();
					ds.ReadXml(MapPath("Cars.xml"));
					Session["ds"] = ds;
					return ds;
				}
			}
			set { Session["ds"] = value; }
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (AMS.Web.RemoteScripting.InvokeMethod(this))
				return;

			ddlMake.Attributes.Add("onchange","LoadModel()");
			ddlModel.Attributes.Add("onchange","LoadType()");

			if (!Page.IsPostBack) 
			{
				ddlMake.DataSource = ds.Tables["Make"];
				ddlMake.DataTextField = "MakeName";
				ddlMake.DataValueField = "MakeID";
				ddlMake.DataBind();
				ddlMake.Items.Insert(0,new ListItem("","-1"));
			}

			ddlModel.DataSource = new DataView(ds.Tables["Model"], "MakeID='" + ddlMake.SelectedValue + "'", null, DataViewRowState.CurrentRows);
			ddlModel.DataTextField = "ModelName";
			ddlModel.DataValueField = "ModelID";
			ddlModel.DataBind();
			ddlModel.Items.Insert(0, new ListItem("","-1"));
			ddlModel.SelectedValue = Request["ddlModel"];

			ddlType.DataSource = new DataView(ds.Tables["Type"], "ModelID='" + ddlModel.SelectedValue + "'", null, DataViewRowState.CurrentRows);
			ddlType.DataTextField = "TypeName";
			ddlType.DataValueField = "TypeID";
			ddlType.DataBind();
			ddlType.Items.Insert(0, new ListItem("","-1"));
			ddlType.SelectedValue = Request["ddlType"];

		}

		#region Vom Web Form-Designer generierter Code
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}
		
		private void InitializeComponent()
		{    
			this.btnCheck.Click += new System.EventHandler(this.btnCheck_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnCheck_Click(object sender, System.EventArgs e)
		{
			lblMake.Text = "Make ID: "+ddlMake.SelectedItem;
			lblModel.Text = "Model ID: "+ddlModel.SelectedItem;
			lblType.Text = "Type ID: "+ddlType.SelectedItem;
		}

		public string GetModel(string MakeID) 
		{
			string output = "<option value='-1'></option>";
			foreach (DataRow row in ds.Tables["Model"].Select("MakeID='" + MakeID + "'"))
				output += "<option value='" + row[0] + "'>" + row[2] + "</option>";
			return output;

		}

		public string GetType(string ModelID) 
		{
			string output = "<option value='-1'></option>";
			foreach (DataRow row in ds.Tables["Type"].Select("ModelID='" + ModelID + "'"))
				output += "<option value='" + row[0] + "'>" + row[2] + "</option>";
			return output;
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Publisher
Poland Poland
Software Developer's Journal (formerly Software 2.0) is a magazine for professional programmers and developers publishing news from the software world and practical articles presenting very interesting ready programming solutions. To read more

Comments and Discussions