发布于 2016-05-28 04:38:57 | 100 次阅读 | 评论: 1 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


最近分页方面根据实际需要修改了一些函数
基本函数如下:

/// <summary> 
/// 需要分页时使用,根据参数和ConditionExpress获取DataTable 
/// </summary> 
/// <param name="_tableName">表名</param> 
/// <param name="_fieldNames">字段名集合,用逗号分开</param> 
/// <param name="_OrderColumn">排序字段,用于统计有多少条记录</param> 
/// <param name="IsDesc">是否倒序</param> 
/// <param name="_indexColumn">自增字段名</param> 
/// <param name="_currentPage">当前页</param> 
/// <param name="pageSize">页大小</param> 
/// <param name="_rowsCount">总记录数</param> 
/// <returns>获取到的DataTable</returns> 
public static DataTable GetDataTable(string _tableName, string _fieldNames, string _OrderColumn, bool IsDesc, string _indexColumn, int _currentPage, int pageSize, string conditionExpress, ref int _rowsCount) 
{ 
using (SqlConnection conn = new SqlConnection(connectionString)) 
{ 
string whereStr = " where 1=1 "; 
string sort = IsDesc ? " desc" : " asc"; 

string sqlStr = " from " + _tableName; 
//排序字段 
string orderStr = " order by " + _OrderColumn + sort; 
if (_OrderColumn != _indexColumn) 
orderStr += "," + _indexColumn + sort; 
if (conditionExpress != string.Empty) 
{ 
whereStr += conditionExpress; 
} 
sqlStr += whereStr; 

//取得符合条件的数据总数 
SqlCommand cmd = new SqlCommand("select count(" + _OrderColumn + ") " + sqlStr, conn); 
conn.Open(); 
try 
{ 
_rowsCount = (int)cmd.ExecuteScalar(); 
} 
catch (Exception ex) 
{ 
throw new Exception(ex.Message); 
} 

if (_currentPage > _rowsCount) _currentPage = _rowsCount; 

if (_currentPage > 1) 
{ 
if (IsDesc) 
sqlStr += " and " + _OrderColumn + " < (select MIN(" + _OrderColumn + ") from "; 
else 
sqlStr += " and " + _OrderColumn + " > (select MAX(" + _OrderColumn + ") from "; 
sqlStr += "(select top " + (pageSize * (_currentPage - 1)) + " " + _OrderColumn + " from " + _tableName + whereStr + orderStr + ") as t)"; 
} 
sqlStr = "select top " + pageSize + " " + _fieldNames + sqlStr + orderStr; 

try 
{ 
DataSet ds = new DataSet(); 
SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn); 
da.Fill(ds); 
return ds.Tables[0]; 
} 
catch (Exception EX) 
{ 
throw new Exception(EX.Message); 
} 
} 
} 

调用如下:

private void bind() 
{ 
int rowCount = 1; 
string wherestr = string.Empty; 
//设置分页 
anPager.AlwaysShow = true; 
anPager.PageSize = 10; 
this.rptdictionary.DataSource = GetDataTable( 
"dictionary_Toysgogo_", 
"[id_dictionary_],[namecn_dictionary_],[nameen_dictionary_],[point_dictionary_]", 
"[id_dictionary_]", 
true, 
"[id_dictionary_]", 
this.anPager.CurrentPageIndex, 
anPager.PageSize, 
wherestr, 
ref rowCount 
); 
this.anPager.RecordCount = rowCount; 
this.rptdictionary.DataBind(); 
} 


//分页切换 
protected void anPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) 
{ 
this.anPager.CurrentPageIndex = e.NewPageIndex; 
this.tbxType.Text = this.tbxType.Text; 
bind(); 
} 

之前一直在页数方面直接用数字写进去,没有写成anPager.PageSize=10;的形式,在老汤的提醒下,做了修改,也解决了一直困扰我的问题。

最新网友评论  共有(1)条评论 发布评论 返回顶部
mnzi 发布于2016-06-05 21:54:15
好的很,支持你!
支持(0)  反对(0)  回复

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务