ajax无刷新省市区三级联动菜单
1、GetCity.aspx?pid=
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Regex re=new Regex("^[0-9]{1,2}$");
Response.Clear();
if(!re.IsMatch(Request.QueryString["pid"]))
{
Response.End();
}
else
{
DataBase db=new DataBase("ConHouse");
//SqlCommand cmd=new SqlCommand("select name from city where pid=@pid order by id FOR XML AUTO,Elements");
SqlCommand cmd=new SqlCommand("select name from city where pid=@pid order by id FOR XML AUTO,Elements");
Response.ContentType = "text/xml";
Response.Output.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
Response.Output.WriteLine("<citys>");
cmd.Parameters.Add("@pid",SqlDbType.TinyInt).Value=Request.QueryString["pid"];
db.Open();
//System.Xml.XmlReader xr=db.ExecuteCmd4XmlReader(cmd);
SqlDataReader xr=db.ExecuteCmd4Reader(cmd);
System.Text.StringBuilder s=new System.Text.StringBuilder(128);
while(xr.Read())
{
s.Append(xr.GetString(0));
}
xr.Close();
db.Close();
Response.Output.WriteLine(s.ToString());
Response.Output.WriteLine("</citys>");
Response.End();
}
}
2、GetZone.aspx?pid=&cid=
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Regex re=new Regex("^[0-9]{1,2}$");
Response.Clear();
if(!re.IsMatch(Request.QueryString["pid"])
!re.IsMatch(Request.QueryString["cid"]))
{
Response.End();
}
else
{
DataBase db=new DataBase("ConHouse");
//SqlCommand cmd=new SqlCommand("select name from city where pid=@pid order by id FOR XML AUTO,Elements");
SqlCommand cmd=new SqlCommand("select name from zone where pid=@pid and cid=@cid order by id FOR XML AUTO,Elements");
Response.ContentType = "text/xml";
Response.Output.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
Response.Output.WriteLine("<zones>");
cmd.Parameters.Add("@pid",SqlDbType.TinyInt).Value=Request.QueryString["pid"];
cmd.Parameters.Add("@cid",SqlDbType.TinyInt).Value=Request.QueryString["cid"];
db.Open();
//System.Xml.XmlReader xr=db.ExecuteCmd4XmlReader(cmd);
SqlDataReader xr=db.ExecuteCmd4Reader(cmd);
System.Text.StringBuilder s=new System.Text.StringBuilder(128);
while(xr.Read())
{
s.Append(xr.GetString(0));
}
xr.Close();
db.Close();
Response.Output.WriteLine(s.ToString());
Response.Output.WriteLine("</zones>");
Response.End();
}
}
3、由于用javascript在客户端改变了下拉框的值后服务器端不认帐,故应加几个hidden控件来保存状态
<INPUT id="province" type="hidden" name="province" runat="server">
<INPUT id="province" type="hidden" name="city" runat="server">
<INPUT id="province" type="hidden" name="zone" runat="server">
并在load时初始化:
this.province.Value=this.ddlProvince.SelectedIndex.ToString();
this.city.Value=this.ddlCity.SelectedIndex.ToString();
this.zone.Value=this.ddlZone.SelectedIndex.ToString();