C# DropDownList AppendDataBoundItems(第一项为空且无重复项)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/730555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DropDownList AppendDataBoundItems (first item to be blank and no duplicates)
提问by zohair
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.
我有一个DropDownList
在UpdatePanel
从SqlDataSource
. 它有一个参数,它是另一个控件。我有时需要多次回发,但是每次更新面板刷新时,都会将项目添加到DropDownList
. 所以DropDownList
最终会得到不正确的数据或重复的数据。
I have the AppendDataBoundItems
property set to true
because I need the first item to be blank.
我将AppendDataBoundItems
属性设置为true
因为我需要第一个项目为空。
How can I overcome this problem? Is there another way to have a blank first item?
我怎样才能克服这个问题?还有另一种方法可以让第一项空白吗?
(This DropDownList
is in an ASP.NET 2.0 web app, and codebehind is in C#)
(这DropDownList
是在 ASP.NET 2.0 Web 应用程序中,代码隐藏在 C# 中)
采纳答案by Keltex
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.
而不是使用AppendDataboundItems='true'
(这将导致您正在谈论的问题),响应DataBound
事件DropDownList
,然后将您的“空白”项目添加到列表顶部。
<asp:DropDownList runat="server" ID="MyList"
ondatabound="MyListDataBound"></asp:DropDownList>
Then in your code behind:
然后在你后面的代码中:
protected void MyListDataBound(object sender, EventArgs e)
{
MyList.Items.Insert(0, new ListItem("- Select -", ""));
}
回答by gius
You probably bind that DropDownList in the code behind. So you should not do it after postback again:
您可能在后面的代码中绑定了 DropDownList。所以你不应该在再次回发后这样做:
// probably in Page_Load method
if (!Page.IsPostBack)
{
// do data binding here
};
回答by bagher
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server"
DataSourceID="SqlDataSource1" DataTextField="state" DataValueField="state">
<asp:ListItem Text="(Select a State)" Value="" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT DISTINCT [state] FROM [authors]">
</asp:SqlDataSource>
回答by Miguel
Here's an Idea.
这是一个想法。
There's a property in the drop down list called AutoPostBack
set it to true and then in the code behind you put all the binding method inside the if(!Page.IsPostBack)
. That worked for me.
下拉列表中有一个名为AutoPostBack
将其设置为 true的属性,然后在后面的代码中将所有绑定方法放在if(!Page.IsPostBack)
. 那对我有用。
regards.
问候。
回答by Terminator
Here is an idea, we can use 2 events: DataBoundand DataBinding:
这是一个想法,我们可以使用 2 个事件:DataBound和DataBinding:
protected void MyListDataBound(object sender, EventArgs e)
{
MyList.Items.Insert(0, new ListItem("- Select -", ""));
}
protected void MyListDataBinding(object sender, EventArgs e)
{
MyList.Items.Items.Clear();
}
回答by Tony L.
There are good answers here but I felt the need to include more information because there are multiple options that work and we need to decide which to use.
这里有很好的答案,但我觉得有必要包含更多信息,因为有多种可行的选择,我们需要决定使用哪个。
First, we should understand AppendDataBoundItems
. If AppendDataBoundItems = "true"
, ListItems
are added to the DropDownList
without clearing out the old ones. Otherwise, the DropDownList
is cleared about before the next DataBind
. MSDN AppendDataBoundItems doc
首先,我们应该了解AppendDataBoundItems
。如果AppendDataBoundItems = "true"
,ListItems
被添加到DropDownList
而不清除旧的。否则,DropDownList
将在下一个 之前清除DataBind
。MSDN AppendDataBoundItems 文档
There are basically 2 options covered by most of the answers:
大多数答案基本上涵盖了两个选项:
1. Define a blank option in html and add the ListItems from the database to the DropDownList only once.
1.在html中定义一个空白选项,并且只将数据库中的ListItems添加到DropDownList中一次。
Notice 3 things here:
请注意这里的 3 件事:
- Blank
ListItem
is defined in html AppendDataBoundItems="true"
DataBind
is NOT called on postbacks or when theDropDownList
item count is > 1
- 空白
ListItem
在 html 中定义 AppendDataBoundItems="true"
DataBind
不会在回发或DropDownList
项目计数 > 1时调用
Source:
来源:
<asp:DropDownList ID="MyList" runat="server" AppendDataBoundItems="true" DataValueField="Id" DataTextField="Name" >
<asp:ListItem Text="- Select One -" Value="" />
</asp:DropDownList>
Code behind:
后面的代码:
protected void Page_Load(object sender, System.EventArgs e)
{
if (MyList.Items.Count <= 1 ) {
MyList.DataSource = MyDataSource;
MyList.DataBind();
}
}
Note: I like the logic of checking the count vs checking IsPostBack
. Though PostBacks are often the cause of duplicate databinding, it is possible to cause it other ways. Checking the item count is basically just checking to see if it's already been loaded.
注意:我喜欢检查计数与检查的逻辑IsPostBack
。尽管回发通常是导致重复数据绑定的原因,但也可能以其他方式导致它。检查项目计数基本上只是检查它是否已经加载。
OR (option to use IsPostBack
instead)
或(IsPostBack
替代使用的选项)
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack) {
MyList.DataSource = MyDataSource;
MyList.DataBind();
}
}
2. Clear and reload the DropDownList on each page refresh.
2. 在每次页面刷新时清除并重新加载 DropDownList。
Notice 3 differences from the first option:
请注意与第一个选项的 3 个不同之处:
AppendDataBoundItems="false"
(if it is not defined thenfalse
is it's default value)- Blank
ListItem
is is added in code behind. We can't define it in html because withAppendDataBoundItems="false"
, it would be cleared out. DataBind
is called on everyPage_Load
AppendDataBoundItems="false"
(如果未定义,false
则为默认值)- 空白
ListItem
是在后面的代码中添加的。我们不能在 html 中定义它,因为使用AppendDataBoundItems="false"
,它会被清除。 DataBind
被称为每个Page_Load
Source:
来源:
<asp:DropDownList ID="MyList" runat="server" DataValueField="Id" DataTextField="Name"
OnDataBound="MyList_DataBound" >
</asp:DropDownList>
Code behind:
后面的代码:
protected void Page_Load(object sender, System.EventArgs e)
{
MyList.DataSource = MyDataSource;
MyList.DataBind();
}
protected void MyList_DataBound(object sender, EventArgs e)
{
MyList.Items.Insert(0, new ListItem("- Select One -", ""));
}