下拉菜单被清除

发布于 2024-10-03 18:44:32 字数 1559 浏览 1 评论 0原文

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

这是我在设计页面中的下拉列表:

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

        </asp:DropDownList>

绑定代码如下所示。

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

    }

提前致谢..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人间不值得 2024-10-10 18:44:32

从你的问题来看,如果我理解正确的话,你不希望下拉列表在填充后重新绑定。另请检查您的视图状态,这不应该发生,除非您禁用了视图状态

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

}

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

}

染墨丶若流云 2024-10-10 18:44:32

将下拉列表的 AppendDataBoundItems 属性设置为 true,这将允许您混合使用数据绑定项目和非数据绑定项目(否则插入语句将清除您的列表)

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

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

臻嫒无言 2024-10-10 18:44:32

您是否在页面上禁用了视图状态?由于您仅在第一次加载页面时将项目加载到下拉列表中,因此如果未启用视图状态,则回发后列表中将没有任何内容。

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.

如果没有 2024-10-10 18:44:32

不是积极的,但我在其他语言中看到过错误的解释...

您的产品值是 ToDecimal 的转换,这意味着例如 99.999。

如果您绑定的 ID 基于整数(即:整数),则绑定值将不匹配...即使 Value = 1 vs Value = 1.00,它也不会匹配并且不会被考虑与您的列表匹配的有效“值”。将您的答案转换为整数/整数,它可能会达到您的预期。

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.

败给现实 2024-10-10 18:44:32

在没有看到页面的完整源代码的情况下,我只是猜测,但是您是否在页面上禁用了 ViewState ?如果是这样,DropDownList 无法在回发之间保留其值,并且每次都必须重新加载列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文