- HTML code
<asp:DropDownList ID="DDLProvince" runat="server" CssClass="select" AutoPostBack="True" Height="22px" onselectedindexchanged="DDLProvince_SelectedIndexChanged" Width="150px"> </asp:DropDownList> <asp:DropDownList ID="DDLCity" runat="server" CssClass="select" CausesValidation="True" Height="19px" Width="150px"> </asp:DropDownList>
- C# code
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { RegionalInformation ri = new RegionalInformation(); string[][] allProvinces = ri.GetProvinces(); for (int i = 0; i < allProvinces.Length; i++) { ListItem lip = new ListItem(allProvinces[i][1], allProvinces[i][0]); DDLProvince.Items.Add(lip); } } } protected void DDLProvince_SelectedIndexChanged(object sender, EventArgs e) { RegionalInformation ri = new RegionalInformation(); string a = Convert.ToString(DDLProvince.SelectedIndex + 1); string[][] citiesOfProvince = ri.GetCitiesOfProvince(a); for (int j = 0; j < citiesOfProvince.Length; j++) { ListItem li = new ListItem(citiesOfProvince[j][1], citiesOfProvince[j][0]); DDLCity.Items.Add(li); } }
这是显示省市联动
我在第一次选择的时候,是正常的,但第二次选择的时候,第一次选择的内容还是在里面
就像下面图片那样
这是第一次选择的,是正常的
第二次选择,就成这样的了
------解决方案--------------------------------------------------------
citiesOfProvince.Items.Clear()
for (int j = 0; j < citiesOfProvince.Length; j++)
{
ListItem li = new ListItem(citiesOfProvince[j][1], citiesOfProvince[j][0]);
DDLCity.Items.Add(li);
}
添加之前先删除旧的
湖南的内容已经加上去了,在后面
------解决方案--------------------------------------------------------
每次绑定前先清空.
这个网上例子也比较多.有很多不同方法.楼主也可以看看