GridView“GridView1”激发了未处理的事件“PageIndexChanging”。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: GridView“GridView1”激发了未处理的事件“PageIndexChanging”。
源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
堆栈跟踪:
[HttpException (0x80004005): GridView“GridView1”激发了未处理的事件“PageIndexChanging”。]
System.Web.UI.WebControls.GridView.OnPageIndexChanging(GridViewPageEventArgs e) +324
System.Web.UI.WebControls.GridView.HandlePage(Int32 newPage) +110
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +516
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +213
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +244
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3837
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42
ASP.NET的CS代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
string zhumin = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDataGrid();
IsFirstLastPage();
}
}
private void BindDataGrid()
{
SqlConnection MyConnection = new SqlConnection(zhumin);
String cmdText = "SELECT * FROM Orders";
SqlDataAdapter da = new SqlDataAdapter(cmdText,MyConnection);
MyConnection.Open();
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
MyConnection.Close();
}
protected void GridView1_PageIndexChanged(object source,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindDataGrid();
}
protected void PageCut_Click(object sender, EventArgs e)
{
String commangArg = ((LinkButton)sender).CommandArgument;
switch (commangArg)
{
case "First":
{
GridView1.PageIndex = 0;
break;
}
case "Prev":
{
GridView1.PageIndex = (int)Math.Max(0,GridView1.PageIndex - 1);
break;
}
case "Next":
{
GridView1.PageIndex = (int)Math.Min(GridView1.PageCount - 1, GridView1.PageIndex + 1);
break;
}
case "Last":
{
GridView1.PageIndex = GridView1.PageCount - 1;
break;
}
default:
{
break;
}
}
BindDataGrid();
IsFirstLastPage();
}
private void IsFirstLastPage()
{
if (GridView1.PageCount > 0)
{
if (GridView1.PageCount != 1)
{
if (GridView1.PageIndex == 0)
{
LinkButton2.Enabled = false;
}
else
{
LinkButton2.Enabled = true;
}
if (GridView1.PageIndex == GridView1.PageCount - 1)
{
LinkButton3.Enabled = false;
}
else
{
LinkButton3.Enabled = true;
}
}
else
{
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
}
}
else
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
}
}
}
最后是我的ASPX代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="Yellow"
ForeColor="Black" Width="372px" AllowPaging="True">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="员工ID" />
<asp:BoundField DataField="EmployeeID" HeaderText="顾客ID" />
<asp:BoundField DataField="ShipVia" HeaderText="ShipID" />
</Columns>
</asp:GridView>
</center>
<br />
<br />
<center style="text-align: left">
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="First">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="Prev">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandArgument="Next">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandArgument="Last">尾页</asp:LinkButton></center>
</div>
</form>
</body>
</html>