Datalist数据绑定分页

前台代码:

<form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" RepeatColumns="1"
            RepeatDirection="Horizontal"   BackColor="White"
            GridLines="Horizontal" BorderColor="#CCCCCC" BorderStyle="None"
            BorderWidth="10px" CellPadding="4" ForeColor="Black"    Width="1350px"
            ondeletecommand="DataList1_DeleteCommand" DataKeyField="A_id">
           
             <HeaderTemplate> 
             <table border="0" cellspacing="1" cellpadding="4" align="center" bgcolor="#94cef5"    width="99%"> 
                 <caption style="font-size:20px;  "> 活动信息列表 </caption>
                <tr>
                    <td align="left">
                        <asp:Image ID="Image1" runat="server" ImageUrl="~/images/news1.jpg"
                            Width="10px" />
                 <b>活动标题</b>
                           
                    </td>
                    <td align="right">
                        <asp:Label ID="Labelposttime" runat="server"  Text='发布时间'></asp:Label>
                    </td>

                      <td align="right" style=" width:50px">
                       <b>操作</b>
                     </td>
                </tr>            
            </table>       
              </HeaderTemplate> 
             
        <ItemTemplate>
           <table border="0" cellspacing="1" cellpadding="4" align="center"    width="99%"> 
                <tr>
                    <td align="left">
                        &nbsp;<asp:Image ID="Image1" runat="server" ImageUrl="~/images/news1.jpg"
                            Width="10px" />
                        <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("A_title") %>' 
                            NavigateUrl='<%# "../newsview.aspx?pid="+Eval("A_id") %>'></asp:HyperLink>
                           
                    </td>
                    <td align="right">
                        <asp:Label ID="Labelposttime" runat="server"  Text='<%#Eval("A_posttime") %>'></asp:Label>
                    </td>

                      <td align="right" style=" width:50px">
                      <asp:LinkButton ID="lnkDel" runat="server" CommandArgument='<%#Eval("A_id") %>' CommandName="delete">删除</asp:LinkButton>
                     </td>
                </tr>
            </table>
            </ItemTemplate>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <SelectedItemStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#94cef5"  />
        </asp:DataList>
 
        <div align="center"><font face="宋体" >
 
            <asp:linkbutton id="FirstLB" runat="server" OnCommand="LinkButton_Click" CommandName="first">第一页</asp:linkbutton>&nbsp;
             <asp:linkbutton id="PreviousLB" runat="server" OnCommand="LinkButton_Click" CommandName="prev">上一页</asp:linkbutton>&nbsp;
             <asp:linkbutton id="NextLB" runat="server" OnCommand="LinkButton_Click" CommandName="next">下一页</asp:linkbutton>&nbsp;
             <asp:linkbutton id="EndLB" runat="server" OnCommand="LinkButton_Click" CommandName="end">最后一页</asp:linkbutton>&nbsp;&nbsp;
             总<asp:label id="TotalLbl" runat="server"></asp:label>页 当前第<asp:label id="CurrentLbl" runat="server"></asp:label>页
             <asp:linkbutton id="JumpLB" runat="server" OnCommand="LinkButton_Click" CommandName="jump">跳到</asp:linkbutton>第
             <asp:textbox id="TextBox1" runat="server" Width="90px"></asp:textbox>
             页</font></div>
   
    </div>
    </form>

 

 

 

后台代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class managenews_newslist : System.Web.UI.Page
{
    A_articleBLL BLL = new A_articleBLL();
    int CurrentPage;//当前页数
    int PageSize;   //每页条数
    int PageCount;  //总页数
    int RecordCount;//总条数
    protected void Page_Load(object sender, EventArgs e)
    {
        PageSize = 10;//每页10条记录
        if (!IsPostBack)
        {
            CurrentPage = 0;//当前页习惯设为0
            ViewState["PageIndex"] = 0;//页索引也设为0
            //计算总共有多少记录
            RecordCount = CalculateRecord();
            //计算总共有多少页
            if (RecordCount % PageSize == 0)
            {
                PageCount = RecordCount / PageSize;
            }
            else
            {
                PageCount = RecordCount / PageSize + 1;
            }

            this.TotalLbl.Text = PageCount.ToString();//显示总页数
            ViewState["PageCount"] = PageCount;//
            bindnews();
        }
    }

    #region 绑定活动信息
    /// <summary>
    /// 绑定活动信息
    /// </summary>
    public void bindnews()
    {
        try
        {
            int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
             DataTable DT = new DataTable();
            string sql0 = "select top " + PageSize + " * from A_article where A_ID not in(select top " + PageSize * CurrentPage + " A_ID from A_article order by A_ID desc) order by A_ID desc";
            //Response.Write(sql0);
            DT = BLL.SelectsqlNews(sql0);
            DataList1.DataSource = DT;
            DataList1.DataBind();

            this.PreviousLB.Enabled = true;
            this.NextLB.Enabled = true;
            if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
            if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
            this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    #endregion


    private int CalculateRecord()
    {
        try
        {
            int recordCount;

            string sql = "select count(*) as count from A_article";

            SqlDataReader sdr = BLL.CalculateRecord(sql);

            if (sdr.Read())
            {
                recordCount = Int32.Parse(sdr["count"].ToString());
            }
            else
            {
                recordCount = 0;
            }
            sdr.Close();
            return recordCount;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }


    public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
    {
        CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
        PageCount = (int)ViewState["PageCount"];//获得总页数


        string cmd = e.CommandName;

        //判断cmd,以判定翻页方向


        switch (cmd)
        {
            case "prev"://上一页
                if (CurrentPage > 0) CurrentPage--;
                break;

            case "next":
                if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
                break;

            case "first"://第一页
                CurrentPage = 0;
                break;

            case "end"://最后一页
                CurrentPage = PageCount - 1;
                break;

            case "jump"://跳转到第几页
                if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
                {
                    return;
                }
                else
                {
                    CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
                    break;
                }
        }
        ViewState["PageIndex"] = CurrentPage;//获得当前页

        bindnews();//重新将DataList绑定到数据库


    }
 


    #region 删除活动信息

    /// <summary>
    /// 删除活动信息
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
        string mes=BLL.articledel(id);
        if(mes=="")
        {
            mes="删除活动信息成功!";
        }
        Response.Write("<script>alert('"+ mes +"')</script>");
        bindnews();
    }
    #endregion


}
运行截图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值