Ben2307
Ben2307

Reputation: 1012

dropdownlist added more list item after pick

I got a weird problem. I use a DropDownList item in a web application. after i check for the first time one of the options all the items are added to end of the list.
initialized like this

 protected void Page_Load(object sender, EventArgs e)
    {
        string myXMLfile = Server.MapPath("~/VMlist.xml");
        DataSet dsMachines = new DataSet();
        try
        {
            dsMachines.ReadXml(myXMLfile);
            DropDownList1.DataSource = dsMachines;
            DropDownList1.DataValueField = "machineID";
            DropDownList1.DataTextField = "machineName";
            DropDownList1.DataBind();

        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

and the SelectedIndexChanged:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        machineOS.Text = OSdata.OSDataRetrieve.getOSInfo(DropDownList1.SelectedItem.Text);

    }

i use XML initializing. I'm also adding an image of 'before' and 'after'image

Upvotes: 1

Views: 315

Answers (4)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

Because you had set the AppendDataBoundItems="true"

You have to set it to AppendDataBoundItems="false"

Every time the dropdown selected Index changed event is fired, your dropdownlist again binded and added datasource items again.

Upvotes: 1

EbbnFlow
EbbnFlow

Reputation: 663

My problem was that I manually called databind() but found that databind was already being called during because it was a nested databound control. Try commenting out your databind call and see if that fixes it.

Upvotes: 0

Steve Barron
Steve Barron

Reputation: 944

In page_load, you should probably check for IsPostback.

Currently, you are rebinding the list to the listbox on each load. It's not just adding 3 new items - there is a scrollbar and I suspect it is re-adding ALL of the items.

Upvotes: 0

James Johnson
James Johnson

Reputation: 46047

You need to add the following to your Page_Load:

if (!Page.IsPostBack)
{
    //code to bind dropdown
}

Your list is being bound each time the page posts back, which is why you're getting duplicates. Add the above line, and you should be all set.

Upvotes: 1

Related Questions