How do you avoid duplicate items when a databound control gets bound twice?
How do you avoid duplicate bound items in this scenario:
There's a databound control on a page (a DropDownList in this case, though I don't think it matters). It has AppendDataBoundItems set to "true". Somewhere in the code, a DataSource is set and DataBind is called. So this control is bound explicitly.
You have a bunch of other things to bind on the page, so you call Page.DataBind.
Your databound control now has duplicate items in it. It was (1) bound explicitly, then (2) Page.DataBind bound it again. Since AppendDataBoundItems was true, the second bind appends to the first, and you end up with double the items.
A couple limitations:
The e开发者_运维技巧xplicit call to DataBind on the control is done through some centralized code, and would be painful to change.
I really need to bind the rest of the page in aggregate (via Page.Databind()) because there's too many other databound elements to do it individually without writing a ton of individual calls.
I need a method like... Page.DataBindExceptIfTheyHaveAlreadyBeenBoundDuh()
Turn off the AppendDataBoundItems after you do the Control.DataBind(). Then you Page.DataBind() won't append the items again.
DropDownList.DataSource = Data;
DropDownList.DataBind();
DropDownList.AppendDataBoundItems = false;
The bottom line is that there's no great solution. You need to architect your page so that they don't bind controls twice, which means re-writing parts of the page if necessary. The pain of this is better than the workarounds.
You can set AppendDataBoundItems="False"
to the DropDownList and in the DataBound event you can add and select a new listitem like here:
protected void ddlNeighborhoods_DataBound(object sender, EventArgs e)
{
ListItem li = new ListItem("- Select -", "-1", true);
li.Selected = true;
ddlCities.Items.Insert(0, li);
}
I have come across this issue many times in the past and the best solution that I have found that works in pretty much all the cases that I had; was to use a UNION select -1 as Value, '- Select -' as Text.
This worked even when you had grid within a grid and databound to a dropdown that was in a usercontrol within that inner grid.
Using code-behind to load the grid will not allow databinding to a usercontrol field.
I hope this tip helps...
Usually this happens in legacy code when you don't want to make too many changes that would break the rest of the page/control. In some cases with a dropdownlist you may have to clear out the items in the dropdownlist.databinding event as a quick fix.
protected void DropDownList1_DataBinding(object sender, EventArgs e)
{
if((sender as DropDownList).Items.Count > 0)
{
(sender as DropDownList).Items.Clear();
}
}
note: You should be careful doing this if the items values change.
I've faced this in asp.net retrieving values from mysql database. On autopostback the values were being added to the already existing values in the dropdownlist.
solution: i had an sqlcommand to first count the number of rows concerned in the database table. And stored the value in Intvariable1
Then declared another intVariable. had
session("intvariable")=Dropdownlist1.items.count
while session("intvariable") > session("intvariable1")
Dropdownlist1.items.removeAt(Session("intvariable")-1)
session("intvariable)-=1
End while
'this worked for me
精彩评论