I Have a grid view ,which contains a huge data.In the 18th column I have a delete button in my grid view.for confirmation message I have used gridview RowDataBound event(the code for this is below). Its working fine.But when I used Gridview Paging it is throwing the exception saying that "An exception of type System.ArgumentOutOfRangeException' occurred in System.Web.dll but was not handled in user code.
Additional information: Specified argument was out of the range of valid values."
Please anyone help me out from this problem.My codes are below..............
//code of gridview in aspx page
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="False" DataKeyNames="ID,Created"
CssClass="table table-hover table-striped" ForeColor="Black" GridLines="None" OnRowCommand="gvEmployees_RowCommand"
OnRowDataBound="gvEmployees_RowDataBound" AllowPaging="True" OnPageIndexChanging="gvEmployees_PageIndexChanging">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="detailsOfEmployee" Text="Details">
<ControlStyle CssClass="btn btn-info btn-xs" />
</asp:ButtonField>
<asp:BoundField DataField="ID" HeaderText="ID" Visible="False" />
<asp:BoundField DataField="Code" HeaderText="Code" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="MiddleName" HeaderText="Middle Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="DateOfBirth" HeaderText="Date Of Birth" Visible="False" />
<asp:BoundField DataField="EmailID" HeaderText="Email ID" Visible="False" />
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="EncryptedPassword" HeaderText="Encrypted Password" Visible="False" />
<asp:BoundField DataField="PasswordSalt" HeaderText="Password Salt" Visible="False" />
<asp:BoundField DataField="DateOfJoining" HeaderText="Date Of Joining" Visible="False" />
<asp:BoundField DataField="Designation" HeaderText="Designation" />
<asp:BoundField DataField="UserRole" HeaderText="UserRole" Visible="False" />
<asp:BoundField DataField="IsActive" HeaderText="IsActive" Visible="False" />
<asp:BoundField DataField="Created" HeaderText="Created" Visible="False" />
<asp:BoundField DataField="LastUpdated" HeaderText="Last Updated" Visible="False" />
<asp:ButtonField ButtonType="Button" CommandName="editEmployee" Text="Edit">
<ControlStyle CssClass="btn btn-info btn-xs" />
</asp:ButtonField>
<asp:ButtonField ButtonType="Button" CommandName="deleteEmployee" Text="Delete">
<ControlStyle CssClass="btn btn-info btn-xs" />
</asp:ButtonField>
</Columns>
</asp:GridView>
//code of RowDatabpond event in .CS page
protected void gvEmployees_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (Button button in e.Row.Cells[18].Controls.OfType<Button>())
{
if (button.CommandName == "deleteEmployee")
{
button.Attributes["onclick"] = "if(!confirm('Do you want to delete ?')){ return false; };";
}
}
}
//code for gridview paging event
protected void gvEmployees_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvEmployees.PageIndex = e.NewPageIndex;
gvEmployees.DataBind();
}