一个页面上有一个DataGrid控件。
控件自带分页
当我点页码2的时候。没有反映,就页面刷新一下。也没有提示错,而且内容还是原来的,
但当我再点一次页码的时候,页面有变化了。
直接提示错
怎么解决啊??
现在2个问题,为什么要点2下才可以分页,而且提示错误~~
-------------------------------------------------------------------------
能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: 未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。
源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
堆栈跟踪:
[HttpException (0x80004005): 未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。]
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +355
System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +302
System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +302
System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +302
System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +302
System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +302
System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +302
System.Web.UI.Page.LoadAllState() +506
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2541
------------------------------------------------
页面代码如下
aspx 页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wenruoshow.aspx.cs" Inherits="wenruoshow"%>
<!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>
<asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True"
CellPadding="0" GridLines="None" OnItemCreated="DataGrid1_ItemCreated" OnPageIndexChanged="DataGrid1_PageIndexChanged"
ShowHeader="False" AutoGenerateColumns="False">
<PagerStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" HorizontalAlign="Right" Mode="NumericPages" />
<Columns>
<asp:TemplateColumn HeaderText="标题">
<ItemTemplate>
<a href=' <%# DataBinder.Eval(Container,"DataItem.id") %>'><%# DataBinder.Eval(Container,"DataItem.title") %></a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="时间">
<ItemTemplate>
<%# DataBinder.Eval(Container,"DataItem.data","{0:d}") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></div>
</form>
</body>
</html>
.cs页面
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.OleDb;
public partial class wenruoshow : System.Web.UI.Page
{
OleDbCommand cmd;
OleDbConnection con;
OleDbDataAdapter adp;
int nian;
protected void Page_Load(object sender, EventArgs e)
{
nian = Convert.ToInt32(Request.QueryString["nian"]);
if (nian == 0)
{
if (!IsPostBack)
{
Bind();
}
}
else
{
try
{
if (!IsPostBack)
{
Bind1();
}
}
catch
{
Response.Write("操作失败");
Response.Write(nian);
}
}
}
private void Bind()
{
string dataPatch = ConfigurationManager.AppSettings["data"];
string ConnetionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + HttpContext.Current.Server.MapPath(dataPatch);
con = new OleDbConnection(ConnetionString);
cmd = new OleDbCommand("Select id, title,info,data from info1", con);
adp = new OleDbDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds, "info1");
DataGrid1.DataSource = ds.Tables["info1"];
DataGrid1.DataBind();
}
private void Bind1()
{
string dataPatch = ConfigurationManager.AppSettings["data"];
string ConnetionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + HttpContext.Current.Server.MapPath(dataPatch);
con = new OleDbConnection(ConnetionString);
cmd = new OleDbCommand("Select id, title,info,data from info1 where nian=@nian order by nian", con);
cmd.Parameters.Add(new OleDbParameter("@nian", OleDbType.Integer));
cmd.Parameters["@nian"].Value = nian;
adp = new OleDbDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds, "info1");
DataGrid1.DataSource = ds.Tables["info1"];
DataGrid1.DataBind();
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
}
protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
ListItemType type = e.Item.ItemType;
if (type == ListItemType.Pager)
{
TableCell pager = (TableCell)e.Item.Controls[0];
Label lbl = new Label();
lbl.EnableViewState = false;
pager.Controls.Add(lbl);
for (int i = 0; i < pager.Controls.Count; i += 2)
{
object o = pager.Controls[i];
if (o is LinkButton)
{
LinkButton h = (LinkButton)o;
h.Text = "[" + h.Text + "]";
}
else
{
Label l = (Label)o;
l.Text = "<font color=\"#FF0000\">第" + l.Text + "页</font>";
}
}
}
}
}