本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

[学习公共库]Datalist分页代码
麦林 发表于 2007/7/18 16:20:28

界面源代码:<body>    <form id="form1" runat="server">        <asp:DataList ID="DataList" runat="server" CellPadding="4" ForeColor="#333333" Width="663px">            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />            <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />            <AlternatingItemStyle BackColor="White" ForeColor="#284775" />            <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />            <ItemTemplate>               类名称:<%# DataBinder.Eval(Container.DataItem,"mastername") %>            </ItemTemplate>        </asp:DataList><br />        共有&nbsp;<asp:Label ID="lblRecordCount" runat="server" ForeColor="Red" Width="39px"></asp:Label>条记录,当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页&nbsp;        <asp:Button ID="lbnPrevPage" runat="server" OnClick="lbnPrevPage_Click" Text="上一页" />        &nbsp;<asp:Button ID="lbnNextPage" runat="server" OnClick="lbnNextPage_Click" Text="下一页" />        &nbsp; &nbsp;&nbsp;</form></body> 实现代码:public partial class _Default : System.Web.UI.Page {    SqlConnection conn;    int pageSize,RecordCount,PageCount,CurrentPage;     protected void Page_Load(object sender, EventArgs e)    {        pageSize  = 8;        string MyConnString = "server= .;database=webSite;uid = sa;pwd = 123";        conn = new SqlConnection(MyConnString);        conn.Open();         if(!IsPostBack)        {            ListBind();            CurrentPage = 0;            ViewState["PageIndex"] = 0;             RecordCount = CalculateRecord();            lblRecordCount.Text = RecordCount.ToString();             PageCount = (RecordCount%pageSize>0)?(RecordCount/pageSize)+1:(RecordCount/pageSize);            lblPageCount.Text = PageCount.ToString();            ViewState["PageCount"] = PageCount;        }    }        public int CalculateRecord()        {            int intCount;            string strCount = "select count(*) as co from master";            SqlCommand cmd = new SqlCommand(strCount,conn);            SqlDataReader dr = cmd.ExecuteReader();            //SqlDataReader dr = cmd.EndExecuteReader();            if (dr.Read())            {                intCount = Int32.Parse(dr["co"].ToString());            }            else            {                intCount = 0;            }            dr.Close();            return intCount;        }     public void ListBind()    {         int StartIndex;        StartIndex = CurrentPage * pageSize;        string strSel = "select * from master";        DataSet ds = new DataSet();         SqlDataAdapter da = new SqlDataAdapter(strSel, conn);        da.Fill(ds, StartIndex, pageSize, "ok");         DataList.DataSource = ds.Tables["ok"].DefaultView;        DataList.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();    }     protected void lbnNextPage_Click(object sender, EventArgs e)    {        CurrentPage = (int)ViewState["PageIndex"];        PageCount = (int)ViewState["PageCount"];        if (CurrentPage < (PageCount - 1)) CurrentPage++;        ViewState["PageIndex"] = CurrentPage;        ListBind();     }    protected void lbnPrevPage_Click(object sender, EventArgs e)    {        CurrentPage = (int)ViewState["PageIndex"];        PageCount = (int)ViewState["PageCount"];        if (CurrentPage > 0) CurrentPage--;        ViewState["PageIndex"] = CurrentPage;        ListBind();    }}

阅读全文(4559) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
 

The Neurotic Fishbowl

.: 公告

fighting for the work!


Bloginess

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

.: 我的分类(专题)

首页(449)
文字感悟(103)
学习公共库(61)
生活体会(100)
资源收集(14)
.NET-Winform(27)
音乐文字(15)
心情小记(119)
SQL问题解决(10)


In the Bowl

.: 最新日志

C#打印代码
你看到的我是蓝色的
即将逝去的3月
这个假期
两个月过去
表名存在其他表时获取数据
条码打印
Remoting的用法
INI文件用法


.: 最新回复

回复:条码打印
回复:条码打印
回复:即将逝去的3月
回复:cookie对象使用
回复:条码打印
回复:JSP学习(字符串比较)
回复:应收应付核销规则及常见问题(续)
回复:两个月过去
回复:两个月过去


The Fishkeeper
blog名称:栗色?蓝色?
日志总数:449
评论数量:201
留言数量:37
访问次数:2260039
建立时间:2006年5月16日



Text Me

.: 留言板

签写新留言

链接完成
回:loseVC
来喽~
转转......
新年啦
对自己说
祝福
回:佑手
WO


Other Fish in the Sea

.: 链接


loseVC's BLOG
Collapsar_feel
布衣加针
C#and.Net
Kinogam Web




站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144778616 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号