概述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class product : System.Web.UI.Page
{
public static readonly string strconn = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
string tblName = "v_product"; //要查询表的表明
string fldName = "id"; //条件 自动增张的
// string orderStr = "asc"; //顺序 排序 如果是desc倒叙的话 需要改下
int PageSize = 25;
// protected DateTime dt; //定义时间差变量
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["username"] == null)
{
Response.Write("<script>window.open('login.aspx');</script>");
Response.Write("<script>window.close();</script>");
}
else
{
//开始显示第一页
//ShowDate(2);
ShowDate(Convert.ToInt32(lblCurrentPage.Text));
}
}
}
protected override void OnPreInit(EventArgs e)
{
// dt = DateTime.Now; //初始时间
base.OnPreInit(e);
}
//页面结束时间 并打印时间差
//protected override void OnLoadComplete(EventArgs e)
//{
// base.OnLoadComplete(e);
// TimeSpan ts = DateTime.Now - dt;
// Response.Write("加载页面用时:" + ts.TotalMilliseconds);
//}
protected void btnSearch_Click(object sender, EventArgs e)
{
txtToPage.Text = string.Empty;
lblCurrentPage.Text = "1";
ShowDate(Convert.ToInt32(lblCurrentPage.Text));
}
private void ShowDate(int page)
{
botten();
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string sql1 = string.Empty;
sql1 = "select count(*) from " + tblName + " where 1=1";
if (txtName.Text != "")
{
sql1 += "and (bm like '%" + txtName.Text + "%')";
}
// Response.Write(txtName.Text);
SqlCommand cmd = new SqlCommand(sql1, conn);
lblPageCount.Text = Convert.ToString(((int)cmd.ExecuteScalar() / PageSize + 1));//计算总页数
GridView1.PageIndex = Convert.ToInt32(lblPageCount.Text);//datadrid每次就显示一页,所有要手动加上总页
string sql = string.Empty;
if (txtName.Text.ToString() != "")//模糊搜索分页SQL
{
sql = "Select top " + PageSize + " id,bm,beizhu,bigname,smallname,name,danwei_name from " + tblName + " where " + fldName + " not in(select top " + (page - 1) * PageSize + " " + fldName + " from " + tblName + " where (bm like '%" + txtName.Text + "%'or name like '%" + txtName.Text + "%' or beizhu like '%" + txtName.Text + "%') order by " + fldName + " asc) ";
sql += "and (bm like '%" + txtName.Text + "%'or name like '%" + txtName.Text + "%' or beizhu like '%" + txtName.Text + "%')";
}
else
{//分页SQL
sql = "Select top " + PageSize + " id,bm,beizhu,bigname,smallname,name,danwei_name from " + tblName + " where " + fldName + " not in(select top " + (page - 1) * PageSize + " " + fldName + " from " + tblName + " order by " + fldName + " asc) ";
}
sql += "order by " + fldName + "";
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds,"gg");
GridView1.DataSource=ds.Tables["gg"];
GridView1.DataBind();
ds.Clear();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
/// dataGrid单击的页数
protected void GridView_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//GridView1.CurrentPageIndex = e.NewPageIndex;
lblCurrentPage.Text = Convert.ToString(e.NewPageIndex + 1);
ShowDate(e.NewPageIndex + 1);
}
//上一页
protected void lbtnBack_Click1(object sender, EventArgs e)
{
txtToPage.Text = string.Empty;
if (lblCurrentPage.Text != "1")
{
lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(lblCurrentPage.Text) - 1);
ShowDate(Convert.ToInt32(lblCurrentPage.Text));
}
}
//首页
protected void lbtnFirst_Click1(object sender, EventArgs e)
{
txtToPage.Text = string.Empty;
lblCurrentPage.Text = "1";
ShowDate(1);
}
//下一页
protected void lbtnNext_Click1(object sender, EventArgs e)
{
txtToPage.Text = string.Empty;
if (lblCurrentPage.Text != lblPageCount.Text)
{
lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(lblCurrentPage.Text) + 1);
ShowDate(Convert.ToInt32(lblCurrentPage.Text));
}
}
//尾页
protected void lbtnLast_Click1(object sender, EventArgs e)
{
txtToPage.Text = string.Empty;
lblCurrentPage.Text = lblPageCount.Text;
ShowDate(Convert.ToInt32(lblPageCount.Text));
}
//页面跳转
protected void btnToPage_Click1(object sender, EventArgs e)
{
if (Convert.ToInt32(txtToPage.Text.Trim()) > 0 && Convert.ToInt32(txtToPage.Text.Trim()) < Convert.ToInt32(lblPageCount.Text))
{
lblCurrentPage.Text = txtToPage.Text;
ShowDate(Convert.ToInt32(txtToPage.Text));
}
}
private void botten()
{
if (lblCurrentPage.Text == "1")
{
lbtnFirst.Enabled = false;
lbtnBack.Enabled = false;
lbtnLast.Enabled = true;
lbtnNext.Enabled = true;
}
if (lblCurrentPage.Text == lblPageCount.Text)
{
lbtnLast.Enabled = false;
lbtnNext.Enabled = false;
lbtnFirst.Enabled = true;
lbtnBack.Enabled = true;
}
if (lblCurrentPage.Text != "1" && lblCurrentPage.Text != lblPageCount.Text)
{
lbtnLast.Enabled = true;
lbtnNext.Enabled = true;
lbtnFirst.Enabled = true;
lbtnBack.Enabled = true;
}
}
}
最后
以上就是饱满耳机为你收集整理的经典的c# 分页程序 20W 数据实测 0.875秒的全部内容,希望文章能够帮你解决经典的c# 分页程序 20W 数据实测 0.875秒所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复