在进行网格视图分页时如何避免“指定的参数超出有效值范围"异常. [英] How to avoid '' Specified argument was out of the range of valid values'' exception while doing grid view Paging..

查看:154
本文介绍了在进行网格视图分页时如何避免“指定的参数超出有效值范围"异常.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量数据的网格视图.在第18列中,我的网格视图中有一个删除按钮.对于确认消息,我使用了gridview RowDataBound事件(下面的代码).它的工作正常.但是,当我使用Gridview Paging时,它抛出异常,说"System.ArgumentOutOfRangeException类型的异常"发生在System.Web.dll中,但未在用户代码中处理. 附加信息:指定的自变量超出有效值范围."

请任何人帮助我解决这个问题.我的代码在下面..............

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..............



//aspx页面中gridview的代码


//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>


//.CS页面中RowDatabpond事件的代码


//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; };";
             //button.Attributes.Add("onclick", "return confirm('Do you want to delete the record?');");

         }

     }
 }



//gridview分页事件的代码



//code for gridview paging event

protected void gvEmployees_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       gvEmployees.PageIndex = e.NewPageIndex;
       gvEmployees.DataBind();
   }

推荐答案

调用在PageIndexChanged事件后填充GridView gvEmployees的函数.
call the function that fill the GridView gvEmployees after the PageIndexChanged Event

protected void gvEmployees_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       gvEmployees.PageIndex = e.NewPageIndex;
     this.fillGridView1();
   }
private void fillGridView1() 
{ 
gvEmployees.DataSource = datasourcename;
 gvEmployees.DataBind(); 
}


这篇关于在进行网格视图分页时如何避免“指定的参数超出有效值范围"异常.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆