我的DropDownList中原有三项内容,刷新屏幕后却出现了六项内容,后三项是前三项的重复,这个问题该如何解决?

解决方案 »

  1.   

    private void Page_Load(object sender, System.EventArgs e)
    {
                if(!this.IsPostBack)//加这句
              {
                this.down1_bind();
              }
    }
    private void down1_bind()
    {
              string sql = "select provinceID,province from povince";
              DataSet ds = GetDataSet(sql);
              this.DropDownList1.DataSource = ds;
              this.DropDownList1.DataValueField = "provinceID";
              this.DropDownList1.DataTextField = "province";
              this.DropDownList1.DataBind();          
    }
      

  2.   


    if(!this.IsPostBack)
    {     
      
    }
    里写代码
      

  3.   

    一楼的说得对啊
    可能是你是用ADD()方式绑定的,那样的话你要在第二次绑定的时候用CLEAR()清除第一次的内容
      

  4.   

    那就在绑定前清空,加这句
    drpStatus.Items.Clear();
    如private void Page_Load(object sender, System.EventArgs e)
    {
                if(!this.IsPostBack)//加这句
              {
                    DropDownList1.Items.Clear();//加这句
                this.DropDownList1.DataSource = ds;
                  this.DropDownList1.DataValueField = "provinceID";
                  this.DropDownList1.DataTextField = "province";
                  this.DropDownList1.DataBind();
              }
    }
      

  5.   

    我应该在哪里写你所说的clear()?我的确用的是ADO的方式绑定数据的。
      

  6.   

    我应该在哪里写你所说的clear()?
    ---------------
    上面不是说了吗?加在你绑定的语句之前.
      

  7.   

    clear()语句加了也没有,真的,我试过了。
      

  8.   

    就是楼上 singlepine(小山) ( ) 
    说的,在绑定前清空
      

  9.   

    DropDownList1.Items.Clear();
    在绑定之前清空
      

  10.   

    DropDownList1.Items.Clear();
    这样应该可以的
      

  11.   

    if(!IsPostBack)
    {
    }
    或结合DropDownList1.Items.Clear();
    就能解决问题。
      

  12.   

    如果数据库里是重复记录,在sql语句里加上distinct关键字
    如果不是数据的问题private void Page_Load(object sender, System.EventArgs e)
    {
                if(!this.IsPostBack)//加这句
              {
                this.DropDownList1.DataSource = ds;
                  this.DropDownList1.DataValueField = "provinceID";
                  this.DropDownList1.DataTextField = "province";
                  this.DropDownList1.DataBind();
              }
    }
    就可以了不用clear
      

  13.   

    在前台.aspx的文件在绑定前用js清空一次
      

  14.   

    绑定写在“!IsPostBack”里,就不会出现重复值的。
      

  15.   

    是不是在其他事件里,你又调用了帮定DropDownList控件的方法。也就是说你重复帮定了DropDownList
      

  16.   

    我单步跟踪了一下,clear()函数执行的,而且是先clear后band,但结果还是数据有重复。
    我把代码贴出来,各位帮我看看吧:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace TopFounder
    {
    /// <summary>
    /// QC_Accessory_RMA 的摘要说明。
    /// </summary>
    public class QC_Accessory_RMA : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button3;
    protected System.Web.UI.WebControls.Button Button2;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Label Label8;
    protected System.Web.UI.WebControls.Label Label7;
    protected System.Web.UI.WebControls.TextBox TextBox3;
    protected System.Web.UI.WebControls.Label Label5;
    protected System.Web.UI.WebControls.TextBox TextBox2;
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.Label Label4;
    protected System.Web.UI.WebControls.DropDownList DropDownList2;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.DropDownList DropDownList4;
    protected System.Web.UI.WebControls.Label Label6;
    protected System.Web.UI.WebControls.Label Label9;
    protected System.Web.UI.WebControls.TextBox TextBox4;
    protected System.Web.UI.WebControls.DropDownList DropDownList3;

    //家里的连接
    private static SqlConnection myConnection = new SqlConnection("server=LUKE;uid = sa; pwd = ;database=TopFounder_QC");
    //公司连接
    //private static SqlConnection myConnection = new SqlConnection("server=HEXIAOJUN;uid = sa; pwd = ;database=TopFounder_QC");
    private static DataSet myDataSet = new DataSet();

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    TextBox1.Text = System.DateTime.Now.ToString();
    if (!this.IsPostBack)
    {
    this.DropDownList1.Items.Clear();
    this.DropDownList2.Items.Clear();
    this.DropDownList3.Items.Clear();
    this.DropDownList4.Items.Clear();
    BindDataGrid();
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DropDownList4.SelectedIndexChanged += new System.EventHandler(this.DropDownList4_SelectedIndexChanged);
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    private void BindDataGrid()
    {
    string selectionString_Users  = "Select *  from Users";
    string selectionString_AccessoryMainTypes = "select * from AccessoryMainTypes"; // SqlCommand myCommand = new SqlCommand(selectionString_Users, myConnection);
    SqlCommand myCommand2 = new SqlCommand(selectionString_AccessoryMainTypes, myConnection); 
    SqlDataAdapter myAdapter = new SqlDataAdapter(selectionString_Users, myConnection);
    SqlDataAdapter myAdapter2 = new SqlDataAdapter(myCommand2);

    myAdapter.Fill(myDataSet, "Users");
    myAdapter2.Fill(myDataSet,"AccessoryMainTypes"); DataView myView = myDataSet.Tables[0].DefaultView;
    DataView myView2 = myDataSet.Tables[1].DefaultView; this.DropDownList1.DataSource = myView;
    this.DropDownList1.DataTextField = "EmployeeName";
    this.DropDownList1.DataBind(); this.DropDownList2.DataSource = myView;
    this.DropDownList2.DataTextField = "EmployeeName";
    this.DropDownList2.DataBind();

    this.DropDownList4.DataSource = myView2;
    this.DropDownList4.DataTextField = "AccessoryName";
    this.DropDownList4.DataValueField = "AccessoryId";
    this.DropDownList4.DataBind();
    string id = DropDownList4.SelectedValue;

    string selectionString_AccessorySubTypes="select * from AccessorySubTypes where AccessoryId='"+id+"'";
    //DataSet dsf=new DataSet(); SqlDataAdapter myAdapter3 = new SqlDataAdapter(selectionString_AccessorySubTypes, myConnection);
    myAdapter3.Fill(myDataSet,"AccessorySubTypes");
    DataView myView3 =myDataSet.Tables[2].DefaultView; this.DropDownList3.DataSource=myView3;
    this.DropDownList3.DataTextField="AccessoryType";
    this.DropDownList3.DataValueField="AccessoryId";
    this.DropDownList3.DataBind();
    }
    private void DropDownList4_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    if(DropDownList4.SelectedIndex!=-1)
    {
    string id = DropDownList4.SelectedValue;

    string sql="select * from AccessorySubTypes where AccessoryId='"+id+"'";
    DataSet dsf=new DataSet(); SqlDataAdapter myAdapter3 = new SqlDataAdapter(sql, myConnection);
    myAdapter3.Fill(dsf,"AccessorySubTypes");
    DataView myView3 =dsf.Tables[0].DefaultView; this.DropDownList3.DataSource=dsf;
    this.DropDownList3.DataTextField="AccessoryType";
    this.DropDownList3.DataValueField="AccessoryId";
    this.DropDownList3.DataBind();
            }
    } private void Button2_Click(object sender, System.EventArgs e)
    {
    this.TextBox2.Text = "";
    this.TextBox3.Text = "";
    this.TextBox4.Text = "";
    } private void Button1_Click(object sender, System.EventArgs e)
    {
    //家里的连接
    SqlConnection myConnection = new SqlConnection("server=LUKE;uid = sa; pwd = ;database=TopFounder_QC");
    //公司连接
    //SqlConnection myConnection = new SqlConnection("server=HEXIAOJUN;uid = sa; pwd = ;database=TopFounder_QC");
    string InsertString_QCAccessoriesPassDeatails = "insert into QC_Accessories_RMA_Details values(@EmployeeName,@PMName,@QC_Date,@AccessoryType,@AccessorySN,@QC_Number,@Re)";
    SqlCommand myCommand = new SqlCommand(InsertString_QCAccessoriesPassDeatails, myConnection);

    myCommand.Parameters.Add(new SqlParameter ("@EmployeeName", SqlDbType.Char, 10));
    myCommand.Parameters["@EmployeeName"].Value = DropDownList1.SelectedItem.Value;
    myCommand.Parameters.Add(new SqlParameter("@PMName", SqlDbType.Char, 10));
    myCommand.Parameters["@PMName"].Value = DropDownList2.SelectedItem.Value;
    myCommand.Parameters.Add(new SqlParameter("@QC_Date", SqlDbType.DateTime,8));
    myCommand.Parameters["@QC_Date"].Value = TextBox1.Text;
    myCommand.Parameters.Add(new SqlParameter("@AccessoryType", SqlDbType.Char, 50));
    myCommand.Parameters["@AccessoryType"].Value = DropDownList3.SelectedItem.Text;
    myCommand.Parameters.Add(new SqlParameter("@AccessorySN", SqlDbType.Char, 20));
    myCommand.Parameters["@AccessorySN"].Value = TextBox3.Text;
    myCommand.Parameters.Add(new SqlParameter("@QC_Number", SqlDbType.Char, 20));
    myCommand.Parameters["@QC_Number"].Value = TextBox2.Text;
    myCommand.Parameters.Add(new SqlParameter("@Re", SqlDbType.VarChar, 200));
    myCommand.Parameters["@Re"].Value = this.TextBox4.Text; myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
    this.TextBox2.Text = "";
    this.TextBox3.Text = "";
    this.TextBox4.Text = ""; }
    }
    }
      

  17.   

    数据库结构是:
    drop database TopFounder_QC;
    gocreate database TopFounder_QC;
    gouse TopFounder_QC;
    go
    create table Departments
    (
    DepartmentNumber char(4) primary key 
    ,Department char(20) not null
    )
    gocreate table Users
    (
    --EmployeeNumber char(4) not null
    EmployeeNumber char(4) primary key
    ,EmployeeName char(10) not null
    ,Pwd char(8) not null
    ,DepartmentNumber char(4) foreign key references Departments(DepartmentNumber) 
    --,constraint prim_Users primary key(EmployeeNumber, EmployeeName)
    )
    gocreate table AccessoryMainTypes
    (
    AccessoryId char(4) primary key
    ,AccessoryName char(50) not null
    ,AccessoryMark char(10)
    )
    gocreate table AccessorySubTypes
    (
    AccessoryNumber char(4) primary key
    ,AccessoryId char(4) foreign key references AccessoryMainTypes(AccessoryId)

    ,AccessoryType char(50) not null
    )
    gocreate table QC_Accessories_Pass_Details
    ( EmployeeName char(10) not null
    ,PMName char(10) not null
    --,constraint foreignUser_QAP foreign key (EmployeeNumber,EmployeeName) references Users(EmployeeNumber,EmployeeName)
    ,QC_Date datetime
    ,AccessoryType char(50) not null
    ,AccessorySN char(20) null
    ,QC_Number char(20)

    )
    gocreate table QC_Accessories_RMA_Details
    ( EmployeeName char(10) not null
    ,PMName char(10) not null
    --,constraint foreignUser_QAR foreign key (EmployeeNumber,EmployeeName) references Users(EmployeeNumber,EmployeeName)
    ,QC_Date datetime
    ,AccessoryType char(50) not null
    ,AccessorySN char(20) null
    ,QC_Number char(20) null
    ,Re varchar(200)
    )
    gocreate table Product_Types
    (
    ProductTypeId char(4) primary key
    ,ProductType char(20) not null 
    )
    gocreate table QC_Products_Pass_Details
    (
    EmployeeName char(10) not null
    ,PMName char(10) not null
    --,constraint foreignUser_QPP foreign key (EmployeeNumber,EmployeeName) references Users(EmployeeNumber,EmployeeName)
    ,QC_Date datetime
    ,ProductTypeId char(4) foreign key references Product_Types(ProductTypeId)
    ,ProductSN char(20) 
    )
    gocreate table QC_Products_RMA_Details
    (
    EmployeeName char(10) not null
    ,PMName char(10) not null
    --,constraint foreignUser_QPR foreign key (EmployeeNumber,EmployeeName) references Users(EmployeeNumber,EmployeeName)
    ,QC_Date datetime
    ,ProductTypeId char(4) foreign key references Product_Types(ProductTypeId)
    ,ProductSN char(20) 
    ,Re varchar(200)
    )
    go
      

  18.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    //if(Session["username"]==null||Session["userpass"]==null)
    //Response.Redirect("show_pic_error.aspx");
    if(!IsPostBack)
    {
    OleDbConnection myConn = new OleDbConnection() ;
    string MyConnString=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=";
    MyConnString+=Server.MapPath("..\\showpic_data\\show_pic_tttick.mdb");
    myConn.ConnectionString=MyConnString;
    myConn.Open();
    string strCom="select * from show_pic_class";
    OleDbCommand countCommand = new OleDbCommand(strCom,myConn);
    OleDbDataReader selreader=countCommand.ExecuteReader();

    while(selreader.Read())
    {
    DropDownList1.Items.Add(selreader["show_pic_class"].ToString());
    }
    myConn.Close();
    }

    // 在此处放置用户代码以初始化页面
    }
    之前做一个项目的时候也遇到了。。我这样就解决了。。
      

  19.   

    selreader.Read()是什么东东啊?