简体   繁体   中英

DropDownList adding extra ListItems OnSelectedIndexChanged when changing from first Selection

I have a little issue with the WebApp I'm building. I have a DropDownList(productddl) which is populated from a DB. OnSelectedIndexChanged for the first dropdown I have a second DropDownList(versionddl) which is populated based on the SelectItem.Value of the first productddl.

After binding productddl I inserted a "Select Product" like so:

productddl.Items.Insert(0, new ListItem(" - Select Product - ", "0"));

The issue is that for example, I select a product the first time and if I try to change it after it adds a second " - Select Product - " to the dropdown. If I keep changing the product it keeps adding and adding.

How can I prevent this from happening? Any help will be really appreciated.

Below is how the productddl is being loaded/populated

if (!IsPostBack)
{
        productddl.DataSource = db_GetSet.isp_GetSingle("PRODTCKT", "", "");
        productddl.DataTextField = "pr_product";
        productddl.DataValueField = "pr_product";
        productddl.DataBind();
        productddl.Items.Insert(0, new ListItem(" - Select Product - ", "0"));
}

Below is the OnSelectedIndexChanged:

protected void getversion_OnSelect(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        versionddl.DataSource = db_GetSet.isp_GetMulti("VERDDLV", productddl.SelectedItem.Value, "");
        versionddl.DataTextField = "vr_version";
        versionddl.DataValueField = "vr_version";
        versionddl.DataBind();
        productddl.Items.Insert(0, new ListItem("Select Product", "0"));

    }
}

Remove productddl.Items.Insert(0, new ListItem("Select Product", "0")); in the OnSelectedIndexChanged method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM