第一项为空且无重复项

第一项为空且无重复项

本文介绍了DropDownList AppendDataBoundItems(第一项为空且无重复项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UpdatePanel 中有一个 DropDownList,它在从 SqlDataSource 回发时填充.它有一个参数,它是另一个控件.我有时需要多次回发,但每次更新面板刷新时,都会将项目添加到 DropDownList.所以 DropDownList 最终会有不正确的数据,或者重复的数据.

I have a DropDownList inside an UpdatePanel that is populated on postback from a SqlDataSource. It has a parameter which is another control. I sometimes need multiple postbacks, but what happens is that each time the update panel refreshes, items are added to the DropDownList. So the DropDownList ends up having data that is incorrect, or repeated data.

我将 AppendDataBoundItems 属性设置为 true 因为我需要第一个项目为空.

I have the AppendDataBoundItems property set to true because I need the first item to be blank.

我怎样才能克服这个问题?有没有其他方法可以让第一项空白?

How can I overcome this problem? Is there another way to have a blank first item?

(此 DropDownList 在 ASP.NET 2.0 Web 应用程序中,代码隐藏在 C# 中)

(This DropDownList is in an ASP.NET 2.0 web app, and codebehind is in C#)

推荐答案

而不是使用 AppendDataboundItems='true'(这会导致您正在谈论的问题),响应 DropDownList 的 DataBound 事件,然后将空白"项添加到列表顶部.

Instead of using AppendDataboundItems='true' (which will cause the problem you are talking about), respond to the DataBound event for the DropDownList and then add your "blank" item to the top of the list.

<asp:DropDownList runat="server" ID="MyList"
  ondatabound="MyListDataBound"></asp:DropDownList>

然后在你的代码后面:

protected void MyListDataBound(object sender, EventArgs e)
{
    MyList.Items.Insert(0, new ListItem("- Select -", ""));
}

这篇关于DropDownList AppendDataBoundItems(第一项为空且无重复项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:56