我想要实现下拉菜单联动
第一次加载没问题,可是点击时出现了问题,在顺序点下拉菜单正常,当重复点上边点击过的条目时,就会出现如下错误:“/SSMS”应用程序中的服务器错误。
--------------------------------------------------------------------------------索引超出了数组界限。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 索引超出了数组界限。源错误: 
行 113: break;
行 114: case 1:
行 115: SqlCommand cmdtc=new SqlCommand("select * from teacher where tsid="+this.DropDownList1.SelectedValue[a],con);
行 116: SqlDataReader dr=cmdtc.ExecuteReader();
行 117: this.DropDownList2.DataSource=dr;
 源文件: c:\inetpub\wwwroot\ssms\teacherctrl.aspx.cs    行: 115 堆栈跟踪: 
[IndexOutOfRangeException: 索引超出了数组界限。]
   System.String.get_Chars(Int32 index) +20
   SSMS.teacherCtrl.DropDownList1_SelectedIndexChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\ssms\teacherctrl.aspx.cs:115
   System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
   System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
   System.Web.UI.Page.RaiseChangedEvents()
   System.Web.UI.Page.ProcessRequestMain() 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573 原代码如下:private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
int a=Convert.ToInt32(this.DropDownList1.SelectedValue);
SqlConnection con=DBCON.createCon();
con.Open();
switch(a)
{
case 0:
SqlCommand cmdstu=new SqlCommand("select * from student where tsid="+this.DropDownList1.SelectedValue[b],con);
SqlDataReader sdr=cmdstu.ExecuteReader();
this.DropDownList2.DataSource=sdr;
this.DropDownList2.DataTextField="clid";
this.DropDownList2.DataValueField="sID";
this.DropDownList2.DataBind();
sdr.Close();
break;
case 1:
SqlCommand cmdtc=new SqlCommand("select * from teacher where tsid="+this.DropDownList1.SelectedValue[b],con);
SqlDataReader dr=cmdtc.ExecuteReader();
this.DropDownList2.DataSource=dr;
this.DropDownList2.DataTextField="clid";
this.DropDownList2.DataValueField="tid";
this.DropDownList2.DataBind();
dr.Close();
break;
}
con.Close();
}

解决方案 »

  1.   

    this.DropDownList1.SelectedValue是当前选中项的value
    this.DropDownList1.SelectedValue[index]返回当前选中项的value一个char的
    如果index大于this.DropDownList1.SelectedValue.Length就会有错的
      

  2.   

    最好是在前面加一个DropDownList1.items.clear();每次加载给清除一次!!
      

  3.   

    this.DropDownList1.SelectedValue[b]头一次看见这么写的
      

  4.   

    DropDownList1.SelectedValue获得的是个字符串  string s=“abcd”;s[1]返回的是b
    当索引的的长度超过了字符串的长度就会出现索引越界的错误
      

  5.   

    this.DropDownList1.SelectedValue[a]
    原来索引还能这样用 受教了
      

  6.   

    SqlCommand cmdtc=new SqlCommand("select * from teacher where tsid="+this.DropDownList1.SelectedValue[a],con); 前面加个判断,value长度大于a ,才调用。
      

  7.   

    private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    DropDownList2.items.clear();
        int a=Convert.ToInt32(this.DropDownList1.SelectedValue);
        SqlConnection con=DBCON.createCon();
        con.Open();
        switch(a)
        {
            case 0:
                SqlCommand cmdstu=new SqlCommand("select * from student where tsid="+this.DropDownList1.SelectedValuecon);
                SqlDataReader sdr=cmdstu.ExecuteReader();
                this.DropDownList2.DataSource=sdr;
                this.DropDownList2.DataTextField="clid";
                this.DropDownList2.DataValueField="sID";
                this.DropDownList2.DataBind();
                sdr.Close();
                break;
            case 1:
                SqlCommand cmdtc=new SqlCommand("select * from teacher where tsid="+this.DropDownList1.SelectedValue,con);
                SqlDataReader dr=cmdtc.ExecuteReader();
                this.DropDownList2.DataSource=dr;
                this.DropDownList2.DataTextField="clid";
                this.DropDownList2.DataValueField="tid";
                this.DropDownList2.DataBind();
                dr.Close();
                break;
        }
        con.Close();
    }
      

  8.   

    确定DropDownList1里的value值只有0和1吗