Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

c# - DropDownList AppendDataBoundItems (first item to be blank and no duplicates)

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.

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 DropDownList is in an ASP.NET 2.0 web app, and codebehind is in C#)

question from:https://stackoverflow.com/questions/730555/dropdownlist-appenddatabounditems-first-item-to-be-blank-and-no-duplicates

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

Then in your code behind:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...