ropDownList的AppendDataBoundItems

ropDownList的AppendDataBoundItems

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

问题描述

我有一个的DropDownList 内的的UpdatePanel 是填充从回发的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 属性设置为真正,因为我需要的第一项为空白。

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应用程序和codebehind是在C#)

(This DropDownList is in an asp.net-2.0 web app, and codebehind is in c#)

感谢您。

推荐答案

而不是使用 AppendDataboundItems =真正的(这将导致该问题你正在谈论)应对数据绑定事件的的DropDownList ,然后添加你的空白项添加到列表的顶端。

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>

然后在你的code背后:

Then in your code behind:

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

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

08-26 04:40