Four Ways to Locally Update a Web Page
page 5 of 12
by Xianzhong Zhu
Average Rating: 
Views (Total / Last 10 Days): 49193/ 95

Sample 2

1. Create a Sample Web Site

Launch Visual Studio 2005 and create an ASP.NET website named JavaScriptWay (select Visual C# as the built-in language).

Open the web page "Default.aspx" and make some modification with it. The following Figure gives the final design-time snapshot.

Figure 3: The design-time snapshot for Sample 2

Note that here the DropDownList control is directly embedded inside the page, in contrast to that in Sample 1.

Now, let us start to research into the most interesting part - injecting JavaScript into the client side.

2. Inject the client-side JavaScript

Press F7 and switch to the CodeFile "Default.aspx.cs."  With the foregoing analysis, we would better achieve the goal of injecting all the required JavaScript functions into the client side at the very time the page just start being loaded. OK, the best site should be function Page_Load. Listing 6 shows the interesting inner workings.

Listing 6

protected void Page_Load(object sender, EventArgs e)
  StringBuilder myscript = new StringBuilder();
  myscript.Append("function Search()    {\n");
  myscript.Append("var city=document.getElementById('txtCity').value; \n");
  myscript.Append("switch(city)       {\n");
  myscript.Append("case 'BeiJing': \n");
  myscript.Append("FillData('" + GetAreaStr("BeiJing"+ "'); \n");
  myscript.Append("break; \n");
  myscript.Append("case 'ShangHai': \n");
  myscript.Append("FillData('" + GetAreaStr("ShangHai"+ "'); \n");
  myscript.Append("break; \n");
  myscript.Append("case 'WeiFang': \n");
  myscript.Append("FillData('" + GetAreaStr("WeiFang"+ "'); \n");
  myscript.Append("break; }\n");
  myscript.Append(" }\n");
  if (!Page.ClientScript.IsClientScriptBlockRegistered("Search"))
    Page.ClientScript.RegisterClientScriptBlock(typeof(string), "Search",
      myscript.ToString(), true);
private string GetAreaStr(string city)
  string area = "";
  switch (city)
    case "BeiJing":
      area = "Chao Yang,Hai Dian,East City,West City";
    case "ShangHai":
      area = "Pu Dong,Jing An,Hong Kou,Xu Hui";
    case "WeiFang":
      area = "Fang Zi,Wei Cheng,Kui Wen,Kai Fa";
  return area;

Here, we have created and embedded a JavaScript function "Search" into a StringBuilder object; finally, we call Page.ClientScript.RegisterClientScriptBlock to inject the code of JavaScript function "Search" into the client side. In building up the string we have mentioned a client-side helper function "FillData" which must be created on the client side.  Also, for simplicity, we have hard coded the helper function GetAreaStr.

For now, cute readers may have noticed something. Undoubtedly, we have hard coded the JavaScript code to be injected into the client side, which obviously results in poor flexibility.

3. The client-side Coding

Now, let us take a quick look at the client-side function FillData. The following lists the complete source code of this function.

Listing 7

function FillData(strArea)
  document.getElementById("DropDownList1").options.length = 0;
  var indexofcity;
  var city;
  while (strArea.length > 0)
    indexofcity = strArea.indexOf(",");
    if (indexofcity > 0)
      city = strArea.substring(0, indexofcity);
      strArea = strArea.substring(indexofcity + 1);
      document.getElementById("DropDownList1").add(new Option(city, city));
      document.getElementById("DropDownList1").add(new Option(strArea, strArea))

Simply put, this function uses a while loop to cut the passed string (which holds area names related to the city specified by the user) into pieces and populate the control "DropDownList1" with them.

That is all!  Now it is time to appreciate the final result. Just press F5 and, without anything wrong, you will see the run-time snapshot as displayed in Figure 4.

Figure 4: The run-time snapshot for Sample 2

Next, let us continue to dig into another way to achieve the partially updating effects.

View Entire Article

User Comments

Title: d   
Name: d
Date: 2012-10-24 12:04:07 PM
Title: Locally update a web page   
Name: Santosh
Date: 2010-05-04 3:41:59 AM
It's an amazing and really very very helpful article. After reading this article, I have become a fan of this site.... I request you to not delete this article, as it may be very helpful to many people.
Title: updating a web page   
Name: Mrs.
Date: 2009-05-19 3:23:22 PM
this is great
Title: Download Links Does Not Work   
Name: Tarik
Date: 2008-11-07 9:04:04 AM
Good Article , But Please Review The Downloads Links
Title: Mr.   
Name: Joydip Kanjilal
Date: 2008-10-16 10:25:58 AM
Title: how up date webpage   
Date: 2008-09-08 10:32:32 PM

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-07-25 12:25:19 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search