方法1:
Index.aspx:
Index.aspx.cs:
protected string provinceID;
private void Page_Load(object sender, System.EventArgs e)
{
//接收参数
provinceID = this.Request.QueryString["provinceID"];
//判断参数是否为空(注意:空有两种形式,一种为null,一种为空)
if (provinceID + "a" != "a")
{
//如果有传递上述参数,则表示联动操作开始
this.xmlBind(provinceID);
}
//在页面初次加载的时候,绑定第一/二个下拉框,第二个下拉框为所有值
//但实际上,第二个下拉框应显示空值,因为所有值可能也不少,最好只显示一个"请选择"字样
if(!this.IsPostBack)
{
this.DownBind1();
this.DownBind2();
}
}
///
/// 返回第2个下拉框需要的值给xmlhttp
///
/// 传递的关键ID值
private void xmlBind(string provinceID)
{
string mystr = "";
string sql = "select city,cityID from city where father = " + provinceID;
DataTable mytab = ConnObject.Get_Dt(sql);
//将取到的值形成: ID|名称,ID|名称...这样的形式
if (mytab.Rows.Count != 0)
{
for (int i = 0; i < mytab.Rows.Count; i++)
{
mystr += "," + mytab.Rows[i]["cityID"].ToString() + "|" + mytab.Rows[i]["city"].ToString();
}
mystr = mystr.Substring(1);
}
//输出页面
this.Response.Write(mystr);
this.Response.End();
}
///
/// 绑定第一个下拉框
///
private void DownBind1()
{
//显示所有的主分类
string sql = "select provinceID,province from province order by provinceID asc ";
DataTable mytab = ConnObject.Get_Dt(sql);
//绑定第一个下拉框
this.ddlPro.DataSource = mytab;
this.ddlPro.DataValueField = "provinceID";
this.ddlPro.DataTextField = "province";
this.ddlPro.DataBind();
//添加一个"请选择"行
this.ddlPro.Items.Insert(0, new ListItem("请选择省份", ""));
//为此下拉框添加一个默认选择项(此处默认为sortid = 1
//做选项时,如果你添加的选定项而此控件中却没有此项,即会出错
//如:this.mydown1.SelectedValue = "1";
//所以此处以如下方式进行选定
ListItem myItem = this.ddlPro.Items.FindByValue("1");
if (myItem != null)
{
myItem.Selected = true;
}
//为此下拉框添加选择事件,第一个参数是自己,第二个参数为要填充的下拉框的名称
this.ddlPro.Attributes.Add("onchange", "XmlPost(this,'" + this.ddlCity.ClientID + "');");
}
///
/// 绑定第二个下拉框
///
private void DownBind2()
{
//默认显示分类号为1的所有子类
string sql = "select father,city,cityID from city where father =1";
DataTable mytab = ConnObject.Get_Dt(sql);
//绑牢控件
this.ddlCity.DataSource = mytab;
this.ddlCity.DataSource = mytab;
this.ddlCity.DataValueField = "cityID";
this.ddlCity.DataTextField = "city";
this.ddlCity.DataBind();
//添加一个空的首行
//this.ddlCity.Items.Insert(0, new ListItem("请选择名称", ""));
}
方法2:
Register.aspx