为什么
if(!this.IsPostBack)
{
string strCmd="select * from Area where Level=1";
string DB="Area";
string strColumn="Province";
ddl_Province.Items.Clear();
ddl_City.Items.Clear();
ddl_Region.Items.Clear();
LoadData_ddl(strCmd,DB);
ddl_Province.DataSource=LoadArray(DB,strColumn);
ddl_Province.DataBind();
ddl_Province.Items.Add(Convert.ToString(Session["myProvince"]));
ddl_Province.SelectedIndex=ds.Tables[DB].Rows.Count;
strCmd="select * from Area where Province='"+Convert.ToString(Session["myProvince"])+"' and Level=2";
strColumn="City";
LoadData_ddl(strCmd,DB);
ddl_City.DataSource=LoadArray(DB,strColumn);
ddl_City.DataBind();
ddl_City.Items.Add(Convert.ToString(Session["myCity"]));
ddl_City.SelectedIndex=ds.Tables[DB].Rows.Count;
strCmd="select * from Area where Province='"+Convert.ToString(Session["myProvince"])+"' and City='"+Convert.ToString(Session["myCity"])+"' and Level=3";
strColumn="Region";
LoadData_ddl(strCmd,DB);
ddl_Region.DataSource=LoadArray(DB,strColumn);
ddl_Region.DataBind();
ddl_Region.Items.Add("不限");
ddl_Region.SelectedIndex=ds.Tables[DB].Rows.Count;
}
private void LoadData_ddl(string strcmd,string DataBase)
{
string strCmd=strcmd;
string DB=DataBase;
SqlConnection conn=new SqlConnection();
SqlCommand cmd=new SqlCommand();
conn.ConnectionString="server=(local);database=searchhouse;user id=lb;password=lb";
cmd.Connection=conn;
cmd.CommandText=strCmd;
da.SelectCommand=cmd;
da.Fill(ds,DB);
conn.Close();
conn.Dispose();
}
private string[] LoadArray(string DB,string strColumn)
{
int num=ds.Tables[DB].Rows.Count;
string[] strDDL=new string[num];
for(int i=0;i<num;i++)
{
strDDL[i]=ds.Tables[DB].Rows[i][strColumn].ToString();
}
return strDDL;
}
数据库格式
id province city region level
1 辽宁 NULL NULL 1
2 辽宁 大连 NULL 2
3 辽宁 大连 中山区 3
4 辽宁 大连 甘井子区 3
5 辽宁 沈阳 NULL 2
显示在DDL控件里的数据总是读取正确的数据加上他上一及的数据
比如:select * from area where province='session["***"]' and city='session["***"]' and level=3
在DDL里应该显示ID是4 , 5
但是读出来的数据是ID 3 ,4 ,5的数据
本来我以为只是首页的是这样,但后来我在别的页上也出现这个问题,是不是代码有问题???