Click here to Skip to main content
Licence 
First Posted 15 May 2001
Views 212,196
Bookmarked 56 times

Refresh Portion Of Your Web Page Using XMLHTTP

By | 15 May 2001 | Article
This article explains how portion of a web page can be selectively refreshed using XMLHTTP

Introduction

Did you ever have to refresh only selective part of a web page without having to refresh the whole page?. If yes, you are not alone. Fortunately, there are number of solutions which can address this issue, including remote scripting and XMLHTTP. This articles explains how a part of web page can be refreshed by using XMLHTTP, without having to refresh the whole page .

XMLHTTP

XMLHTTP object can be used to make HTTP request from the web browser to the server. XMLHTTP is part of Microsoft's XML parser and it is already installed in your machine if you have Internet Explorer.

Please refer MSDN help on how to use XMLHTTP object. Also, I recommend you reading my article Making HTTP Communication from MFC/Windows Application first, because I use the same techniques here too.

Example

Sample app screenshot

In the above example web page, we can select a country from the country list box. For a country selected, we'll display list of states/province for that country. When you click on a country, a HTTP request to the web server will be send to get all the states for the specific country. States list box will be refreshed with the result from the server. Note that we are not using the submit to send a HTTP request to the web server. Instead, we use XMLHTTP since we do not want the whole page refreshed. Also, note that both request and response are in XML string and we will have to use the XML DOM to read the node values.

If you selected Canada in the country list box, the XML request string which is send from the web browser to the web server will look like below:

<RequestStates Country='Canada'></RequestStates>

The server (i.e. states.asp in this case) will process this request and sends back XML response, which looks like as shown below:

<Response>
	<State>Alberta</State>
	<State>British Columbia</State>
	<State>Ontario</State>
	<State>Quebec</State>
</Response>

The client has to process this XML response and update the states list box. The DisplayStates() function in demo.html takes care of this functionality.

Related Articles

The article Refreshing only part of your Web Page is on the same topic. However, it uses remote scripting (not XMLHTTP) to refresh part of web page.

Installation Notes

Download the sources (states.asp and demo.html) to the root directory of your web server. Note that the sample application assumes that your web server runs on the same machine as the client. If your web server runs on different machine, you have to change the URL name in XMLHTTP's open() method (in demo.html) appropriately. This sample application will not work if the URL specified is wrong.

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

About the Author

Dhandapani Ammasai



India India

Member

Dhandapani Ammasai(Dan in short) is a software delivery manager at a top tier IT company in India.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPlease use MSXML2.XMLHTTP and MSXML2.DOMDocument PinmemberUmut Alev21:13 17 Dec '06  
GeneralRefresh PinmemberDavid Pfahl12:17 7 Dec '05  
GeneralAJAX.net PinmemberSean Rock23:47 1 Jul '05  
GeneralServer to client transfer Pinmembersundarbunny22:35 8 Jun '05  
GeneralRe: Server to client transfer PinmemberHazem Salem10:41 1 Nov '06  
Generaljavascript! PinsussAnonymous15:55 27 Sep '04  
GeneralRe: javascript! PinmemberAlex Korchemniy7:35 18 Dec '04  
QuestionXMLHTTP question? PinsussVeddy9:53 7 Oct '03  
Generalsubmit + iframes vs. xmlhttp PinsussNidi Iacobsohn2:27 1 Jun '03  
GeneralRe: submit + iframes vs. xmlhttp Pinmemberrobpinion19:39 21 Jul '03  
GeneralRe: submit + iframes vs. xmlhttp PinsussAnonymous1:36 13 Oct '05  
Generalxmlhttp Pinmemberarvindsha8:46 27 Nov '02  
Generalmissing value attribute Pinmember9key11:43 31 May '02  
GeneralGreat piece of code! PinmemberJulian Tysoe4:56 30 Jan '02  
I love this piece of code, much neater than refreshing the whole screen each time, or the javascript methods.
 
I have re-written the states.asp page using VBscript so that it is dynamic, you might find it useful. Dreamweaver did the connections and I removed the error handling for simplicities sake. You'll have to put in your own connection info. I have a table called country, with columns country and state. My query is basically "Select state from Country where country=Country":
 
<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="YOUR CONNECTION STRING" -->
<%
set ObjXML = CreateObject("Microsoft.XmlDom")
ObjXML.load Request
set Query = ObjXML.selectSingleNode("/RequestStates")
Country = Query.getAttribute("Country")
 
set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = CONNECTION STRING
Recordset1.Source = "{call YOUR QUERY('" + Replace(Country, "'", "''") + "')}"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 3
Recordset1.Open()
Recordset1_numRows = 0
 
Dim Repeat1__numRows
Repeat1__numRows = -1
Dim Repeat1__index
Repeat1__index = 0
Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
 
Dim responseXML
responseXML = "<Response>"
 
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))
     responseXML = responseXML + "<State>" + (Recordset1.Fields.Item("state").Value) + "</State>"
     Repeat1__index=Repeat1__index+1
     Repeat1__numRows=Repeat1__numRows-1
     Recordset1.MoveNext()
Wend
 
responseXML = responseXML + "</Response>"
response.write(responseXML)
Recordset1.Close()
ObjXML = null
%>
 
It's fairly trivial, but I thought it might save someone else re-inventing the wheel
 
www.tysoe.com
Generalforeing Characters PinmemberHector10:50 2 Nov '01  
GeneralRe: foreing Characters PinmemberSandro Araújo7:27 16 Nov '01  
QuestionClient requirements? PinmemberTom Archer2:35 22 May '01  
AnswerRe: Client requirements? PinmemberGerald6:25 22 May '01  
GeneralRe: Client requirements? PinmemberTom Archer6:47 22 May '01  
GeneralRe: Client requirements? PinmemberGerald7:00 22 May '01  
GeneralRe: Client requirements? PinmemberJason Gerard8:42 23 May '01  
GeneralRe: Client requirements? PinmemberDhandapani Ammasai8:46 23 May '01  
GeneralRe: Client requirements? PinmemberWhiteTrailerTrash5:11 23 Nov '06  
GeneralRe: Client requirements? PinmemberGary Menzel13:40 25 Jul '01  
GeneralRe: Client requirements? PinmemberMark Bruk15:46 5 Sep '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120528.1 | Last Updated 16 May 2001
Article Copyright 2001 by Dhandapani Ammasai
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid