1:在数据库 建立分页的存储过程
2:在 动软生成器生成的DAO代码中加上获取分页数据的方法,和计算记录数的方法
3:在页面中 假如 asp控件并设置显示的样式
4:在后台的page_load 事件中先设置 anp 控件的总记录数,再绑定 rep控件
5:双击anp控件,进行anp控件的分页事件。
存储过程如下(只适合sql2005以及以上)
-- ============================================= -- Author: 牛腩 -- Create date: 2009-07-22 12:41 -- Description: 分页,用到了ROW_NUMBER() -- ============================================= create PROCEDURE [dbo].[proc_FenYe] @tblName varchar(255), -- 表名 @strGetFields varchar(1000) = '*', -- 需要返回的列,默认* @strOrder varchar(255)='', -- 排序的字段名,必填 @strOrderType varchar(10)='ASC', -- 排序的方式,默认ASC @PageSize int = 10, -- 页尺寸,默认 @PageIndex int = 1, -- 页码,默认 @strWhere varchar(1500) = '' -- 查询条件(注意: 不要加where) AS declare @strSQL varchar(5000) if @strWhere !='' set @strWhere=' where '+@strWhere set @strSQL= 'SELECT * FROM ('+ 'SELECT ROW_NUMBER() OVER (ORDER BY '+@strOrder+''+@strOrderType+') AS pos,'+@strGetFields+''+ 'FROM '+@tblName+''+@strWhere+ ') AS sp WHERE pos BETWEEN '+str((@PageIndex-1)*@PageSize+1)+' AND '+str(@PageIndex*@PageSize) exec (@strSQL)
接下来,我们去DAL层查看 LinkDAO.cs 修改 获取分页记录的代码,添加获取 总记录数的方法
/// <summary>分页获取数据列表
///
/// </summary>
/// <param name="strGetFields">选择的字段</param>
/// <param name="strOrder">排序字段</param>
/// <param name="strOrderType">排序类型 desc或者asc</param>
/// /// <param name="PageSize">页面大小</param>
/// <param name="PageIndex">页索引</param>
/// <param name="strWhere">条件</param>
/// <returns></returns>
public DataSet GetList(string strGetFields, string strOrder, string strOrderType, int PageSize, int PageIndex, string strWhere)
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetStoredProcCommand("proc_FenY");
db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "shop_link");
db.AddInParameter(dbCommand, "strGetFields", DbType.AnsiString, strGetFields);
db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
db.AddInParameter(dbCommand, "strOrder ", DbType.String, strOrder);
db.AddInParameter(dbCommand, "strOrderType", DbType.String, strOrderType);
db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
return db.ExecuteDataSet(dbCommand);
}
/********************************************************* * 开发人员:Joey QQ:1727050508 博客: http://1727050508.cnblogs.com * 创建时间:2012-3-2 16:17:39 * 描述说明:link_list.aspx 用来显示 友情链接列表 * * 更改历史: * * *******************************************************/ using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Niunan.Shop.Model; using Niunan.Shop.DAL; namespace Niunan.Shop.Web.admin { public partial class link_list : System.Web.UI.Page { LinkDAO link = new LinkDAO(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindRep(); } } private void BindRep() { int pagesize = anp.PageSize; int pageindex = anp.CurrentPageIndex; anp.RecordCount = link.ClacCount(GetCond()); repList.DataSource = link.GetList("*", "id", "desc", pagesize, pageindex, GetCond()); repList.DataBind(); } private string GetCond() { string cond = ""; string key = txtKey.Text.Trim(); if (key.Length!=0) { cond = "linkname like '%"+key+"%' or url like '%"+key+"%' "; } return cond; } protected void lbtnDel_Click(object sender, EventArgs e) { string id = (sender as LinkButton).CommandArgument; //这里的sender是上面挂号里面的 object对象,转换为 linkbutton对象 link.Delete(int.Parse(id)); BindRep(); } protected void anp_PageChanged(object sender, EventArgs e) { BindRep(); } protected void btnSearch_Click(object sender, EventArgs e) { BindRep(); } } }
我们在用 linkbutton 做删除的时候
前台代码
<asp:LinkButton ID="lbtnDel" runat="server" OnClientClick="return confirm('是否删除该链接?')"
OnClick="lbtnDel_Click" CommandArgument='<%#Eval("id") %>'>删除</asp:LinkButton>
后台代码
protected void lbtnDel_Click(object sender, EventArgs e) { string id = (sender as LinkButton).CommandArgument; //这里的sender是上面挂号里面的 object对象,转换为 linkbutton对象 link.Delete(int.Parse(id)); BindRep(); }
如何判断传入的ID的值是否为数字
string id = Request.QueryString["id"]; int x; //如果字符串不等于null 不等于空 并且是数字 if (!string.IsNullOrEmpty(id) && int.TryParse(id, out x)) { Niunan.Shop.Model.Link linkModel = new Niunan.Shop.DAL.LinkDAO().GetModel(int.Parse(id)); if (linkModel != null) { txtLinkName.Text = linkModel.linkname; txtLinkUrl.Text = linkModel.url; litH1.Text = "修改友情链接"; btnAdd.Text = "修改"; } } }