<select id="test" runat="server">
<option value="1">测试</option>
<option value="1">测试</option>
<option value="1">测试</option>
<option value="1">测试</option>
</select>当K,V值相同时,SelectedIndex获取到的值永远等于0,换用DropDownList,同样。不晓得为什么会这样?.netbugdropdownlistselect
调试欢乐多
{
return this.LoadPostData(postDataKey, postCollection);
}protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string[] values = postCollection.GetValues(postDataKey);
bool flag = false;
if (values != null)
{
if (!this.Multiple)
{
int num = this.Items.FindByValueInternal(values[0], false);
if (this.SelectedIndex != num)
{
this.SelectedIndex = num;
flag = true;
}
}
else
{
int num2 = values.Length;
int[] selectedIndices = this.SelectedIndices;
int[] array = new int[num2];
for (int i = 0; i < num2; i++)
{
array[i] = this.Items.FindByValueInternal(values[i], false);
}
if (selectedIndices.Length == num2)
{
for (int j = 0; j < num2; j++)
{
if (array[j] != selectedIndices[j])
{
flag = true;
break;
}
}
}
else
{
flag = true;
}
if (flag)
{
this.Select(array);
}
}
}
else
{
if (this.SelectedIndex != -1)
{
this.SelectedIndex = -1;
flag = true;
}
}
if (flag)
{
base.ValidateEvent(postDataKey);
}
return flag;
}public virtual int SelectedIndex
{
get
{
for (int i = 0; i < this.Items.Count; i++)
{
if (this.Items[i].Selected)
{
return i;
}
}
if (this.Size <= 1 && !this.Multiple)
{
if (this.Items.Count > 0)
{
this.Items[0].Selected = true;
}
return 0;
}
return -1;
}
set
{
if (this.Items.Count == 0)
{
this.cachedSelectedIndex = value;
return;
}
if (value < -1 || value >= this.Items.Count)
{
throw new ArgumentOutOfRangeException("value");
}
this.ClearSelection();
if (value >= 0)
{
this.Items[value].Selected = true;
}
}
}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;
}
因为它post上去的是value=1,服务器据此推断selectindex
嗯,我说微软他们那帮人,根本不懂最基础的html,还开发个P的ASP.NET啊,楼主你说是么???
因为它post上去的是value=1,服务器据此推断selectindex+1
就像数据库的没有设置主键一样,id值都是1有很多条。
如果你想取id=1的一条数据,你会知道你取到的是数据库的第几条吗?
确实有这种需求,不然,也不会发现这个bug。