Dropdown gets cleared

I have one asp.net application, in which i have one dropdown which is binded to dataset. But after selecting one item, the drop down gets cleared all the value, How we can resolve this issue?

This is my dropdown list in design page:

<asp:DropDownList ID="ddlProduct" runat="server" CssClass="textEntry" Width="300px"
            AutoPostBack="True" OnSelectedIndexChanged="ddlProduct_SelectedIndexChanged">

        </asp:DropDownList>

and binding code is shown below.

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindProductDdl();
    }

    private void BindProductDdl()
    {
        Products objProducts = new Products();
        dsProducts dsProduct = new dsProducts();
        ListItem olst = default(ListItem);
        olst = new ListItem(" Select", "0");
        dsProduct = objProducts.GetDataset("");            
        ddlProduct.DataSource = dsProduct;
        ddlProduct.DataTextField = "Product";
        ddlProduct.DataValueField = "Id";
        ddlProduct.DataBind();
        ddlProduct.Items.Insert(0, olst);
    }

 protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        Products objProducts = new Products();
        dsProducts dsProduct = new dsProducts();
        string criteria = "";

        if (ddlProduct.SelectedItem.Text != " Select")
        {
            string id = ddlProduct.SelectedItem.Value;
            criteria = "Id='" + id + "'";
            dsProduct = objProducts.GetDataset(criteria);
            productValue = Convert.ToDecimal(dsProduct.tblProducts.Rows[0]["Value"].ToString());
        }

    }

Thanks in advance..

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

From your question if I understand correctly you dont want the dropdown list to rebind if it is populated. Also please check your viewstate, this should not be happening, unless you have disabled viewstate

 protected void Page_Load(object sender, EventArgs e)
{        
  if (!IsPostBack && ddlProduct.Items.count <=0 )
        BindProductDdl();

}

Method 2

Set the AppendDataBoundItems property of the dropdown to true and this will allow you to have a mix of databound items and non databound items (otherwise that insert statement is clearing your list)

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx

Method 3

Do you have viewstate disabled on the page? Since you are only loading the items into the dropdownlist on the first load of the page, if viewstate is not enabled there will be nothing in the list after the postback.

Method 4

Not positive, but I’ve seen in other languages and false interpretation…

You have your product Value as convert of ToDecimal which implies 99.999 for example.

If your ID that you are binding to is based on a whole number (ie: Integer basis), the bound value won’t match… even if Value = 1 vs Value = 1.00 it won’t match and will not be considered a valid “value” that matches your list. Convert your answer to a whole/integer number and it might do what you expect.

Method 5

Without seeing the full source for the page I am simply speculating, but have you disabled ViewState on the page? If so, the DropDownList cannot retain its values between postbacks and the lists will have to be reloaded each time.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x