内存分页

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class userinfo : System.Web.UI.Page
{
    public string UsersIp = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault<IPAddress>(a => a.AddressFamily.ToString().Equals("InterNetwork")).ToString();
    public DateTime UsersTime = DateTime.Now;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
    private void BindData()
    {
        string sql = "select * from Users where 1 = 1 ";
        if (TextBox1.Text.Trim().Length > 0)
        {
            sql += "and UserName ='" + TextBox1.Text.Trim() + "'";
        }
        DataTable dt = SqlHelp.ExecuteDataTable(sql, null);//读取数据 内存数据表中
        AspNetPager1.RecordCount = dt.Rows.Count; //分页控件总记录数等于数据总记录数

        PagedDataSource psd = new PagedDataSource(); //数据绑定对象
        psd.DataSource = dt.DefaultView;//数据源=默认视图
        psd.AllowPaging = true;  //数据绑定启用
        psd.PageSize = AspNetPager1.PageSize;//要显示的数目
        psd.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;//当前页的索引

        rep1.DataSource = psd; //绑定数据源
        rep1.DataBind();
    }
}

 

posted @ 2017-11-14 15:41  enych  阅读(520)  评论(0编辑  收藏  举报