我试图通过gridview循环并一次保存所有项目。不过,我有一个问题,从下拉列表和文本框中获取值。ASP.Net获取值ArgumentOutOfRangeException抛出抛出
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);
}
...
}
两个文本框和dropdownlists是在rowbound动态创建的,如果该事项。
TIA
你会更好地使用GridViewRow.FindControl()服务,并通过名称查找控件,而不是通过索引使用Controls [index]来引用它您可能需要将列转换为TemplateColumns才能使其工作,但它是在我的经验中更容易,更不容易出错。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.findcontrol.aspx也见http://stackoverflow.com/questions/1965835/find-control-inside-grid -row – David
不要在'RowDataBound'中创建它们,而要在'RowCreated'中创建它们。只有当GridView是数据绑定时才会调用前者。后者在每一次回传中都会被调用。 –