Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I wrote some code which prompt the user for conformation on delete in a detail view. and I am using command field (Edit, Delete) to perform Edit and delete function.

C#
protected void DViewComputer_DataBound(object sender, EventArgs e)
{
    Button button = (Button)(DViewComputer.Rows[30].Cells[0].Controls[2]);


                    // Add delete confirmation
    ((System.Web.UI.WebControls.Button)(button)).OnClientClick = "if (!confirm('Are you sure " +"you want to delete this record?')) return;";

}


The code works fine as it was intended to do. It ask for conformation and delete the record, but it causes an error saying "Index was out of range. Must be non-negative and less than the size of the collection". Why is that? how can I fix it?
Posted
Comments
Nilesh Patil Kolhapur 20-Feb-12 23:32pm    
if possible give source code of gridview then i will give u exact answer

1 solution

Look at the code: you have hard-coded the access to some button assuming you have at least 31 rows, at least one cell in a row #20, which has at least one control, and also that this control is assignment-compatible with Button. Wow! How could you guarantee that? No wonder you have this exception. Just never ever do it.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900