一个简单的分页,那里出错了呢?
前台:
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "temp.aspx.cs " Inherits= "bms._temp " %>
<!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 ">
<asp:Label ID= "lbl " runat= "server "> </asp:Label> <br>
<asp:linkbutton id= "btnFirst " onclick= "PagerButtonClick " runat= "server " CommandArgument= "1 "> </asp:linkbutton>
<asp:linkbutton id= "btnPrev " onclick= "PagerButtonClick " runat= "server " CommandArgument= "prev "> </asp:linkbutton>
<asp:linkbutton id= "btnNext " onclick= "PagerButtonClick " runat= "server " CommandArgument= "next "> </asp:linkbutton>
<asp:linkbutton id= "btnLast " onclick= "PagerButtonClick " runat= "server " CommandArgument= "last "> </asp:linkbutton>
</form>
</body>
</html>
后台:
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;
namespace bms
{
public partial class _temp :System.Web.UI.Page
{
int destpage;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState[ "PageIndex "] =1;
btnFirst.Text = " <首页> ";
btnPrev.Text = " <上一页> ";
btnNext.Text = " <下一页> ";
btnLast.Text = " <末页> ";
}
destpage = Convert.ToInt16(ViewState[ "PageIndex "]);
lbl.Text = destpage.ToString();
}
protected void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next ":
if (destpage < 3)
{
destpage = destpage + 1;
}
break;
case "prev ":
if (destpage > 0)
{
destpage -= 1;
}
break;
case "last ":
destpage =3;
break;
default:
destpage = System.Convert.ToInt16(arg);
break;
}
ViewState[ "PageIndex "] = destpage;
}
}
}
难以理解啊,
为什么载入后点击“下一页”后,值不变,再点就变了,上面是简单的代码,请问我那里的逻辑出现错误?谢谢,
[解决办法]
绑定顺序不对
你改变 ViewState[ "PageIndex "] 的值在 PAGELOAD里BIND之后执行的 而此刻并没有按照你的要求重新绑定 而是在下次回传才重绑
仔细看MSDN有关 GRID系列的代码 很明确 很清楚的
[解决办法]
databind()呀