想实现省、市、地区的DropdownList的无刷三联动,代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="CHEDAI.Web.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register TagPrefix="anthem" Namespace ="Anthem" Assembly ="Anthem"%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<anthem:DropDownList ID="DropDownList1" runat="server" AutoCallBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"></anthem:DropDownList>
<anthem:DropDownList ID="DropDownList2" runat="server" AutoCallBack="true"
EnabledDuringCallBack="false"
onselectedindexchanged="DropDownList2_SelectedIndexChanged"></anthem:DropDownList>
<anthem:DropDownList ID="DropDownList3" runat="server" AutoCallBack="true" EnabledDuringCallBack="false"></anthem:DropDownList>
</div>
</form>
</body>
</html>
后台代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem list = new ListItem();
list.Text = "--请选择--";
list.Value = "";
//进行数据绑定
DropDownList1.Items.Add(new ListItem("浙江省", "1"));
DropDownList1.Items.Add(new ListItem("江西省", "2"));
//在第一个位置添加"--请选择--"
DropDownList1.Items.Insert(0, list);
DropDownList2.Items.Insert(0, list);
DropDownList3.Items.Insert(0, list);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListItem list = new ListItem();
list.Text = "--请选择--";
list.Value = "";
//如果DropDownList1的value值为空,代表没有选择任何的省份.
if (DropDownList1.SelectedValue != "")
{
DropDownList2.Items.Clear();
DropDownList2.Items.Add(new ListItem("杭州市","1"));
DropDownList2.Items.Add(new ListItem("宁波市", "1"));
DropDownList2.Items.Add(new ListItem("嘉兴市", "1"));
DropDownList2.UpdateAfterCallBack = true;//刷新DropDownList2,这是Anthem组件里面控件的一个属性,单独刷新该控件.
DropDownList2.Items.Insert(0, list);//为第一个位置添加--请选择--
}
else
{
DropDownList2.Items.Clear();//清除DropDownList2里面的值.
DropDownList2.Items.Insert(0, list);
DropDownList2.UpdateAfterCallBack = true;
DropDownList3.Items.Clear();
DropDownList3.Items.Insert(0, list);
DropDownList3.UpdateAfterCallBack = true;
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)