欢迎来到福编程网,本站提供各种互联网专业知识!

AJAX和三层架构实现分页功能具体思路及代码

发布时间:2013-05-21 作者: 来源:转载
本文涉及到AJAX和三层架构方面的知识,在学习分页的同时也巩固了一下它们的相关知识,适合初学者的你
复制代码 代码如下:
-----------------------------HTMLPage1.htm---------------------------------










第一页上一页下一页最后一页id="txtPageindex" type="text" />Go



-------------------------WebService1 --------------------------------
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public List GetListAjax(int pagesize, int pageindex)
{
BLL.T_News1 bnews = new BLL.T_News1();
DataTable dt = bnews.GetListDataTable(pagesize, pageindex);
List list = new List();
int Id;
string newstitle = "";
string newscontent = "";
DateTime createtime;
for (int i = 0; i < dt.Rows.Count; i++)
{
Id = Convert.ToInt32(dt.Rows[i]["Id"]);
newstitle = dt.Rows[i]["NewsTitle"].ToString();
newscontent = dt.Rows[i]["NewsContent"].ToString();
createtime = Convert.ToDateTime(dt.Rows[i]["CreateTime"]);
Model.T_News1 news = new Model.T_News1()
{
Id = Id,
NewsTitle = newstitle,
NewsContent = newscontent,
CreateTime = createtime
};
list.Add(news);
}
return list;
}
[WebMethod]
public int GetLastPageindex(int pagesize)
{
BLL.T_News1 bnews = new BLL.T_News1();
int totalcount = bnews.GetRecordCount("");
if (totalcount % pagesize == 0)
{
return totalcount / pagesize;
}
else
{
return totalcount / pagesize + 1;
}
}
------------------------------DAL层:--------------------------
///
/// 分页获取数据列表
///

public DataTable GetListDataTable(int PageSize, int PageIndex)
{
SqlParameter[] parameters = {
new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int)
};
parameters[0].Value = PageSize;
parameters[1].Value = PageIndex;
return DbHelperSQL.RunProcedureDataTable("pro_fenye", parameters);
}
--------------------BLL层:--------------------------
public DataTable GetListDataTable(int pagesize, int pageindex)
{
return dal.GetListDataTable(pagesize, pageindex);
}
------------------DbHelperSQL:-----------------------
public static DataTable RunProcedureDataTable(string storedProcName, IDataParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dt);
connection.Close();
return dt;
}
}

相关推荐