ArgumentOutOfRangeException was unhandled by user code - (ASP .net)

Posted by ASr.. on Stack Overflow See other posts from Stack Overflow or by ASr..
Published on 2010-06-07T05:27:08Z Indexed on 2010/06/07 5:32 UTC
Read the original article Hit count: 188

Filed under:

I use a GridView to display the records from the database. Also i've attached a Hyperlink to the GridView using TemplateField. When I try to add "onclick" attribute to the HyperLink inside the RowDateBound event, I get the following error..

   GridView1.DataKeys[e.Row.RowIndex].Value = 'GridView1.DataKeys[e.Row.RowIndex]' threw an exception of type 'System.ArgumentOutOfRangeException'

Message = "Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"

This is the coding inside the RowDataBound mentod..

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink HyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");

//The following line makes the error
                HyperLink1.Attributes.Add("onclick", "ShowMyModalPopup('" + GridView1.DataKeys[e.Row.RowIndex].Value + "')");

            } 
        }

This is "ShowMyModalPopup" function in javascript

<script type="text/javascript">
        function ShowMyModalPopup(userpk)
        { 
         var modal = $find('ModalPopupExtender1'); 
         modal.show(); 
         WebService.FetchOneUser(userpk,DisplayResult);
        }
</script>

Can anyone please explain me why this error occurs..

Many thanks in advance..

© Stack Overflow or respective owner

Related posts about asp.net-ajax