我想要实现下拉菜单联动
第一次加载没问题,可是点击时出现了问题,在顺序点下拉菜单正常,当重复点上边点击过的条目时,就会出现如下错误:“/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();
}
第一次加载没问题,可是点击时出现了问题,在顺序点下拉菜单正常,当重复点上边点击过的条目时,就会出现如下错误:“/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();
}
this.DropDownList1.SelectedValue[index]返回当前选中项的value一个char的
如果index大于this.DropDownList1.SelectedValue.Length就会有错的
当索引的的长度超过了字符串的长度就会出现索引越界的错误
原来索引还能这样用 受教了
{
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();
}