【发布时间】:2012-10-09 21:01:41
【问题描述】:
我正在尝试遍历 gridview 并一次保存其中的所有项目。但是,我从下拉列表和文本框中获取值时遇到问题。我每次都会收到这个错误:
ArgumentOutOfRangeException was caught. Specified argument was out of the range of valid values.
这是我正在使用的代码:
foreach (GridViewRow gvr in gvInvalidOrgs.Rows)
{
try
{
org_code = Convert.ToInt32(gvr.Cells[0].Text);
division = ((DropDownList)gvr.Cells[1].Controls[0]).SelectedValue;
org_description = (((TextBox)gvr.Cells[2].Controls[0]).Text);
}
...
}
如果重要的话,文本框和下拉列表都是在行绑定上动态创建的。
TIA
【问题讨论】:
-
您最好使用 GridViewRow.FindControl() 并按名称查找控件,而不是使用 Controls[index] 通过索引引用它 您可能需要将列转换为 TemplateColumns 以获取它工作,但根据我的经验,它更容易,更不容易出错。 msdn.microsoft.com/en-us/library/… 另见stackoverflow.com/questions/1965835/…
-
不要在
RowDataBound中创建它们,而是在RowCreated中创建它们。只有当 GridView 是数据绑定时才会调用前者。每次回发时都会调用后者。