62,243
社区成员




// System.Web.UI.WebControls.DropDownList
protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string[] values = postCollection.GetValues(postDataKey);
base.EnsureDataBoundInLoadPostData();
if (values != null)
{
base.ValidateEvent(postDataKey, values[0]);
int num = this.Items.FindByValueInternal(values[0], false);
if (this.SelectedIndex != num)
{
base.SetPostDataSelection(num);
return true;
}
}
return false;
}
// System.Web.UI.WebControls.ListItemCollection
internal int FindByValueInternal(string value, bool includeDisabled)
{
int num = 0;
foreach (ListItem listItem in this.listItems)
{
if (listItem.Value.Equals(value) && (includeDisabled || listItem.Enabled))
{
return num;
}
num++;
}
return -1;
}
// System.Web.UI.WebControls.ListControl
protected void SetPostDataSelection(int selectedIndex)
{
if (this.Items.Count != 0 && selectedIndex < this.Items.Count)
{
this.ClearSelection();
if (selectedIndex >= 0)
{
this.Items[selectedIndex].Selected = true;
}
}
}
当然你要了解 LoadPostData 机制原理,也就是了解 webform,才能知道看哪里。// System.Web.UI.WebControls.ListControl
[Bindable(true), Browsable(false), DefaultValue(0), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Themeable(false), WebCategory("Behavior"), WebSysDescription("WebControl_SelectedIndex")]
public virtual int SelectedIndex
{
get
{
for (int i = 0; i < this.Items.Count; i++)
{
if (this.Items[i].Selected)
{
return i;
}
}
return -1;
}
set
{
if (value < -1)
{
if (this.Items.Count != 0)
{
throw new ArgumentOutOfRangeException("value", SR.GetString("ListControl_SelectionOutOfRange", new object[]
{
this.ID,
"SelectedIndex"
}));
}
value = -1;
}
if ((this.Items.Count != 0 && value < this.Items.Count) || value == -1)
{
this.ClearSelection();
if (value >= 0)
{
this.Items[value].Selected = true;
}
}
else if (this._stateLoaded)
{
throw new ArgumentOutOfRangeException("value", SR.GetString("ListControl_SelectionOutOfRange", new object[]
{
this.ID,
"SelectedIndex"
}));
}
this.cachedSelectedIndex = value;
}
}
以及// System.Web.UI.WebControls.DropDownList
[DefaultValue(0), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), WebCategory("Behavior"), WebSysDescription("WebControl_SelectedIndex")]
public override int SelectedIndex
{
get
{
int num = base.SelectedIndex;
if (num < 0 && this.Items.Count > 0)
{
this.Items[0].Selected = true;
num = 0;
}
return num;
}
set
{
base.SelectedIndex = value;
}
}
当你考虑到“当要显示历史记录的时候”的时候,如果你懂 DropdownList,那么你就知道控件根本不知道你(这里应该是还并没有说明的)那种代码。