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.
我在一个UpdatePanel中有一个下拉列表,它是从SqlDataSource在回发中填充的。它有一个参数,这是另一个控件。我有时需要多个回发,但是每次更新面板刷新时,都会将项目添加到下拉列表中。所以下拉列表最终会有不正确的数据,或者重复的数据。
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#)
(这个下拉列表在一个asp.net 2.0的web应用程序中,而codebehind在c#中)
Thank you.
谢谢你!
8 个解决方案
#1
61
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'(这会导致您正在谈论的问题),对DropDownList的DataBound事件作出响应,然后将“blank”项添加到列表的顶部。
<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 -", ""));
}
#2
4
You probably bind that DropDownList in the code behind. So you should not do it after postback again:
您可能会在后面的代码中绑定下拉列表。所以你不应该在回邮后再这么做:
// probably in Page_Load method
if (!Page.IsPostBack)
{
// do data binding here
};
#3
3
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
首先,我们应该理解appenddatabounditem。如果AppendDataBoundItems =“true”,listitem将添加到下拉列表中,而不清除旧的列表。否则,在下一个DataBind之前,DropDownList将被清除。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中定义一个空白选项,并仅将数据库中的listitem添加到下拉列表中一次。
Notice 3 things here:
请注意三件事:
- Blank
ListItem
is defined in html - 空白ListItem是在html中定义的
AppendDataBoundItems="true"
- AppendDataBoundItems = " true "
-
DataBind
is NOT called on postbacks or when theDropDownList
item count is > 1 - 在回发或下拉列表项计数为> 1时,不会调用DataBind
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。清除并重新加载每个页面刷新上的下拉列表。
Notice 3 differences from the first option:
注意3与第一个选项的区别:
-
AppendDataBoundItems="false"
(if it is not defined thenfalse
is it's default value) - AppendDataBoundItems="false"(如果没有定义,则为默认值)
- Blank
ListItem
is is added in code behind. We can't define it in html because withAppendDataBoundItems="false"
, it would be cleared out. - 在后面的代码中添加了空白ListItem。我们不能在html中定义它,因为使用AppendDataBoundItems="false",它将被清除。
-
DataBind
is called on everyPage_Load
- 每个Page_Load都调用DataBind
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 -", ""));
}
#4
2
Here is an idea, we can use 2 events: DataBound and 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();
}
#5
1
<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>
#6
0
The code works, try to give it a value:
代码起作用了,试着给它一个值:
MyList.Items.Insert(0, new ListItem("- Select -", "0"));
#7
0
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.
的问候。
#8
0
Just Add EnableViewState="false" to the Dropdown tag
只需将EnableViewState="false"添加到下拉标签
<asp:DropDownList runat="server" ID="DropDownList1" DataSourceID="SqlDataSource"
DataTextField="Name" DataValueField="ID" EnableViewState="false"
AppendDataBoundItems="true">
<asp:ListItem Value="">Select</asp:ListItem>
</asp:DropDownList>
#1
61
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'(这会导致您正在谈论的问题),对DropDownList的DataBound事件作出响应,然后将“blank”项添加到列表的顶部。
<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 -", ""));
}
#2
4
You probably bind that DropDownList in the code behind. So you should not do it after postback again:
您可能会在后面的代码中绑定下拉列表。所以你不应该在回邮后再这么做:
// probably in Page_Load method
if (!Page.IsPostBack)
{
// do data binding here
};
#3
3
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
首先,我们应该理解appenddatabounditem。如果AppendDataBoundItems =“true”,listitem将添加到下拉列表中,而不清除旧的列表。否则,在下一个DataBind之前,DropDownList将被清除。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中定义一个空白选项,并仅将数据库中的listitem添加到下拉列表中一次。
Notice 3 things here:
请注意三件事:
- Blank
ListItem
is defined in html - 空白ListItem是在html中定义的
AppendDataBoundItems="true"
- AppendDataBoundItems = " true "
-
DataBind
is NOT called on postbacks or when theDropDownList
item count is > 1 - 在回发或下拉列表项计数为> 1时,不会调用DataBind
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。清除并重新加载每个页面刷新上的下拉列表。
Notice 3 differences from the first option:
注意3与第一个选项的区别:
-
AppendDataBoundItems="false"
(if it is not defined thenfalse
is it's default value) - AppendDataBoundItems="false"(如果没有定义,则为默认值)
- Blank
ListItem
is is added in code behind. We can't define it in html because withAppendDataBoundItems="false"
, it would be cleared out. - 在后面的代码中添加了空白ListItem。我们不能在html中定义它,因为使用AppendDataBoundItems="false",它将被清除。
-
DataBind
is called on everyPage_Load
- 每个Page_Load都调用DataBind
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 -", ""));
}
#4
2
Here is an idea, we can use 2 events: DataBound and 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();
}
#5
1
<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>
#6
0
The code works, try to give it a value:
代码起作用了,试着给它一个值:
MyList.Items.Insert(0, new ListItem("- Select -", "0"));
#7
0
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.
的问候。
#8
0
Just Add EnableViewState="false" to the Dropdown tag
只需将EnableViewState="false"添加到下拉标签
<asp:DropDownList runat="server" ID="DropDownList1" DataSourceID="SqlDataSource"
DataTextField="Name" DataValueField="ID" EnableViewState="false"
AppendDataBoundItems="true">
<asp:ListItem Value="">Select</asp:ListItem>
</asp:DropDownList>