我有一个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
我要完成的效果是通过选择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
解决方案 »
- 二进制流“0”不包含有效的 BinaryHeader。这可能是由于无效流,或由于在序列化和反序列化之间的对象版本更改。
- 急,急,急,接收进来的时间如何判断更合理???
- 做了几个LINQ视频,支持我一下吧
- 如何在C#里导入AUTOCAD图
- [SQL问题]请教怎样在select语句where Cid=后面加一个变化的值.具体如正文
- C#中使用XMLHTTP
- 请教如何给一个事件的委托方法传值。
- ???如何用.net制作.chm帮助文件,或者用其他工具制作也行!!!
- label不能循环显示结果
- 开发邮件系统
- c# addin开发,高手进。谢谢! 帮顶给分!
- 终于攒了点分可以发帖了,我想问下怎样使用加密狗
取得的不是你想要的行索引 你可以调试下看看。
取得的不是你想要的行索引很可能是-1, 你可以调试下看看。
我调试过了,index的值就是从0开始的。
{ CheckBoxList1.Items[index].Value = ((DropDownList)sender).SelectedValue;
CheckBoxList1.Items.Add(ddl.Items[i].Value);
}
假设checkboxlist里没有项,你这里开始设置CheckBoxList1.Items[index].Value的时候,一定索引越界
错误停在哪一句?另外 CheckBoxList1里面的项和Dropdownlist1的项内容一样多么应该是这里的错:
CheckBoxList1.Items[index].Value = ((DropDownList)sender).SelectedValue;
CheckBoxList1.Items.Add(ddl.Items[i].Value);
希望看到是否如9L猜测 LZ要调试下具体哪里啊,,
我的gridview里面有添加按钮啊,添加了以后就有另外一个dropdowanlist了。
所以我想实现的是每行的Dropdownlist添加入相应的checkboxl的ITEM里面。