Click here to Skip to main content
15,879,326 members
Articles / Web Development / HTML

Magic AJAX: Applying AJAX to your existing Web Pages

Rate me:
Please Sign up or sign in to vote.
4.82/5 (72 votes)
28 May 2007MIT12 min read 964.2K   2.7K   251  
How to apply AJAX technologies to your web pages without replacing ASP.NET controls and/or writing JavaScript code.
<%@ Page language="C#" MasterPageFile="~/MasterPage.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
	<ajax:ajaxpanel ID="Ajaxpanel1" runat="server">
	<fieldset>
		<legend>Windows Processes (list is updated every 10 seconds using Callback Timer)</legend>
		<i>This example demonstrates the use of the MagicAjax CallbackTimer. <br />For this example we've set the Callback timer in the Page_Load event (hint: click 'View Source' at the bottom of this page to see the complete source of this aspx file).</i><br /><br />
		<div style="overflow:auto;height:400px;width:325px">
		<asp:DataGrid AutoGenerateColumns="false" AllowSorting="True" OnSortCommand="grid_SortCommand" runat="server" ID="DataGrid1" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="5" GridLines="Horizontal" >
			<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
			<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
			<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" Mode="NumericPages" />
			<AlternatingItemStyle BackColor="#F7F7F7" />
			<ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
			<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
			<Columns>
				<asp:BoundColumn DataField="ProcessName" SortExpression="ProcessName" HeaderText="Image Name" />
				<asp:BoundColumn DataField="PhysicalMemory" SortExpression="PhysicalMemory" HeaderText="Mem Usage" DataFormatString="{0:###,###,###} K" />
				<asp:BoundColumn DataField="HandleCount" SortExpression="HandleCount" HeaderText="Handles" DataFormatString="{0:###,###}" />
			</Columns>
		</asp:DataGrid>
		</div>
	</fieldset>
	</ajax:ajaxpanel>
</asp:Content>

<script language="C#" runat="server">
	private void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			// For automatic CallBack every 10 seconds.
			MagicAjax.AjaxCallHelper.SetAjaxCallTimerInterval(10000);
		}
		ShowProcesses();
	}

	private void ShowProcesses()
	{
		System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();

		System.Data.DataTable dt = new System.Data.DataTable("Processes");
		dt.Columns.Add("ProcessName");
		dt.Columns.Add("PhysicalMemory", typeof(Int64));
		dt.Columns.Add("HandleCount", typeof(Int32));

		foreach (System.Diagnostics.Process process in processes)
		{
			dt.Rows.Add(new object[] {process.ProcessName, process.WorkingSet64 /1000, process.HandleCount });
		}

		System.Data.DataView dv = dt.DefaultView;

		//sort data
		dv.Sort = SortExpression;

		//bind data
		DataGrid1.DataSource = dv;
		DataGrid1.DataBind();
	}

	 /// <summary>
	 /// SortExpression of the grid (stored in ViewState!)
	 /// </summary>
	 private string SortExpression
	 {
		 get
		 {
			 if (ViewState["_grid_SortExpression"] != null)
			 {
				 return (string)ViewState["_grid_SortExpression"];
			 }
			 else
			 {
						return "PhysicalMemory desc"; //default
			 }
		 }
		 set
		 {
			 ViewState["_grid_SortExpression"] = value;
		 }
	 }

	 private void grid_SortCommand(object source, DataGridSortCommandEventArgs e)
	 {
			string[] currentSort = SortExpression.Split(' ');
			
			if (currentSort[0] == e.SortExpression)
			{
				SortExpression = string.Format("{0} {1}", e.SortExpression, currentSort[1] == "desc" ? "asc" : "desc");
			}
			else
			{
				SortExpression = e.SortExpression + " asc";
			}

		 //refill datagrid
		ShowProcesses();
	 }
</script>

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 MIT License


Written By
Web Developer
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions