Ajax real-time local refresh

Source: Internet
Author: User

Ajax for Local Refresh


<script type= "Text/javascript" >
var xmlhttp;
function GetData ()
{
Get the name that the user fills in
var City=document.getelementbyid ("TXT"). Value;
To create an asynchronous call object
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
Associate an object's state with an event
Xmlhttp.onreadystatechange=statechange;
Load the page you want to link to
xmlHTTP. Open ("POST", "datapage.aspx?city=" +city,true);
Send Request
xmlHTTP. Send ();
}
function StateChange ()
{
To determine whether an asynchronous call has been completed
if (xmlhttp.readystate==4)
{
Determines whether the completed prompt code is an OK state
if (xmlhttp.status==200)
{
Pass the returned data as a parameter to the Fill method
Filldata (Xmlhttp.responsetext);
}
}
}
function Filldata (strcity)
{
document.getElementById ("DropDownList1"). options.length=0;
var indexofcity;
VAR City;
The string to be passed by cutting
while (strcity.length>0)
{
Determine if it is the last string
Indexofcity=strcity.indexof (",");
if (indexofcity >0)
{
City=strcity.substring (0,indexofcity);
Strcity=strcity.substring (indexofcity+1);
Fill drop-down box
document.getElementById ("DropDownList1"). Add (New Option (city,city));
}
Else
{
If this is the last string
Lastcity=strcity.substring (0,2);
document.getElementById ("DropDownList1"). Add (New Option (lastcity,lastcity));
Break
}
};
}
</script>

A second page
protected void Page_Load (object sender, EventArgs e)
{
Get the arguments passed over
String City =request.querystring[' city ';
Response.Clear ();
Judging the city name
Switch (city)
{
Case "Beijing":
Fill in related areas
Response.Write ("Chaoyang, Haidian, Dongcheng, Xicheng");
Break
Case "Shanghai":
Response.Write ("Pudong, Jingan, Hongkou, Xuhui");
Break
Case "Jinan":
Response.Write ("Lixia, Licheng, City, overpass");
Break
}
}



-------------------------------------------------------------------------------using callback techniques for local refreshes
<script type= "Text/javascript" >
function Filldata ()
{
var City=document.getelementbyid ("TextBox1"). Value;

<% =this. Clientscript.getcallbackeventreference (This, "City", "Filldll", null)%>;
}
function Filldll (strcity)
{
document.getElementById ("DropDownList1"). options.length=0;
var indexofcity;
VAR City;
The string to be passed by cutting
while (strcity.length>0)
{
Determine if it is the last string
Indexofcity=strcity.indexof (",");
if (indexofcity >0)
{
City=strcity.substring (0,indexofcity);
Strcity=strcity.substring (indexofcity+1);
Fill drop-down box
document.getElementById ("DropDownList1"). Add (New Option (city,city));
}
Else
{
If this is the last string
document.getElementById ("DropDownList1"). Add (New Option (strcity,strcity));
Break
}
};
}
</script>


private string _data;
public string GetCallbackResult ()
{
Returns the processed data
return _data;
}

public void RaiseCallbackEvent (String eventargument)
{
Determine the parameters passed over
Switch (eventargument)
{
Case "Beijing":
_data = "Chaoyang, Haidian, Dongcheng, Xicheng";
Break
Case "Shanghai":
_data = "Pudong, Jingan, Xuhui, Hongkou";
Break
Case "Jinan":
_data = "Licheng, Lixia, City, overpass";
Break
}
}


-------------------------------------------------------------------------------------iframe for local refresh
<script language= "JavaScript" >
function Search ()
{
var City=document.getelementbyid ("TextBox1"). Value;
if (City! = "")
{
document.getElementById ("Iframe1"). src= "myframe.aspx?city=" +city;
}
}
</script>
<iframe src= "myframe.aspx" style= "Text-align:center" id= "iframe1" width= "100%" height= "100%" frameborder= "0" scrolling= "No"/>

Second page myframe.aspx

<div style= "Text-align:center" >
<asp:dropdownlist id= "DropDownList1" runat= "Server" width= "154px" >
</asp:DropDownList></div>
protected void Page_Load (object sender, EventArgs e)
{
Get the arguments passed over
String city = request.querystring["City"];
Judging the city name
Switch (city)
{
Case "Beijing":
Fill in related areas
DropDownList1.Items.Clear ();
DROPDOWNLIST1.ITEMS.ADD ("Chaoyang");
DROPDOWNLIST1.ITEMS.ADD ("Haidian");
DROPDOWNLIST1.ITEMS.ADD ("Dongcheng");
DROPDOWNLIST1.ITEMS.ADD ("Xicheng");
Break
Case "Shanghai":
DropDownList1.Items.Clear ();
DROPDOWNLIST1.ITEMS.ADD ("Pudong");
DROPDOWNLIST1.ITEMS.ADD ("Jingan");
DROPDOWNLIST1.ITEMS.ADD ("Hongkou");
DROPDOWNLIST1.ITEMS.ADD ("Xuhui");
Break
Case "Jinan":
DropDownList1.Items.Clear ();
DROPDOWNLIST1.ITEMS.ADD ("Lixia");
DROPDOWNLIST1.ITEMS.ADD ("Licheng");
DROPDOWNLIST1.ITEMS.ADD ("In the City");
DROPDOWNLIST1.ITEMS.ADD ("flyover");
Break
}
}


---------------------------------------------------------------------------------using scripting methods for local refreshes
<script type= "Text/javascript" >
function Filldata (strcity)
{
document.getElementById ("DropDownList1"). options.length=0;
var indexofcity;
VAR City;
The string to be passed by cutting
while (strcity.length>0)
{
Determine if it is the last string
Indexofcity=strcity.indexof (",");
if (indexofcity >0)
{
City=strcity.substring (0,indexofcity);
Strcity=strcity.substring (indexofcity+1);
Fill drop-down box
document.getElementById ("DropDownList1"). Add (New Option (city,city));
}
Else
{
If this is the last string
document.getElementById ("DropDownList1"). Add (New Option (strcity,strcity));
Break
}
};
}
</script>



Using System.Text;
protected void Page_Load (object sender, EventArgs e)
{
Creating a String Connection object
StringBuilder myscript = new StringBuilder ();
Using strings to organize a JavaScript scripting method
MyScript. Append ("function seekcity () {\ n");
MyScript. Append ("var City=document.getelementbyid (' TextBox1 ')." VALUE; \ n ");
MyScript. Append ("switch (city) {\ n");
MyScript. Append ("Case ' Beijing ': \ n");
MyScript. Append ("Filldata" + getcitystr ("Beijing") + "'); \ n ");
MyScript. Append ("break; \ n ");
MyScript. Append ("Case ' Shanghai ': \ n");
MyScript. Append ("Filldata" + getcitystr ("Shanghai") + "'); \ n ");
MyScript. Append ("break; \ n ");
MyScript. Append ("Case ' Jinan ': \ n");
MyScript. Append ("Filldata" + getcitystr ("Jinan") + "'); \ n ");
MyScript. Append ("break;} \ n ");
MyScript. Append ("}\n");
Using the Register script method on the page of the client, register the script method written by this string.
Page.ClientScript.RegisterClientScriptBlock (typeof (String), "Seekcity", MyScript. ToString (), true);

}
Returns the city's county string by getting the city name
private string Getcitystr (String incity)
{
String City= "";
Switch (incity)
{
Case "Beijing":
City = "Chaoyang, Haidian, Dongcheng, Xicheng";
Break
Case "Shanghai":
City = "Pudong, Jingan, Xuhui, Hongkou";
Break
Case "Jinan":
City = "Licheng, Lixia, Urban, overpass";
Break
}
Returns a string connection containing counties
return city;
}

Ajax real-time local refresh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.