asp.net分页功能的实现
效果图:
代码:
static int PageSize = 30; #region 分页部分代码
//*********************************** 数据分页 ********************************
protected void FirstPage_Click1(object sender, EventArgs e)
{
int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数
DataTable dt = GetListByPage(ViewState["ssearchCondition"].ToString(),,PageSize);
int PageCount = RecordCount % PageSize > ? RecordCount / PageSize + : RecordCount / PageSize;
int CurrentPageNum;
if (PageCount == )
{
CurrentPageNum = ;
}
else
{
CurrentPageNum = ;
}
CurrentPage.Text = (CurrentPageNum).ToString();
TotalPageCount.Text = PageCount.ToString();
EnableJudger(); ViewState["PageCount"] = PageCount;
ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = ;
if (int.Parse(ViewState["PageCount"].ToString()) > )
{
rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - ) * PageSize;
}
dt.Columns.Add(new DataColumn("rowN"));
for (int i = ; i < dt.Rows.Count; i++)
{
dt.Rows[i]["rowN"] = rowNum++;
} Repeater1.DataSource = dt;
Repeater1.DataBind();
} protected void PrePage_Click(object sender, EventArgs e)
{
int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数
int PageCount = RecordCount % PageSize > ? RecordCount / PageSize + : RecordCount / PageSize;
int CurrentPageNum = (int)ViewState["CurrentPageNum"];
DataTable dt = GetListByPage(ViewState["ssearchCondition"].ToString(), PageSize * (CurrentPageNum - ) + , PageSize * (CurrentPageNum - ) + PageSize);
CurrentPageNum -= ;
CurrentPage.Text = (CurrentPageNum).ToString();
TotalPageCount.Text = PageCount.ToString();
EnableJudger(); ViewState["PageCount"] = PageCount;
ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = ;
if (int.Parse(ViewState["PageCount"].ToString()) > )
{
rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - ) * PageSize;
}
dt.Columns.Add(new DataColumn("rowN"));
for (int i = ; i < dt.Rows.Count; i++)
{
dt.Rows[i]["rowN"] = rowNum++;
} Repeater1.DataSource = dt;
Repeater1.DataBind();
} protected void NextPage_Click(object sender, EventArgs e)
{
int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数
int PageCount = RecordCount % PageSize > ? RecordCount / PageSize + : RecordCount / PageSize;
int CurrentPageNum = (int)ViewState["CurrentPageNum"];
DataTable dt = GetListByPage(ViewState["ssearchCondition"].ToString(), PageSize * (CurrentPageNum) + , PageSize * (CurrentPageNum) + PageSize);
CurrentPageNum += ;
CurrentPage.Text = (CurrentPageNum).ToString();
TotalPageCount.Text = PageCount.ToString();
EnableJudger(); ViewState["PageCount"] = PageCount;
ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = ;
if (int.Parse(ViewState["PageCount"].ToString()) > )
{
rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - ) * PageSize;
}
dt.Columns.Add(new DataColumn("rowN"));
for (int i = ; i < dt.Rows.Count; i++)
{
dt.Rows[i]["rowN"] = rowNum++;
} Repeater1.DataSource = dt;
Repeater1.DataBind();
} protected void LastPage_Click(object sender, EventArgs e)
{
int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数
int PageCount = RecordCount % PageSize > ? RecordCount / PageSize + : RecordCount / PageSize; DataTable dt = GetListByPage(ViewState["ssearchCondition"].ToString(), PageSize * (PageCount - ) + , PageSize * (PageCount - ) + PageSize);
int CurrentPageNum = PageCount;
CurrentPage.Text = PageCount.ToString();
TotalPageCount.Text = PageCount.ToString();
EnableJudger(); ViewState["PageCount"] = PageCount;
ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = ;
if (int.Parse(ViewState["PageCount"].ToString()) > )
{
rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - ) * PageSize;
}
dt.Columns.Add(new DataColumn("rowN"));
for (int i = ; i < dt.Rows.Count; i++)
{
dt.Rows[i]["rowN"] = rowNum++;
} Repeater1.DataSource = dt;
Repeater1.DataBind();
} protected void lbtnGoPage_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtGoPage.Text))
{
return;
}
int gotoPageNum = int.Parse(txtGoPage.Text);
int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数
DataTable dt = new DataTable();
int CurrentPageNum = gotoPageNum;
int PageCount = RecordCount % PageSize > ? RecordCount / PageSize + : RecordCount / PageSize;
if (gotoPageNum < || gotoPageNum > PageCount)
{
}
else
{
dt = GetListByPage(ViewState["ssearchCondition"].ToString(), PageSize * (CurrentPageNum - ) + , PageSize * (CurrentPageNum - ) + PageSize);
CurrentPage.Text = (gotoPageNum).ToString();
TotalPageCount.Text = PageCount.ToString();
EnableJudger(); ViewState["PageCount"] = PageCount;
ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = ;
if (int.Parse(ViewState["PageCount"].ToString()) > )
{
rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - ) * PageSize;
}
dt.Columns.Add(new DataColumn("rowN"));
for (int i = ; i < dt.Rows.Count; i++)
{
dt.Rows[i]["rowN"] = rowNum++;
} Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
private void EnableJudger()
{
//1. 没有数据
if (int.Parse(TotalPageCount.Text) <= )
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
NextPage.Enabled = false;
LastPage.Enabled = false;
lbtnGoPage.Enabled = false;
CurrentPage.Text = "";
return;
}
//2. 只有一页数据
if (int.Parse(CurrentPage.Text) == && int.Parse(TotalPageCount.Text) == )
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
NextPage.Enabled = false;
LastPage.Enabled = false;
lbtnGoPage.Enabled = false;
return;
}
//3. 当前为第一页,总页数大于1
if (int.Parse(CurrentPage.Text) == && int.Parse(TotalPageCount.Text) > )
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
NextPage.Enabled = true;
LastPage.Enabled = true;
lbtnGoPage.Enabled = true;
return;
}
//4. 当前页为中间页
if (int.Parse(CurrentPage.Text) > && int.Parse(CurrentPage.Text) < int.Parse(TotalPageCount.Text))
{
FirstPage.Enabled = true;
PrePage.Enabled = true;
NextPage.Enabled = true;
LastPage.Enabled = true;
lbtnGoPage.Enabled = true;
return;
}
//5.当前页为最后一页,总页数大于1
if (int.Parse(CurrentPage.Text) == int.Parse(TotalPageCount.Text) && int.Parse(CurrentPage.Text) > )
{
FirstPage.Enabled = true;
PrePage.Enabled = true;
NextPage.Enabled = false;
LastPage.Enabled = false;
return;
} } /// <summary>
/// 获取记录总数
/// </summary>
public int GetRecordCount(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select count(1) from tb_ProjectRegister");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
object obj = GetSingle(strSql.ToString());
if (obj == null)
{
return ;
}
else
{
return Convert.ToInt32(obj);
}
}
public static object GetSingle(string SQLString)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NMXT_DBConn"].ToString()))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
/// <summary>
/// 分页获取数据列表
/// </summary>
public DataTable GetListByPage(string strWhere, int startIndex, int endIndex)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT * FROM ( ");
strSql.Append(" SELECT ROW_NUMBER() OVER (");
strSql.Append("order by T.roleId asc,T.replyEnd desc");
strSql.Append(")AS Row, T.* from tb_ProjectRegister T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
strSql.Append(" WHERE " + strWhere);
}
strSql.Append(" ) TT");
strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
DataSet ds = Query(strSql.ToString()); DataTable newDt = ds.Tables[].Copy();
DataColumn newCol1 = new DataColumn("Description");
DataColumn newCol2 = new DataColumn("newCol2");
DataColumn newCol3 = new DataColumn("newCol3");
DataColumn newCol4 = new DataColumn("newCol4");
DataColumn newCol5 = new DataColumn("newCol5");
DataColumn newCol6 = new DataColumn("newCol6");
DataColumn newCol7 = new DataColumn("newCol7");
DataColumn newCol8 = new DataColumn("newCol8");
newDt.Columns.Add(newCol1);
newDt.Columns.Add(newCol2);
newDt.Columns.Add(newCol3);
newDt.Columns.Add(newCol4);
newDt.Columns.Add(newCol5);
newDt.Columns.Add(newCol6);
newDt.Columns.Add(newCol7);
newDt.Columns.Add(newCol8);
Dictionary<string, string> newDic = GetRoleDescriptions();
if (newDic == null)
{
foreach (DataRow row in newDt.Rows)
{
row["Description"] = ""; if (Convert.ToDateTime(row["applyStart"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol2"] = "";
}
else
{
row["newCol2"] = Convert.ToDateTime(row["applyStart"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["applyEnd"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol3"] = "";
}
else
{
row["newCol3"] = Convert.ToDateTime(row["applyEnd"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["replyStart"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol4"] = "";
}
else
{
row["newCol4"] = Convert.ToDateTime(row["replyStart"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["replyEnd"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol5"] = "";
}
else
{
row["newCol5"] = Convert.ToDateTime(row["replyEnd"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["realEnd"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol6"] = "";
}
else
{
row["newCol6"] = Convert.ToDateTime(row["realEnd"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["acceptDate_zdb"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol7"] = "";
}
else
{
row["newCol7"] = Convert.ToDateTime(row["acceptDate_zdb"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["acceptDate_zjb"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol8"] = "";
}
else
{
row["newCol8"] = Convert.ToDateTime(row["acceptDate_zjb"]).ToString("yyyy-MM-dd");
}
}
}
else
{
foreach (DataRow row in newDt.Rows)
{
if (newDic.ContainsKey(row["roleId"].ToString()))
{
row["Description"] = newDic[row["roleId"].ToString()];
}
else
{
row["Description"] = "";
} if (Convert.ToDateTime(row["applyStart"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol2"] = "";
}
else
{
row["newCol2"] = Convert.ToDateTime(row["applyStart"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["applyEnd"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol3"] = "";
}
else
{
row["newCol3"] = Convert.ToDateTime(row["applyEnd"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["replyStart"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol4"] = "";
}
else
{
row["newCol4"] = Convert.ToDateTime(row["replyStart"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["replyEnd"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol5"] = "";
}
else
{
row["newCol5"] = Convert.ToDateTime(row["replyEnd"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["realEnd"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol6"] = "";
}
else
{
row["newCol6"] = Convert.ToDateTime(row["realEnd"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["acceptDate_zdb"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol7"] = "";
}
else
{
row["newCol7"] = Convert.ToDateTime(row["acceptDate_zdb"]).ToString("yyyy-MM-dd");
} if (Convert.ToDateTime(row["acceptDate_zjb"]).ToString("yyyy-MM-dd") == "1900-01-01")
{
row["newCol8"] = "";
}
else
{
row["newCol8"] = Convert.ToDateTime(row["acceptDate_zjb"]).ToString("yyyy-MM-dd");
}
}
}
return newDt;
} public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NMXT_DBConn"].ToString()))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
///********************************* 数据分页结束 ********************************
#endregion
asp.net分页功能的实现的更多相关文章
- ASP.NET--Repeater控件分页功能实现
这两天由于‘销售渠道’系统需要实现新功能,开发了三个页面,三个界面功能大致相同. 功能:分页显示特定sql查询结果,点击上一页下一页均可显示.单击某记录可以选定修改某特定字段<DropDownL ...
- Asp.net MVC3表格共用分页功能
在建立的mvc3项目中,在Razor(CSHTML)视图引擎下,数据会在表格中自动的生成,但分页没有好的控件实现,这里我们开发了设计了一个分页的模板,适合于没有数据提交和有数据提交的分页的分页. 第一 ...
- jPList – 实现灵活排序和分页功能的 jQuery 插件
jPList 是一个灵活的 jQuery 插件,可以用于任何 HTML 结构的排序,分页和筛选.它支持的数据源包括:PHP + MySQL,ASP.NET + SQL Server,PHP + SQL ...
- asp.net 分页-自己写分页控件
去年就发表过asp.net 分页-利用后台直接生成html分页 ,那种方法只是单纯的实现了分页,基本不能使用,那时就想写个自己的分页控件,无奈能力有限.最近有点时间了,就自己做出了这个分页控件.我承认 ...
- GridView使用自带分页功能时分页方式及样式PagerStyle
// 转向地址:http://www.bubuko.com/infodetail-412562.html GridView分页,使用自带分页功能,类似下面样式: 在aspx页面中,GridView上的 ...
- Repeater控件使用(含删除,分页功能)
Repeater控件使用(含删除,分页功能) 摘自:http://www.cnblogs.com/alanliu/archive/2008/02/25/914779.html 前臺代碼 <%@ ...
- 《ASP.NET1200例》<asp:DataList>分页显示图片
aspx页面代码 <asp:DataList ID="dlPhoto" runat="server" Height="137px" W ...
- aspnetpager+repeater+oracle实现分页功能
一.设计原理阐述 数据查询分页,这个功能相信大家都很熟悉,通过数据库或其它数据源进行查询操作后,将获得的数据显示到界面上,但是由于数据量太大,不能一次性完全的显示出来,就有了数据分页的需求.这个需求在 ...
- ASP.NET分页存储过程,解决搜索时丢失条件信息
存储过程: -- ============================================= -- Author: -- Create date: -- Description: 分页 ...
随机推荐
- hdu 4723 How Long Do You Have to Draw(贪心)
How Long Do You Have to Draw Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- linq-to-sql实现left join,group by,count
linq-to-sql实现left join,group by,count 用linq-to-sql实现下面的sql语句: SELECT p.ParentId, COUNT(c.ChildId) FR ...
- Sql Server分页分段查询百万级数据四种项目实例
实际项目中需要实现自定义分页,最关键第一步就是写分页SQL语句,要求语句效率要高. 那么本文的一个查询示例是查询第100000-100050条记录,即每页50条的结果集.查询的表名为infoTab,且 ...
- 深入理解Tomcat虚拟文件夹
我们知道,Web站点中的内容(包含网页,图片,音频文件等)一般都存放在App的文件夹下.但随着站点内容的不断丰富,用户须要把不同层次的内容组织成站点的子文件夹. 我们通常的做法是在站点主文件夹 ...
- git服务器gitlab之搭建和使用
git服务器比较有名的是gitosis和gitolite,这两个管理和使用起来稍微有些复杂,没有web页面,而gitlab则是类似于github的一个工具,github无法免费建立私有仓库,并且为了代 ...
- crontab执行脚本与手动执行结果不一致
反正网上说是环境变量问题,我就直接在脚本第二行加入以下代码: source /etc/profile source ~/.bashrc 问题是解决了!
- Python内置函数之len()
len(s)用来判断对象的长度. 需要说明的是,整型,布尔等是没有长度这一说法的.字符串.字典.列表和元组都有长度. 例子: >>> len() Traceback (most re ...
- unity3d 动画卡帧 动画合成 动画层次
2013-02-26 16:22 2059人阅读 评论(0) 收藏 举报 unity3d 中动画的添加 http://unity3d.com/support/documentation/Manua ...
- JVM内存监控:visualVM jconsole jstatd jmap
本文是亲自测试的详细配置过程,不是转载而且linux下不需修改/etc/hosts文件 由于在建项目的需要,监控tomcat的内存使用,检查内存泄漏的情况.其实JDK自身已经提供了很多工具,都在JAV ...
- 【Mac系统 + Python + Django】之开发一个发布会系统【Django模型(三)】
上一部分给大家介绍Django的视图. 接下来继续来了解Django框架,来看第三部分,此部分是对数据库的操作. 目录: 一.设计系统表 二.admin后台管理 三.基本数据访问(SQLite数据库) ...