DropDownList AppendDataBoundItems (first item to b

2019-01-10 22:52发布

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#)

Thank you.

8条回答
地球回转人心会变
2楼-- · 2019-01-10 23:00

The code works, try to give it a value:

MyList.Items.Insert(0, new ListItem("- Select -", "0"));
查看更多
老娘就宠你
3楼-- · 2019-01-10 23:08
<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>
查看更多
forever°为你锁心
4楼-- · 2019-01-10 23:09

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 -", ""));
}
查看更多
Ridiculous、
5楼-- · 2019-01-10 23:10

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

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.

Notice 3 things here:

  • Blank ListItem is defined in html
  • AppendDataBoundItems="true"
  • DataBind is NOT called on postbacks or when the DropDownList item count is > 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.

OR (option to use IsPostBack instead)

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.

Notice 3 differences from the first option:

  • AppendDataBoundItems="false" (if it is not defined then false is it's default value)
  • Blank ListItem is is added in code behind. We can't define it in html because with AppendDataBoundItems="false", it would be cleared out.
  • DataBind is called on every 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 -", ""));
}
查看更多
太酷不给撩
6楼-- · 2019-01-10 23:20

Just Add EnableViewState="false" to the Dropdown tag

<asp:DropDownList runat="server" ID="DropDownList1" DataSourceID="SqlDataSource" 
DataTextField="Name" DataValueField="ID" EnableViewState="false" 
AppendDataBoundItems="true">
    <asp:ListItem Value="">Select</asp:ListItem>
</asp:DropDownList>
查看更多
Deceive 欺骗
7楼-- · 2019-01-10 23:22

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();
}
查看更多
登录 后发表回答