'ddlXXX' has a SelectedIndex which is invalid because it does not exist in the list of items

'ddlXXX' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'ddlXXX' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value
Source Error:

Line 269:        ddlxxx.DataTextField = "Name"
Line 270:        ddlxxx.DataValueField = "id"
Line 271:        ddlxxx.DataBind() 
Line 272:        ddlxxx.Items.Insert(0, New ListItem("All", "-1"))
Line 273:        ddlStudents.SelectedIndex = 0

============================

listOrgs.Items.Clear();
listOrgs.SelectedIndex = -1;
listOrgs.SelectedValue = null;
listOrgs.ClearSelection();     // Clears the selection to avoid the exception (only one of these should be enough but in my application I needed all..)
listOrgs.DataSource = new Organization().DTListAll(SiteID);
listOrgs.DataTextField = "OrganizationName"; 
listOrgs.DataValueField = "OrganizationID"; 
listOrgs.DataBind();

原文地址:https://www.cnblogs.com/emanlee/p/1669403.html