Repeater分页

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;

public partial class manage_main_mainnews : System.Web.UI.Page
{
    //当前页
    private int CurrentPage
    {
        get { return Convert.ToInt32(ViewState["CurrentPage"]); }
        set { ViewState["CurrentPage"] = value; }
    }
    //记录总数
    private int ItemCount
    {
        get { return Convert.ToInt32(ViewState["ItemCount"]); }
        set { ViewState["ItemCount"] = value; }
    }
    //页总数
    private int PageCount
    {
        get { return Convert.ToInt32(ViewState["PageCount"]); }
        set { ViewState["PageCount"] = value; }
    }
    //分页数
    public static int PageSize = 18;
    string fontColor = "#FF4500";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["flag"] == null)
        {
            Response.Redirect("../login.aspx");
        }
        else
        {
            if (!Page.IsPostBack)
            {
                ItemCount = DataAccess.GetNewsListCount();
                if (ItemCount % PageSize != 0)
                {
                    PageCount = ItemCount / PageSize + 1;
                }
                else
                {
                    PageCount = ItemCount / PageSize;
                }
                if (PageCount % 5 != 0)
                {
                    SectionCount = PageCount / 5 + 1;
                }
                else
                {
                    SectionCount = PageCount / 5;
                }

                btn_first.Enabled = false;
                btn_previous.Enabled = false;

                if (PageCount <= 5)
                {
                    btn_first.Enabled = false;
                    btn_previous.Enabled = false;
                    btn_next.Enabled = false;
                    btn_last.Enabled = false;
                }

                this.lblItemCount.Text = ItemCount.ToString();
                this.lblPageCount.Text = PageCount.ToString();
                CurrentPage = 1;
                CurrentSection = 1;
                GetPageList();
                GetCountPageVisible();
            }
        }
    }

    //绑字数据到页面
    protected void GetCurrentContent(int currentPage, int pageSize)
    {
        this.Repeater1.DataSource = DataAccess.GetNewsList(currentPage, pageSize);
        this.Repeater1.DataBind();
    }

    //首页
    protected void btn_first_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        CurrentSection = 1;
        btn_next.Enabled = true;
        btn_last.Enabled = true;
        btn_first.Enabled = false;
        btn_previous.Enabled = false;
        GetPageList();
        GetCountPageVisible();

    }
    //上一页
    protected void btn_previous_Click(object sender, EventArgs e)
    {
        CurrentPage = CurrentSection * 5 - 9;
        CurrentSection--;
        btn_next.Enabled = true;
        btn_last.Enabled = true;
        if (CurrentSection == 1)
        {
            btn_first.Enabled = false;
            btn_previous.Enabled = false;
        }
        GetPageList();
        GetCountPageVisible();
    }
    //下一页
    protected void btn_next_Click(object sender, EventArgs e)
    {
        CurrentPage = CurrentSection * 5 + 1;
        CurrentSection++;
        btn_previous.Enabled = true;
        btn_first.Enabled = true;
        if (CurrentSection == SectionCount)
        {
            btn_last.Enabled = false;
            btn_next.Enabled = false;
        }
        GetPageList();
        GetCountPageVisible();
    }
    //最后一页
    protected void btn_last_Click(object sender, EventArgs e)
    {
        CurrentSection = SectionCount;
        CurrentPage = PageCount;
        btn_previous.Enabled = true;
        btn_first.Enabled = true;
        btn_last.Enabled = false;
        btn_next.Enabled = false;
        GetPageList();
        GetCountPageVisible();
    }

    //前台页码数处理函数
    protected void p(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        int num = Convert.ToInt32(lb.Text);
        int c = num % 5 > 0 ? num % 5 : 5;
        LinkButton l = (LinkButton)Page.FindControl("p" + c.ToString());
        l.ForeColor = System.Drawing.Color.FromName(fontColor);
        CurrentPage = Convert.ToInt32(l.Text);
        GetCurrentContent(CurrentPage, PageSize);
    }

    //前台页码数列表函数
    private void GetPageList()
    {
        if (CurrentSection > 1)
        {
            p1.Text = Convert.ToString(5 * (CurrentSection - 1) + 1);
            p2.Text = Convert.ToString(5 * (CurrentSection - 1) + 2);
            p3.Text = Convert.ToString(5 * (CurrentSection - 1) + 3);
            p4.Text = Convert.ToString(5 * (CurrentSection - 1) + 4);
            p5.Text = Convert.ToString(5 * (CurrentSection - 1) + 5);
        }
        else
        {
            p1.Text = "1";
            p2.Text = "2";
            p3.Text = "3";
            p4.Text = "4";
            p5.Text = "5";
        }

        LinkButton l = (LinkButton)Page.FindControl("p1");
        l.ForeColor = System.Drawing.Color.FromName(fontColor);
    }

    //隐藏没用的页码数
    protected void GetCountPageVisible()
    {
        if (ItemCount == 0)
        {
            for (int s = 1; s <= 5; s++)
            {
                LinkButton l = (LinkButton)Page.FindControl("p" + Convert.ToString(s));
                l.Visible = false;
            }
        }
        else
        {
            if (PageCount % 5 == 0 || CurrentSection != SectionCount)
            {
                for (int s = 1; s <= 5; s++)
                {
                    LinkButton l = (LinkButton)Page.FindControl("p" + Convert.ToString(s));
                    l.Visible = true;
                }
            }
            else
            {
                for (int t = (PageCount % 5); t < 5; t++)
                {
                    LinkButton l = (LinkButton)Page.FindControl("p" + Convert.ToString(t + 1));
                    l.Visible = false;
                }
            }
        }
        GetCurrentContent(CurrentPage, PageSize);
    }

    public string GetPartitionString(object o, int length)
    {
        if (!string.IsNullOrEmpty(o.ToString()))
        {
            if (o.ToString().Length <= length)
            {
                return o.ToString();
            }
            else
            {
                return o.ToString().Substring(0, length) + "...";
            }
        }
        else
        {
            return "";
        }
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = int.Parse(e.CommandArgument.ToString());
        if (e.CommandName == "update")
        {
            int mark = 0;
            CheckBox chx = (CheckBox)e.Item.FindControl("checkMark");
            if (chx.Checked)
            {
                mark = 1;
            }
            if (DataAccess.UpdateNews2(id, mark))
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>window.alert('修改成功!')</script>", false);
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>window.alert('修改失败!')</script>", false);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值