62,268
社区成员
发帖
与我相关
我的任务
分享
<script language="JavaScript">
<!--
function createXMLHttps()
{
var ret = null;
try
{
ret = new ActiveXObject('Msxml2.XMLHTTP');
}
catch(e)
{
try
{
ret = new ActiveXObject('Microsoft.XMLHTTP');
}
catch(ee)
{
ret = null;
}
}
if (!ret && typeof XMLHttpRequest != 'undefined')
ret = new XMLHttpRequest();
return ret;
}
//获取城市数据
function XmlCityPost(obj)
{
var svalue = obj.value;
document.getElementById("ddlCounty").length= 0;
document.getElementById("ddlCounty").options.add(new Option("请选择县区","0"));
if(svalue == "0")
{
document.getElementById("ddlCity").length= 0;
document.getElementById("ddlCity").options.add(new Option("请选择城市","0"));
}
else
{
var webFileUrl = "?povinceid=" + svalue;
var result = "";
var newxmlhttp = createXMLHttps();
newxmlhttp.open("POST", webFileUrl, false);
newxmlhttp.send("");
result = newxmlhttp.responseText;
if(result != "")
{
document.getElementById("ddlCity").length=0;
document.getElementById("ddlCity").options.add(new Option("请选择城市","0"));
var piArray = result.split(",");
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
document.getElementById("ddlCity").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
else
{
document.getElementById("ddlCity").length= 0;
document.getElementById("ddlCity").options.add(new Option("请选择城市","0"));
}
}
}
//获取县区数据
function XmlCountyPost(obj)
{
var svalue = obj.value;
if(svalue == "0")
{
document.getElementById("ddlCounty").length= 0;
document.getElementById("ddlCounty").options.add(new Option("请选择县区","0"));
}
else
{
var webFileUrl = "?cityid=" + svalue;
var result = "";
var newxmlhttp = createXMLHttps();
newxmlhttp.open("POST", webFileUrl, false);
newxmlhttp.send("");
result = newxmlhttp.responseText;
if(result != "")
{
document.getElementById("ddlCounty").length=0;
var piArray = result.split(",");
document.getElementById("ddlCounty").options.add(new Option("请选择县区","0"));
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
document.getElementById("ddlCounty").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
else
{
document.getElementById("ddlCounty").length= 0;
document.getElementById("ddlCounty").options.add(new Option("请选择县区","0"));
}
}
}
//-->
</script>
<asp:DropDownList ID="ddlProvince" runat="server">
<asp:ListItem Text="请选择省份" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlCity" runat="server">
<asp:ListItem Text="请选择城市" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlCounty" runat="server">
<asp:ListItem Text="请选择县区" Value="0"></asp:ListItem>
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
province_bind();
ddlProvince.Attributes.Add("onchange", "XmlCityPost(this);");
ddlCity.Attributes.Add("onchange", "XmlCountyPost(this);");
}
if (povinceid != "")
{
city_bind(povinceid);
}
if (cityid != "")
{
county_bind(cityid);
}
}
public static DataSet GetDataSet(string sql)
{
Page page = new Page();
string ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + page.Server.MapPath("area.mdb");
OleDbDataAdapter oda = new OleDbDataAdapter(sql, ConnectionString);
DataSet ds = new DataSet();
oda.Fill(ds);
return ds;
}
private string povinceid
{
get
{
if (ViewState["povinceid"] != null && ViewState["povinceid"].ToString() != "")
{
return ViewState["povinceid"].ToString();
}
else
{
if (Request["povinceid"] != null && Request["povinceid"].ToString() != "")
{
return Request["povinceid"];
}
else
{
return "";
}
}
}
set
{
ViewState["povinceid"] = value;
}
}
private string cityid
{
get
{
if (ViewState["cityid"] != null && ViewState["cityid"].ToString() != "")
{
return ViewState["cityid"].ToString();
}
else
{
if (Request["cityid"] != null && Request["cityid"].ToString() != "")
{
return Request["cityid"];
}
else
{
return "";
}
}
}
set
{
ViewState["povinceid"] = value;
}
}
/// <summary>
/// 获取城市数据
/// </summary>
/// <param name="id">省份ID</param>
private void city_bind(string id)
{
string mystr = "";
string sql = "select cityID,city from city where father = '" + id + "'";
DataSet ds = GetDataSet(sql);
if (ds.Tables[0].Rows.Count != 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
mystr += "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
}
mystr = mystr.Substring(1);
}
Response.Write(mystr);
Response.End();
}
/// <summary>
/// 获取县区数据
/// </summary>
/// <param name="id">城市ID</param>
private void county_bind(string id)
{
string mystr = "";
string sql = "select areaID,area from area where father = '" + id + "'";
DataSet ds = GetDataSet(sql);
if (ds.Tables[0].Rows.Count != 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
mystr += "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
}
mystr = mystr.Substring(1);
}
Response.Write(mystr);
Response.End();
}
/// <summary>
/// 省份数据绑定
/// </summary>
private void province_bind()
{
string sql = "select provinceID,province from povince";
DataSet ds = GetDataSet(sql);
ddlProvince.AppendDataBoundItems = true;
ddlProvince.DataSource = ds;
ddlProvince.DataValueField = "provinceID";
ddlProvince.DataTextField = "province";
ddlProvince.DataBind();
}