Search code examples
c#asp.netwebformsdropdown

duplication in dropdown item ASP.net


I have problem with DropDownList in ASP.NET 1, 2.

Each time I select an item, the items in the list are repeated 3.

It appears fine the first time of selection only.

my code C#

protected void Page_Load(object sender, EventArgs e)
{
    CRUD myCrud = new CRUD();
    string mySql = (@" my query here   ");
    SqlDataReader dr = myCrud.getDrPassSql(mySql);
    DropDownList1.DataSource = dr;
    DropDownList1.DataTextField = "type";
    DropDownList1.DataValueField = "id";
    DropDownList1.DataSource = dr;
    DropDownList1.DataBind();

    CRUD myCrud2 = new CRUD();
    string mySql2 = (@" my another query based on the type selected above  ");
    SqlDataReader dr2 = myCrud2.getDrPassSql(mySql2);
    DropDownList3.DataSource = dr2;
    DropDownList3.DataTextField = "NameEN";
    DropDownList3.DataSource = dr2;
    DropDownList3.DataBind();
}

Solution

  • As suggested in comments:

       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // your above page load code goes here
            }
        }