分页功能的实现


利用Repeater实现分页功能


 public class DB
    {
        public static SqlConnection createConnection()
        {
            SqlConnection con = new SqlConnection("server=.;database=vote;User ID=sa;Password=123456");
            return con;
        }
    }


  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack )
            {
                this.Label3.Text = "1";
                this.dataBindToRepeater(); 
            }
        }

 private void dataBindToRepeater()
        {     
            int curPage = Convert.ToInt32(this.Label3 .Text);  //变量当前页
            SqlConnection con = DB.createConnection();
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = new SqlCommand("select * from voteMaster", con);
            DataSet ds = new DataSet();
            sda.Fill(ds, "voteID");
            System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource(); //实现分页功能
            ps.DataSource = ds.Tables["voteID"].DefaultView;
            ps.AllowPaging = true;   //允许分页
            ps.PageSize = 2;  //每页数据有2条
            ps.CurrentPageIndex = curPage - 1;  //当前页数
            this.Button3.Enabled = true;
            this.Button4.Enabled = true;
            if (curPage ==1)
            {
                this.Button3.Enabled = false; //上一页按钮不能用
            }
            if (curPage ==ps.PageCount )
            {
                this.Button4.Enabled = false;  //下一页按钮不能用
            }
            this.Repeater1.DataSource = ps;
            this.Repeater1.DataBind();
        }

 /// <summary>
   /// 下一页功能
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
        protected void Button4_Click(object sender, EventArgs e)
        {
            this.Label3.Text = (Convert.ToInt32(this.Label3.Text) + 1).ToString();
            this.dataBindToRepeater();
        }
        /// <summary>
        /// 上一页功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            this.Label3.Text = (Convert.ToInt32 (this.Label3.Text)-1).ToString() ;
            this.dataBindToRepeater();
        }
    }

<asp:Panel ID="Panel1" runat="server" Height="293px" Width="383px">
            <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                <ItemTemplate >
                    <%# DataBinder .Eval (Container .DataItem,"voteID")  %><%# DataBinder .Eval (Container .DataItem,"voteTitle")  %>
                    <br />
                </ItemTemplate>
                <AlternatingItemTemplate >
                    <font color="blue"><%# DataBinder .Eval (Container .DataItem,"voteID")  %><%# DataBinder .Eval (Container .DataItem,"voteTitle")  %>
                    <br />
                    </font>
                </AlternatingItemTemplate>
                <HeaderTemplate >
                    <h3>模板页眉</h3>
                </HeaderTemplate>
                <FooterTemplate >
                    <h3>模板页脚</h3>
                </FooterTemplate>
                <SeparatorTemplate >
                    <hr color="blue" size="1" />
                </SeparatorTemplate>
            </asp:Repeater>
        </asp:Panel>


最终实现效果:



利用GridView实现分页功能


将属性AllowPaging改为TRUE,PageSize自己设定一个数(我设定的为2)

 protected void Page_Load(object sender, EventArgs e)
        {
            Bind();
        }
        private void Bind()
        {
            SqlConnection con = DB.createConnection();
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = new SqlCommand("select * from voteMaster", con);
            DataSet ds = new DataSet();
            sda.Fill(ds, "voteID");
            this.GridView1.DataSource = ds.Tables["voteID"];
            this.GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

最终效果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值