我是C#开发的

protected void Page_Load(object sender, EventArgs e)
函数里面,我想让 网页把数据库里面的东西的读出来,放在 dropdownlist的item里面
        string sqlCommand = "select distinct 型号 from  mydb";
        OleDbDataAdapter cmd = new OleDbDataAdapter(sqlCommand, conn);  
        DataSet ds = new DataSet();  
        cmd.Fill(ds,"mydb");  
        this.DropDownList1.DataSource = ds.Tables["mydb"].DefaultView;
        DropDownList1.DataTextField = "型号";
        DropDownList1.DataBind();
但是,每次选中,dropdownlist的内容的时候,触发事件以后,页面就会重读page_load函数
导致dropdwonlist里面的东西重置
我应该怎么办呢?

解决方案 »

  1.   

    对啊你加上
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!isPostBack)
       {
            string sqlCommand = "select distinct 型号 from mydb";
            OleDbDataAdapter cmd = new OleDbDataAdapter(sqlCommand, conn);   
            DataSet ds = new DataSet();   
            cmd.Fill(ds,"mydb");   
            this.DropDownList1.DataSource = ds.Tables["mydb"].DefaultView;
            DropDownList1.DataTextField = "型号";
            DropDownList1.DataBind();   }
    }
      

  2.   

    或者直接在前面用sqldatasource,这个没必要在后台写代码