DataList控件实现分页

本文介绍了一个使用ASP.NET和SQL Server实现新闻列表分页显示的例子。通过DataList控件结合SQL查询,实现了新闻信息的有效展示及前后页切换功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前台页面:
    <asp:DataList ID="DataList1" runat="server" Width="600px">
    <ItemTemplate>      
       <table id="tabNewInfo">
       <tr>
         <td><span>发布时间:</span><%#Eval("post_time") %></td>
         <td></td>
         <td><%#Eval("post_admin") %></td>
       </tr>
       <tr>
         <td><span>标题:</span><%#Eval("title") %></td>
         <td><span>作者:</span><%#Eval("writer") %></td>
         <td><span>来源:</span><%#Eval("new_source") %></td>
       </tr>
       </table>
       <table id="tabNewContent">
       <tr>
       <td>
       <%#Eval("new_contect") %>
       </td>
       </tr>
       </table>
    </ItemTemplate>
    <SeparatorTemplate>
    <hr />
    </SeparatorTemplate>
    </asp:DataList>
    <br />
        总共<asp:Label ID="lblRecordCount" runat="server" Text="Label"></asp:Label>条记录
        当前是第<asp:Label ID="lblCurrentPage" runat="server" Text="Label"></asp:Label>页
        总共有<asp:Label ID="lblPageCount" runat="server" Text="Label"></asp:Label>页
        <br />
        <asp:LinkButton ID="lbnPrevPage" CommandName="Prev" runat="server" OnCommand="Page_OnClick">上一页</asp:LinkButton>
        <asp:LinkButton ID="lbnNextPage" CommandName="Next" runat="server" OnCommand="Page_OnClick">下一页</asp:LinkButton>
</div> 

cs文件代码:

using System.Configuration;
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;
using System.IO;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    int PageSize, CurrentPage, PageCount, RecordCount;
    SqlConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
        PageSize = 3;
        conn = new SqlConnection("Data source=Aioros;Initial Catalog=GamsjoyDb;Persist Security Info=true;User ID=sa;Password=sa");
        conn.Open();
        if (!Page.IsPostBack)
        {

            ListBind();
            CurrentPage = 0;
            ViewState["PageIndex"] = 0;
            RecordCount = CalculateRecord();
            lblRecordCount.Text = RecordCount.ToString();
            if (RecordCount % PageSize == 0)
            {
                PageCount = RecordCount / PageSize;
            }
            else
            {
                PageCount = RecordCount / PageSize + 1;
            }
            lblPageCount.Text = PageCount.ToString();
            ViewState["PageCount"] = PageCount;
        }
    }
    public int CalculateRecord() //返回总的记录行数
    {
        int intCount;
        string strCount = "Select count(*) as icount from news";
        SqlCommand MyComm = new SqlCommand(strCount, conn);
        SqlDataReader dr = MyComm.ExecuteReader();
        if (dr.Read())
        {
            intCount = Int32.Parse(dr["icount"].ToString());
        }
        else
        {
            intCount = 0;
        }
        dr.Close();
        return intCount;
    }
    ICollection CreateSource()//获取记录
    {
        int StartIndex;
        StartIndex = CurrentPage * PageSize;
        string strSel = "Select * from news orDER BY id DESC";
        DataSet ds = new DataSet();
        ds.Clear();
        SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, conn);
        MyAdapter.Fill(ds, StartIndex, PageSize, "news");
        return ds.Tables["news"].DefaultView;
    }
    public void ListBind()//绑定数据源到DataList控件
    {
        DataList1.DataSource = CreateSource();
        DataList1.DataBind();
        lbnNextPage.Visible = true;
        lbnPrevPage.Visible = true;
        if (CurrentPage == (PageCount - 1))
        {
            lbnNextPage.Visible = false;
        }

        if (CurrentPage == 0)
        {
            lbnPrevPage.Visible = false;
        }
        int x;
        x = CurrentPage + 1;
        lblCurrentPage.Text = x.ToString();
    }
    public void Page_OnClick(object sender, CommandEventArgs e)
    {
        CurrentPage = (int)ViewState["PageIndex"];
        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["PageIndex"] = CurrentPage;
        ListBind();
    }

 

原文地址:http://www.emeralddream.net/article.asp?id=36

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值