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>
Eventhough the value of index is zero, it throws this exception. Can anyone please explain me why this error occurs..
Many thanks in advance..