首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

初学C#遇到的一个有关问题

2012-09-18 
初学C#遇到的一个问题。我有一个gridview,里面有一个dropdownlist控件和增加删除按钮。我要完成的效果是通过

初学C#遇到的一个问题。
我有一个gridview,里面有一个dropdownlist控件和增加删除按钮。
我要完成的效果是通过选择Dropdownlist的选项,将值加入一个checkboxlist里面。

我的代码如下,可是有错误。
  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
   
  int index = ((GridViewRow)(((DropDownList)sender).NamingContainer)).RowIndex;
  DropDownList ddl = GridView1.Rows[index].Cells[2].FindControl("DropDownList1") as DropDownList;
  int n = ddl.Items.Count;
  for (int i = 0; i < n; i++)
  { if(ddl.Items[i].Selected){

  CheckBoxList1.Items[index].Value = ((DropDownList)sender).SelectedValue;
  CheckBoxList1.Items.Add(ddl.Items[i].Value);
  }
  }  
  }


异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index


[解决办法]
应该是你这一块越界了

C# code
if(ddl.Items[i].Selected){  CheckBoxList1.Items[index].Value = ((DropDownList)sender).SelectedValue;  CheckBoxList1.Items.Add(ddl.Items[i].Value);}
[解决办法]
程序是在什么时候发生错误啊,是根本没运行起来,还是运行起来后选择Dropdownlist导致错误呢
错误停在哪一句?

另外 CheckBoxList1里面的项和Dropdownlist1的项内容一样多么

应该是这里的错:
CheckBoxList1.Items[index].Value = ((DropDownList)sender).SelectedValue;
CheckBoxList1.Items.Add(ddl.Items[i].Value);

热点排行