ASP.NET DropDownList SelectedValue is persisted which results in exception if the control is DataBound second time - by Stilgar

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 666808 Comments
Status Closed Workarounds
Type Bug Repros 4
Opened 5/5/2011 12:24:51 AM
Access Restriction Public


There is a simple page with a single DropDownList. If the control is databound and a SelectedValue is set programatically and then the control is databound again with datasource that does not contain the same value an exception is thrown.

There is a simple page with only a DropDownList and disabled ViewState

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="TestWebApplication.WebForm2" ViewStateMode="Disabled" %>

<html lang="en" >
    <form id="form1" runat="server">
        <asp:DropDownList ID="ddlTest" runat="server">

The code behind:

using System;

namespace TestWebApplication
    public partial class WebForm2 : System.Web.UI.Page
        protected void Page_PreRender(object sender, EventArgs e)
            //SelectedIndex is -1, SelectedValue is "", SelectedItem is null
            ddlTest.DataSource = new[] { 1, 2, 3 };
            ddlTest.SelectedValue = "3";
            //SelectedIndex is 2, SelectedValue is "3", SelectedItem is {3}

            /* if you uncomment this block you still get the exception although the values can change slightly on the ddlTest.Items.Clear row
            //SelectedIndex is 0, SelectedValue is "1", SelectedItem is {1}
            ddlTest.SelectedIndex = -1; //Nothing changes including SelectedIndex
            ddlTest.SelectedValue = ""; //Nothing changes including SelectedValue
            //SelectedIndex is -1, SelectedValue is "", SelectedItem is null
            ddlTest.DataSource = null; //Nothing changes except for the DataSource property
            ddlTest.DataSource = new[] { 1, 2 };
            //'ddlTest' has a SelectedValue which is invalid because it does not exist in the list of items.
            //Parameter name: value

Of course in the real world case I bind in different events (event handlers, Page_Init) and have some more complex logic for what to databind to but the bug is reproduceable in this simple scenario.

If the selected value is cleared it should not be persisted internally and the databinding to the new datasource should be successful.
Sign in to post a comment.
Posted by Damian [MSFT] on 9/11/2011 at 12:23 AM
The issue is caused by the fact that calling ClearSelection() is not enough to clear the internally cached values of the selected item. Instead of calling ClearSelection(), do the following before calling DataBind() again:

ddlTest.SelectedValue = null; // This is key as it clears the cached selected value

Once doing this, the sample provided works.
Posted by BeenaJain on 6/30/2011 at 3:24 PM
I am facing exactly the same problem and looking for solution.
Posted by Macy [MSFT] on 5/5/2011 at 4:51 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 5/5/2011 at 1:14 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(