Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default18.aspx.cs" Inherits="Default18" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
try
{ //from IE old version. below version 6.0
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}catch(e)
{
try
{ //from IE.version 6.0 /7.0
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e2)
{ //from firefox
xmlHttp=new XMLHttpRequest();
}
}
}
function startRequest()
{
createXMLHttpRequest();
var selectoptions=document.all("rblOption");
var selectvalue;
for(i=0;i<selectoptions.length;i++)
{
if(selectoptions[i].checked)
{
selectvalue=selectoptions[i].value;
}
}
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "Handler.ashx?para=" +selectvalue, true);
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readyState == 4) //4 return data success
{
if(xmlHttp.status == 200) //200(OK);404(not found)
{
var blist=document.getElementById("ddlData");
blist.length=0;
var rs = xmlHttp.responseXML.childNodes[1];
var ss=rs.getElementsByTagName("student");
for(var i=0;i<ss.length;i++)
{
var option = document.createElement("OPTION");
option.text = ss[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
option.value = ss[i].getElementsByTagName("sex")[0].childNodes[0].nodeValue;
blist.options.add(option);
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:RadioButtonList ID="rblOption" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList ID="ddlData" runat="server"></asp:DropDownList>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default18 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.rblOption.Attributes.Add("onclick", "startRequest();");
}
}
}
Generic Handler
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/xml";
context.Response.Write("<?xml version='1.0' encoding='utf-8' ?><root><student><name>" + context.Request["para"] + "</name><sex>M</sex></student><student><name>wah2</name><sex>M</sex></student></root>");
}
public bool IsReusable {
get {
return false;
}
}
}