前台
<table >
<tr><td>
省<asp:DropDownList ID="sheng" runat="server"></asp:DropDownList>市 <asp:DropDownList ID="shi" runat="server"></asp:DropDownList></td></tr>
</table>
后台cs
...
shiname= rs["shi"].ToString(); 从数据库读出的当前值
.... public void BindDrop1()
{
string sSql = "select shi from MyCity ";
DataSet ds = new DataSet();
ds = conn.ReturnDS(sSql);
sheng.Items.Clear();
sheng.DataSource = ds.Tables[0];
sheng.DataTextField = "shi";
sheng.DataValueField ="shi";
sheng.DataBind();
sheng.SelectedIndex = sheng.Items.IndexOf(sheng.Items.FindByText(shiname));
}
public void BindDrop2()
{
string sSql = "select shi from MyCity";
DataSet ds = new DataSet();
ds = conn.ReturnDS(sSql);
shi.Items.Clear();
shi.DataSource = ds.Tables[0];
shi.DataTextField = "shi";
shi.DataValueField = "shi";
shi.DataBind();
shi.SelectedIndex = shi.Items.IndexOf(shi.Items.FindByText(shiname));
}从数据库中读取省,市,将前台下拉列表中自动选中,为了测试,我让两段程序完全一样,但只有ID=shi能正确选中当前值,ID=sheng的却只是列出来数据列表。
<table >
<tr><td>
省<asp:DropDownList ID="sheng" runat="server"></asp:DropDownList>市 <asp:DropDownList ID="shi" runat="server"></asp:DropDownList></td></tr>
</table>
后台cs
...
shiname= rs["shi"].ToString(); 从数据库读出的当前值
.... public void BindDrop1()
{
string sSql = "select shi from MyCity ";
DataSet ds = new DataSet();
ds = conn.ReturnDS(sSql);
sheng.Items.Clear();
sheng.DataSource = ds.Tables[0];
sheng.DataTextField = "shi";
sheng.DataValueField ="shi";
sheng.DataBind();
sheng.SelectedIndex = sheng.Items.IndexOf(sheng.Items.FindByText(shiname));
}
public void BindDrop2()
{
string sSql = "select shi from MyCity";
DataSet ds = new DataSet();
ds = conn.ReturnDS(sSql);
shi.Items.Clear();
shi.DataSource = ds.Tables[0];
shi.DataTextField = "shi";
shi.DataValueField = "shi";
shi.DataBind();
shi.SelectedIndex = shi.Items.IndexOf(shi.Items.FindByText(shiname));
}从数据库中读取省,市,将前台下拉列表中自动选中,为了测试,我让两段程序完全一样,但只有ID=shi能正确选中当前值,ID=sheng的却只是列出来数据列表。
然后在sheng的sheng_SelectedIndexChanged事件里 通过sheng选择的值查询相关联的市 并绑定到shi 上
初始值是由数据绑定根据数据的ID号,读取相关数据库得到的,如shiname= rs["shi"].ToString();比如:得到 shiname="东城区", 但前台,只有 第二个shi的列表,正确的选中了,第一个只是列出了全部地区
http://www.cnblogs.com/insus/archive/2011/07/04/2097059.htmlhttp://www.cnblogs.com/insus/archive/2012/10/16/2725307.html
我现在是因为,从数据库读到的sheng的值,不能正确在 前台被选中,所以才测试让两个一样