ASP.NET Web Forms DropDownList has a SelectedValue which is invalid because it does not exist in the list of items

前端 未结 6 912
孤独总比滥情好
孤独总比滥情好 2021-02-02 15:05

First of all there has been questions ( DropDownList has a SelectedValue which is invalid because it does not exist in the list of items , DropDownList "has a SelectedValue

6条回答
  •  北海茫月
    2021-02-02 15:46

    1) As written, your example actually throws an exception on ddlTest.SelectedValue = "";, since there is no item in the collection with a Value of "". I think this would be expected under any circumstance.

    2) I get an exception every time I try to set the SelectedValue or SelectedIndex of the DDL to something that WILL BE invalid by the time the DDL is rendered. But the exception doesn't happen until the DropDownList.Items collection is actually changed, for instance with a DropDownList.Databind(). The first thing I did was make this change to Page_Init:

    protected void Page_Init(object sender, EventArgs e)
    {
        //SelectedIndex is -1, SelectedValue is "", SelectedItem is null
        if (!IsPostBack)
        {
            ddlTest.DataSource = new[] { 1, 2, 3 };
            ddlTest.DataBind();
            ddlTest.SelectedValue = "3";
        }
    }
    

    Now what gets interesting is this first example throws an exception, but the second one doesn't. They both set a SelectedValue that is valid at the time, but the value in the first example ("3") will not be valid by the time the page renders:

    protected void btnTest_Click(object sender, EventArgs e)
    {
        //SelectedIndex is 2, SelectedValue is "3", SelectedItem is {3}
        ddlTest.SelectedValue = "3"; 
        //SelectedIndex is 2, SelectedValue is "3", SelectedItem is {3}
        ddlTest.ClearSelection();
        //SelectedIndex is 0, SelectedValue is "1", SelectedItem is {1}
        ddlTest.Items.Clear();
        //SelectedIndex is -1, SelectedValue is "", SelectedItem is null
        ddlTest.DataSource = null;
        ddlTest.DataSource = new[] { 1, 2 };
        ddlTest.DataBind(); //Exception!
        //'ddlTest' has a SelectedValue which is invalid because it does not exist in the list of items.
        //Parameter name: value
    }
    
    protected void btnTest_Click(object sender, EventArgs e)
    {
        //SelectedIndex is 2, SelectedValue is "3", SelectedItem is {3} (from viewstate)
        ddlTest.SelectedValue = "2"; 
        //SelectedIndex is 1, SelectedValue is "2", SelectedItem is {3}
        ddlTest.ClearSelection();
        //SelectedIndex is 0, SelectedValue is "1", SelectedItem is {1}
        ddlTest.Items.Clear();
        //SelectedIndex is -1, SelectedValue is "", SelectedItem is null
        ddlTest.DataSource = null;
        ddlTest.DataSource = new[] { 1, 2 };
        ddlTest.DataBind(); //No Exception...
    }
    

    So, what conclusion can we draw from this? Well it seems the problem is due to setting the SelectedIndex or SelectedValue property of the DDL before the contents of the DDL may be changed on the same page cycle. If the previously set value is still available in the new contents of DropDownList.Items, you're in business. But if it's not in there, you get an exception.

    My guess is this has something to do with thread-safeness but I'm no expert on that so hopefully someone can comment.

提交回复
热议问题