Reputation:
i am creating a web app in which i have a gridview and on the pre-render method, i have following code
protected void dgvEdit_PreRender(object sender, EventArgs e)
{
if (this.dgvEdit.EditIndex != -1)
{
FileUpload fp = (FileUpload)dgvEdit.Rows[dgvEdit.EditIndex].FindControl("fpTask");
if (fp != null)
{
// You can apply condition here
fp.Enabled = true;
}
}
}
but the page is showing me the error
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
on this line
Line 329: FileUpload fp = (FileUpload)dgvEdit.Rows[dgvEdit.EditIndex].FindControl("fpTask");
what is wrong with the following code
Upvotes: 0
Views: 764
Reputation: 4630
The reason behind showing this error is dgvEdit.Rows.Count<dgvEdit.EditIndex
in Line No 329
.
You can use a Condition like
if (this.dgvEdit.EditIndex != -1 && dgvEdit.Rows.Count>=dgvEdit.EditIndex)
{
FileUpload fp = (FileUpload)dgvEdit.Rows[dgvEdit.EditIndex].FindControl("fpTask");
if (fp != null)
{
// You can apply condition here
fp.Enabled = true;
}
}
N.B: Use PreRender
event to perform any updates before the server control is rendered to the page. Any changes in the view state of the server control can be saved during this event. Such changes made in the rendering phase will not be saved.
I Think you should use RowDataBound
for this.
Upvotes: 0