1.建立一个aspx页面,html代码
<html xmlns=" http://www.w3.org/1999/xhtml " >
<head runat="servidor">
<title>Página sem título</title>
<script type="texto/javascript">
var xmlHttp;
função createXMLHttpRequest()
{
se (janela.ActiveXObject)
{
xmlHttp = novo ActiveXObject("Microsoft.XMLHTTP");
}
senão se (window.XMLHttpRequest)
{
xmlHttp = novo XMLHttpRequest();
}
}
função startRequest()
{
//depurador;
var ProvinceID=document.getElementById("DropDownList1");
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "?ProvinceID="+ProvinceID.value, true);
xmlHttp.send(nulo);
}
função handleStateChange()
{
if(xmlHttp.readyState == 4) //0(未初始化);1(正在装载);2 (装载完毕);3 (交互中);4 (完成)
{
if(xmlHttp.status == 200) //200(OK);404(não encontrado)
{
document.getElementById("gridiv").innerHTML=xmlHttp.responseText;
}
}
}
</script>
</head>
<corpo>
<form id="form1" runat="servidor">
<div>
<asp:DropDownList ID="DropDownList1" runat="servidor">
</asp:DropDownList>
</div>
<div id="gridiv"></div>
</form>
</body>
</html>2.cs代码
usando System.Data.SqlClient;
protegido void Page_Load (remetente do objeto, EventArgs e)
{
se (!Page.IsPostBack)
{
this.DropDownList1.Attributes.Add("onchange", "return startRequest();");
ListProvince();
if (ProvinceID! = "")
{
GetCityByProvinceID(ProvinceID);
}
}
}
propriedade#propriedade da região
string privada ProvinceID
{
pegar
{
if (Request["ProvinceID"] != null && Request["ProvinceID"].ToString() != "")
{
return Solicitação["ProvinceID"];
}
outro
{
retornar "";
}
}
}
#endregion
GetDataSet#region GetDataSet
DataSet privado GetDataSet (string sql)
{
string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda =new SqlDataAdapter(sql,constring);
DataSet ds=new DataSet();
sda.Fill(ds);
retornar ds;
}
#endregion
GetCityByProvinceID#region GetCityByProvinceID
privado vazio GetCityByProvinceID(string ProvinceID)
{
string connStr = ConfigurationSettings.AppSettings["ConnectionString"];
Conexão SqlConnection = new SqlConnection(connStr);
string sql = "selecione * da cidade onde pai ='" + ProvinceID + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
conexão.Open();
SqlDataReader dr = cmd.ExecuteReader();
string s = @"<table cellpacing='0' cellpadding='4' border='0' id='GridView1' style='color:#333333;border-collapse:collapse;'>";
s+="<tr style='color:White;background-color:#990000;font-weight:bold;'>";
s+="<th scope='col'>流水号</th><th scope='col'>代号</th><th scope='col'>城市</th></tr>";
interno m = 0;
enquanto (dr.Read ())
{
se (m % 2 == 0)
{
s += "<tr style='color:#333333;background-color:#FFFBD6;'>";
}
outro
{
s += "<tr style='color:#333333;background-color:White;'>";
}
m++;
s += "<td>" + dr["id"] + "</td>";
s += "<td>" + dr["cityID"] + "</td>";
s += "<td>" + dr["cidade"] + "</td>";
s += "</tr>";
}
s+="</table>";
dr.Fechar();
conexão.Fechar();
this.Response.Write(s);
this.Response.End();
}
#endregion
ListProvince#region ListProvince
privado vazio ListProvince()
{
string sql = "selecione * da província";
DataSetds = GetDataSet(sql);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "província";
DropDownList1.DataValueField = "provinceID";
DropDownList1.DataBind();
}
#endregion