下拉被清除 [英] Dropdown gets cleared

查看:82
本文介绍了下拉被清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net应用程序,其中我有一个下拉这是绑定到数据集。但选择一个项目后,下拉被清除所有的价值,我们如何解决这个问题?

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>

和约束力code如下图所示。

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

推荐答案

从你的问题,如果我理解正确的话,你不想要的下拉列表中是否填充它重新绑定。也请检查您的视图状态,这不应该发生,除非你有禁用ViewState的

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

}

这篇关于下拉被清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆