简体   繁体   中英

dropdown selected index changed event fires while setting data from database in asp.net c#

I have dropdown inside update panel showing some list. When I select dropdown item, selected index changed event fires to populate other dropdown and data is saved and displayed in grid on submit. When I select grid row to edit, dropdown is set with database data. Now all things work fine. But when I select other control with postback, selected index changed event of that dropdown fires without selecting item and resets other dropdown with data to index 0. Why this event fires without selecting dropdown? Please help.

<asp:DropDownList ID="ddl" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="ddl_SelectedIndexChanged">
</asp:DropDownList>

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
     Fillddl();
}
public void Fillddl()
{
    try
    {
        DataSet ds_ddl = _BLL.get_AllItems(ddl.SelectedValue);
        if (ds_ddl != null)
        {
            if (ds_ddl .Tables[0].Rows.Count > 0)
            {
                ddl2.DataSource = ds_ddl.Tables[0].DefaultView;
                ddl2.DataTextField = "NAME";
                ddl2.DataValueField = "ID";
                ddl2.DataBind();
            }
        }
        ddl2.Items.Insert(0, new ListItem("Select", "0"));
        ddl2.Dispose();
    }
    catch { }
}

use

if(!Postback)
{
    // Insert your code
}

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