各位前辈,我用VS2005建了一个WEBFORM,里面有四个Dropdownlist,分别设置如下:
<asp:dropdownlist id="A" runat="server" CssClass="tbox_R" Width="145px"></asp:dropdownlist>
<asp:dropdownlist id="B" runat="server" CssClass="tbox_R" Width="145px"></asp:dropdownlist>
<asp:dropdownlist id="C" runat="server" CssClass="tbox_R" Width="145px"></asp:dropdownlist>
<asp:dropdownlist id="D" runat="server" CssClass="tbox_R" Width="145px"></asp:dropdownlist>
后置类代码如下:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
           
            If Not Page.IsPostBack Then            '第一步读取数据库的字典表,然后绑定到四个下拉菜单中
                BindData()  
            '第二步,插入空字符串或必填标识字符串到四个下拉菜单,位置在首位,并处于被选择状态           
                A.Items.Insert(0, New ListItem("--请选择--"))
                B.Items.Insert(0, New ListItem("--请选择--"))   
                C.Items.Insert(0, New ListItem(""))
                D.Items.Insert(0, New ListItem(""))                      
          
            End If
        End Sub
这一串代码我在VS2003中测试过,完全没有问题,INSERT到DROPDOWNLIST时,空字符串或必填标识字符串一如所想显示在首位,并处于被选择的位置。
但当我将这串代码在VS2005中调试时,却发现了一个很怪的现象,A、B两个DROPDOWNLIST显示的被选择项是字典表的内容,"--请选择--"这个内容是在首位(要将滚动条向上拉一位),C、D两个DROPDOWNLIST显示的被选择项是空字符串(这是正确的),并处于首位,字典表的内容紧跟其后。为什么会这样呢?经监视发现:
1、当第一步绑定字典表后,各个DROPDOWNLIST的SELECTINDEX是0;
2、当第二步插入空字符串或必填标识字符串到四个下拉菜单后,A、B两个DROPDOWNLIST的SELECTINDEX是1,而C、D两个DROPDOWNLIST的SELECTINDEX是0,这是为什么呢?同样是往DROPDOWNLIST插入一个LISTTIEM???在VS2003中就没有这种情况出现,看来VS2005与VS2003是有很多不同的地方。虽然我能够用通过设置DropDownList的AppendDataBoundItems属性为true来达到想要的效果,但对于出现这种情况非常纳闷,可否请VS2005高手们解释一下原因?

解决方案 »

  1.   

    this.A.DataSource = new string[]{"A","B"};
            this.A.DataBind();
            this.B.DataSource = new string[]{"A","B"};
            this.B.DataBind();
             this.C.DataSource = new string[]{"A","B"};
            this.C.DataBind();
             this.D.DataSource = new string[]{"A","B"};
            this.D.DataBind();
         
                Response.Write(A.SelectedIndex);
                Response.Write(B.SelectedIndex);
                Response.Write(C.SelectedIndex);
                Response.Write(D.SelectedIndex);
            
                    A.Items.Insert(0, new ListItem("--请选择--"));
                    B.Items.Insert(0, new ListItem("--请选择--"));   
                    C.Items.Insert(0, new ListItem(""));
                    D.Items.Insert(0, new ListItem(""));
             
                    Response.Write(A.SelectedIndex);
                    Response.Write(B.SelectedIndex);
                    Response.Write(C.SelectedIndex);
                    Response.Write(D.SelectedIndex);------说错了莫怪。
      

  2.   

    如果把第一次的 4个Response.Write注释掉,那么第二次的4个write的值就是0000
    如果不去掉结果就是0000 1111------说错了莫怪。
      

  3.   

    直接显示不获取SelectedIndex是没有问题的
    2005下获取SelectedIndex时会设置this.Items[0].Selected = true;所以.当你先取得这个属性后,再Insert ,会将原先的第一项设置为selected,再加入新的选择,会保留原来的选择
      

  4.   

    2005得到SelectedIndex过程如下Public Overridable Property SelectedIndex As Integer
          Get
                Dim num1 As Integer = 0
                Do While (num1 < Me.Items.Count)
                      If Me.Items.Item(num1).Selected Then
                            Return num1
                      End If
                      num1 += 1
                Loop
                Return -1
          End Get      
    End Property
    Public Overrides Property SelectedIndex As Integer
          Get
                Dim num1 As Integer = MyBase.SelectedIndex
                If ((num1 < 0) AndAlso (Me.Items.Count > 0)) Then
                      Me.Items.Item(0).Selected = True
                      num1 = 0
                End If
                Return num1
          End Get
    End Property
      

  5.   

    If Not Page.IsPostBack Then
                    A.Items.add("--请选择--")
                    B.Items.add("--请选择--") 
                    C.Items.add(""))
                    D.Items.add("")      
    END IF
    用这个试一试
      

  6.   

    谢谢各位大哥的热情参与。不过,VS2005还是真的很奇怪,我今天没有设断点,直接应用,发现很正常,A、B、C、D都分别显示了"--请选择--","--请选择--","", ""。(这是正确的显示,我在VS2003中也是这样的),再查一下它们的SELECTINDEX都是0。
       本来已经得到想要的效果,但我还是不明白原因,于是将A、B、C、D四个DROPDOWNLIST控件 添加到监视,设置断点,发现一个很奇怪的现象
    1、将监视器里的 A,C是处于打开状态(就是点击了前面的“+”号),最后显示在页面的是字典表里的第一项,并且它们的SELECTINDEX都是1;
    2、将监视器里的B、D是处于不展开状态(在INSERT操作过后,可以点击打开),最后显示在页面的分别是"--请选择--"和"",并且它们的SELECTINDEX都是0。
      我用不同的A、B、C、D组合,试了很多次,结果都是一样,凡是在INSERT之前,在监视器里处于打开状态的,最后显示在页面的是字典表里的第一项,并且它们的SELECTINDEX都是1;凡是处于不展开状态的,它们显示的都是INSERT到0索引的内容,它们的SELECTINDEX都是0。
      是我的VS2005(SP1)编译器有问题呢?还是别的原因,真的纳闷,VS2005的编译测试功能太不好用了!!
      

  7.   

    可能是它为了保持一致性,即selectindex对应的item是选中的,在添加item前没有访问 selectedindex 属性,可以理解为selectedindex 为null selecteditem 为null  此时再添加item.第一次访问selectedindex属性才有值.为0