我在文章列表页中按文章类别显示文章.
用这个方法
    protected void bindTypeList(string classid)
    {
        OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["Data2ConnectionString"].ConnectionString);
        conn.Open();
        OleDbCommand cmd = new OleDbCommand("select * from ZD_ArticleType order by listid", conn);
        OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        int typeleval;
        string tt = "";
        // string id = Request.QueryString["id"];        while (dr.Read())
        {
            typeleval = Convert.ToInt32(dr["typelevel"]);            for (int i = 0; i < typeleval; i++)
            {
                tt += "─";
                //DropDownList1.Items.Add(dr["title"]);
            }
            string itemT = tt + "┣" + dr["title"];
            ListItem item = new ListItem(itemT, dr["id"].ToString());
            //if (classid == dr["id"].ToString())
            //{
            //    DropDownList1.SelectedValue = classid ;
            //}
            DropDownList1.Items.Add(item);            tt = "";        }
        conn.Close();
        if (classid!=null)
        {
            DropDownList1.SelectedValue = classid;
        }
    }
可是我第一次打开页面DropDownList1中的正常.我再一选别的类别.可以调也相应的记录.但是DropDownList1中的项增加了一倍.我在Page_Load()
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindTypeList(0, "");
            bindDatas();
        }
        else
        {
        DropDownList1.Items.Clear();
        bindTypeList(DropDownList1.SelectedValue);
        bindDatas();
        }
    }
加上回传清空DropDownList1.Items.Clear();
就会报错.不知道为什么?应当如何解决?望高手指点.

解决方案 »

  1.   

    没提交一次都会执行一次Page_Load(),调用一次bindTypeList()就会增加数据
      

  2.   

    绑定前都加上,这个也IsPostBack
    DropDownList1.Items.clear();
      

  3.   

    麻烦回答详细点.
    DropDownList1.Items.clear();加到哪里都不行.
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //DropDownList1.Items.Clear();
                bindTypeList("");
                bindDatas();
            }
            else
            {
            //DropDownList1.Items.Clear();
            bindTypeList(DropDownList1.SelectedValue);
            bindDatas();
            }
        }
      

  4.   

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindTypeList(0, "");
                bindDatas();
            }
            else
            {
            string strtemp=DropDownList1.SelectedValue;
            DropDownList1.Items.Clear();
            bindTypeList(strtemp);
            bindDatas();
            }
        }