.net(c#)

ll_sky 2009-04-06 10:01:24
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.SqlClient;

public partial class _533 : System.Web.UI.Page
{
SqlConnection myconn;
int G_int_Pagesize, G_int_Recordcount, G_int_Page_Count, G_int_CurrentPage;
public int calculaterecord()
{
int intcount;
string strcount = "select count(*) as co from tb_Article";
SqlCommand mycomm = new SqlCommand(strcount, myconn);
SqlDataReader read = mycomm.ExecuteReader();
if (read.Read())
{
intcount = Int32.Parse(read["co"].ToString());
}
else
{
intcount = 0;
}
}

ICollection createsource()
{
int startindex;
startindex = G_int_CurrentPage * G_int_Pagesize;
string strsel = "select * from tb_Article";
DataSet ds = new DataSet();
SqlDataAdapter myadapter = new SqlDataAdapter(strsel, myconn);
myadapter.Fill(ds, startindex, G_int_Pagesize, "tb_Article");
return ds.Tables["tb_Artical"].DefaultView;
}

public void listbind()
{
DlMain.DataSource = createsource();
DlMain.DataBind();
lnkbtnpageUP.Enabled = true;
lnkbtnPageDown.Enabled = true;
if (G_int_CurrentPage == (G_int_Page_Count - 1))
lnkbtnPageDown.Enabled = false;
if (G_int_CurrentPage == 0)
lnkbtnpageUP.Enabled = false;
}



protected void page_load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=db_blog;uid=sa;pwd=sa");
con.Open ();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_Article", con);
DataSet ds = new DataSet();
ada.Fill(ds);
DlMain.DataSource = ds;
DlMain.DataBind();



G_int_Pagesize = 1;
myconn = new SqlConnection("server=.;database=db_blog;uid=sa;pwd=sa");
myconn.Open();
if (!IsPostBack)
{
listbind();
G_int_CurrentPage = 0;
ViewState["pageindex"] = 0;
G_int_Recordcount = calculaterecord();
lblcount.Text = G_int_Recordcount.ToString();
if (G_int_Recordcount % G_int_Pagesize != 0)
{
G_int_Page_Count = G_int_Recordcount / G_int_Pagesize + 1;
}
else
{
G_int_Page_Count = G_int_Recordcount / G_int_Pagesize;
}
ViewState["G_int_PageCount"] = G_int_Page_Count;
}

}



protected void Page_OnClick(object sender, CommandEventArgs e)
{
G_int_CurrentPage = (int)ViewState["PageIndex"];
G_int_Page_Count = (int)ViewState["G_int_PageCount"];
string cmd = e.CommandName;
switch (cmd)
{
case "next":
if (G_int_CurrentPage < (G_int_Page_Count - 1)) G_int_CurrentPage++;
break;
case "prev":
if (G_int_CurrentPage > 0) G_int_CurrentPage--;
break;
case "first":
G_int_CurrentPage = 0;
break;
case "last":
G_int_CurrentPage = G_int_Page_Count - 1;
break;
}
ViewState["PageIndex"] = G_int_CurrentPage;
listbind();
}
}
谁能详细分析一下这个分页控件代码,看了好几遍,看懂了但似乎少点什么...
有好的分页代码共享一下,你觉得上面可以改进下吗??
(。。。。初学....)
...全文
69 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
greymouseyu 2009-04-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cyz9977 的回复:]
分页控件 Aspnetpager+存儲過程 就很好了,.net編程能省力就省~~
[/Quote]

对,很好用
cyz9977 2009-04-06
  • 打赏
  • 举报
回复
數據顯示部分?gridview?table?

gridview自帶分頁。

這種分頁是取出所有,再分頁顯示,一般不用這種方法(數據量很大時,速度就慢)。

一般每次只要取出一定數量,比如說20條,點下頁的時候,再取一次。。。
cyz9977 2009-04-06
  • 打赏
  • 举报
回复
分页控件 Aspnetpager+存儲過程 就很好了,.net編程能省力就省~~

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧