开发者

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:Dr开发者_如何学运维opDownList>

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..


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();

}


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜