Page 5 of 6               << Previous  1  2  3  4  5  6  Next >>

 

AJAX (Asynchronous JavaScript And XML) Tutorial

AJAX Example - Server Web Page

The server page is the page that is called by AJAX to perform the actual processing - converting a state abbreviation to the full name of the state, in our case. The server page does not have any GUI elements since it just returns data.

Because the server page used in this example was written in ASP.NET V2003, it only contains the following HTML:

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="AjaxServerPage.aspx.vb" 
               Inherits="TheScarms.AjaxServerPage" smartNavigation="False"%>

Using ASP.NET V2005, the page would look like:

    <%@ Page Language="vb" AutoEventWireup="true" CodeFile="AJAXServerPage.aspx.vb" 
               Inherits="ServerPage" %>

Here is the Visual Basic code in the page's Page_Load event:

        Dim GotValue As Boolean = False
        Dim returnValue As String = ""

        If Not Request("val") Is Nothing AndAlso Request("val").Length > 0 Then
            Dim inputValue As String = Request("val").ToString()

            Select Case inputValue.ToUpper()
                Case "C"
                    returnValue = "Need 2nd letter"
                Case "D"
                    returnValue = "Delaware"
                Case "F"
                    returnValue = "Florida"
                Case "H"
                    returnValue = "Hawaii"
                Case "N"
                    returnValue = "Need 2nd letter"
                Case "M"
                    returnValue = "Maine"
                Case "CA"
                    returnValue = "California"
                Case "CT"
                    returnValue = "Connecticut"
                Case "DE"
                    returnValue = "Delaware"
                Case "FL"
                    returnValue = "Florida"
                Case "HI"
                    returnValue = "Hawaii"
                Case "NJ"
                    returnValue = "New Jersey"
                Case "NY"
                    returnValue = "New York"
                Case "ME"
                    returnValue = "Maine"
                Case Else
                    returnValue = "Unknown"
            End Select

            GotValue = True
        End If

        If GotValue Then
            Response.Clear()
            Response.ContentType = "text/xml"
            Response.Write(returnValue)
            Response.End()
        Else
            Response.Clear()
            Response.End()
        End If

First the state abbreviation entered thus far by the user is extracted from the URL via the Request object. Then, the state abbreviation is decoded based on whether one or two letters have been typed so far. The logic is nothing to write home about but it works for this purpose.

When the state name is known, it is time to return it to the client. The Response abbreviation entered thus far by the user is extracted from the URL via The Response object's Clear method is called to remove all output from the server's buffer. Next the ContentType property is set to denote the HTTP Mime type of the data.

The Response.Write call is what adds the data, state name, to the HTTP output buffer. The Response.End statement sends all currently buffered output to the client and stops execution of the page.


Page 5 of 6               << Previous  1  2  3  4  5  6  Next >>




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page