views:

4210

answers:

8

Hi,

I have a drop down list inside an update panel that is populated on postback from a sql data source. 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 dropdown list ends up having data that is incorrect, or repeated data.

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?

(This dropdown is in an asp.net2.0 web app, and codebehind is in c#)

Thank you.

+8  A: 

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

<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 -", ""));
}
Keltex
Thanks .. !! It helped
msbyuva
A: 

Doesn't work for me :( in ASP 3.5.

A: 

The code works, try to give it a value:

MyList.Items.Insert(0, new ListItem("- Select -", "0"));
A: 

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
};
gius
A: 


" SelectCommand="SELECT DISTINCT [state] FROM [authors]">

bagher
A: 
bagher
A: 

Here's an Idea.

there's a property in the dropdown 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.

regards.

Miguel