开发者

dropdownlist added more list item after pick

开发者 https://www.devze.com 2023-03-27 23:36 出处:网络
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.

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 dsMa开发者_JAVA百科chines = 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'

dropdownlist added more list item after pick


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消