<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

解决方案 »

  1.   

    假如客户端post一个字符串“1”这个值,你的控件要找到对应的index,自然是第一个(下标0)就匹配。本来就是如此,没有什么可奇怪的。
      

  2.   

    出现你这个疑问,主要原因使你没有去理解html从浏览器端给服务器提交的是什么值(字符串“1”,而不是位置编号)。如果你稍微深入一点,不要浅尝辄止,就会更容易搞懂技术了。
      

  3.   

    对于asp.net这么低门槛的东西,学习它要会读取源代码。来看看 HtmlSelect 控件的源代码bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
    {
    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;
    }
      

  4.   


    因为它post上去的是value=1,服务器据此推断selectindex
      

  5.   

    所以论坛中有很多脚本小子在搞所谓的“模拟浏览器提交”,你不要觉得奇怪——他们连浏览器都不用,哪里来的selectindex,人家照样提交数据。
      

  6.   

    这是不懂基础的缘故吧?
    嗯,我说微软他们那帮人,根本不懂最基础的html,还开发个P的ASP.NET啊,楼主你说是么???
      

  7.   


    因为它post上去的是value=1,服务器据此推断selectindex+1
      

  8.   

    LZ为什么想着会有value和text值都相同的选项出来呢?
    就像数据库的没有设置主键一样,id值都是1有很多条。
    如果你想取id=1的一条数据,你会知道你取到的是数据库的第几条吗?
      

  9.   


    确实有这种需求,不然,也不会发现这个bug。
      

  10.   

    key 必须唯一 这不算bug。