结贴快:看一下为什么DropDownList不能正确选中默认值?
恰好 2013-05-10 03:24:53 前台
<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的却只是列出来数据列表。