DataList分页问题——只能点一次下一页。
下面这是我用网上找的改的一段DataList分页代码。只能点一次下一页,然后再点下一页或上一页就页面就不变了。不知哪位仁兄能帮我看看哪出错了,谢谢。SpecicalClothes.cs
public partial class SpecicalClothes : System.Web.UI.Page
{
int PageSize, RecordCount, PageCount, CurrentPage,
SqlConnection cn = null;
protected void Page_Load(object sender, EventArgs e)
{
PageSize = 5;
string constr = "Integrated Security=SSPI;server=localhost;Trusted_Connection=true;database=ClothesStore";
cn = new SqlConnection(constr);
cn.Open();
if (!Page.IsPostBack)
{
CurrentPage = 0;
PageIndex = 0;
ViewState["PageIndex"] = 0;
ClothesStore.ClothesStoreDBO ClothesCataloguesInfo = new ClothesStore.ClothesStoreDBO();
RecordCount = CalculateRecord();
int a =CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
PageCount = RecordCount / PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
ListBind();
}
}
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Clothes";
SqlCommand cmd = new SqlCommand(strCount, cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
public void ListBind()
{
MyList.DataSource = CreateSource();
MyList.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
if (CurrentPage == 0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage + 1).ToString();
}
ICollection CreateSource()
{ //设定导入的起终地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from Clothes";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strSel, cn);
da.Fill(ds, StartIndex, PageSize, "Clothes1");
return ds.Tables["Clothes1"].DefaultView;
}
public void Page_OnClick(Object sender, CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
int a = (int)ViewState["StartIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
switch (cmd)
{
case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
}
ViewState["StartIndex"] = CurrentPage;
ListBind();
}
}
}
部分 SpecicalClothes.aspx
<tr>
<td>
<asp:LinkButton ID="lbnPrevPage" CommandName="prev" runat="server" OnCommand="Page_OnClick">上一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick">下一页</asp:LinkButton>
</td>
<td>
共有<asp:Label id="lblRecordCount" runat="server" CssClass="rec" />条记录
当前为<asp:Label id="lblCurrentPage" runat="server" CssClass="rec" />/
<asp:Label id="lblPageCount" runat="server" CssClass="rec" /> </td>
</tr> --------------------编程问答-------------------- 你的这个函数
public void Page_OnClick(Object sender, CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
// int a = (int)ViewState["StartIndex"]; 无用的语句
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
switch (cmd)
{
case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
}
//因为你的当前页CurrentPage是从ViewState["PageIndex"]获取的,所以你要更改的是ViewState["PageIndex"]中的值
//ViewState["StartIndex"] = CurrentPage; 改成下面的就OK了
ViewState["PageIndex"] = CurrentPage;
ListBind();
}
--------------------编程问答-------------------- 没用啊,改了和没改效果都一样。我是琢磨着那PageIndex的值就没变过。能帮我解释一下 PageIndex吗?
补充:.NET技术 , C#