熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

三層+存儲過程實現分頁示例代碼

2022-06-13   來源: .NET編程 

  前台設計

復制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="pagingaspxcs" Inherits="五二一練習paging" %>
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html xmlns="
<head runat="server">
<title></title>
<script src="js/Jqueryjs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$(#txtPagination)focus(function () {
$(this)val("");
})
})
</script>
</head>
<body>
<form id="form" runat="server">
<div>
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False"
Height="px" Width="px">
<Columns>
<asp:BoundField DataField="Id" HeaderText="編號" />
<asp:BoundField DataField="NewsTitle" HeaderText="標題" />
<asp:BoundField DataField="NewsContent" HeaderText="內容" />
<asp:BoundField DataField="CreateTime"
DataFormatString="{:yyyyMMdd hh:mm:ss}" HeaderText="發布時間" />
</Columns>
</asp:GridView>
<asp:LinkButton ID="btnFirst" runat="server" onclick="btnFirst_Click">第一頁</asp:LinkButton>
<asp:LinkButton
ID="btnPre" runat="server" onclick="btnPre_Click">上一頁</asp:LinkButton>
<asp:LinkButton ID="btnNext"
runat="server" onclick="btnNext_Click">下一頁</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" onclick="btnLast_Click">最後一頁</asp:LinkButton><asp:TextBox
ID="txtPagination" runat="server"></asp:TextBox>
<asp:LinkButton ID="btnSkip" runat="server" onclick="btnSkip_Click">GO</asp:LinkButton>
</div>
</form>
</body>
</html>

  
首先在數據庫創建存儲過程

復制代碼 代碼如下:
create proc usp_role_GetDateByPageIndex
@pageSize int
@pageIndex int
as
begin
select * from
(
select *ROW_NUMBER() over(order by role_id) as rownumber from role) as tbl
where tblrownumber between (@pageSize*(@pageIndex)+) and @pageIndex*@pageSize
end
exec usp_role_GetDateByPageIndex

  
在項目中添加BLLDALDataAccessMODEL層
在DAL中寫一個方法

復制代碼 代碼如下:
//自己寫的方法分頁獲取數據列表
public DataTable GetListDataTable(int PageSize int PageIndex)
{
SqlParameter[] parameters = {
new SqlParameter("@PageSize" SqlDbTypeInt)
new SqlParameter("@PageIndex" SqlDbTypeInt)
};
parameters[]Value = PageSize;
parameters[]Value = PageIndex;
return DbHelperSQLRunProcedureDataTable("usp_role_GetDateByPageIndex" parameters);
}
在BLL中調用GetListDataTable
public DataTable GetListDataTable(int pagesize int pageindex)
{
return dalGetListDataTable(pagesize pageindex);
}
在DbHelper中添加RunProcedureDataTable方法
public static DataTable RunProcedureDataTable(string stroreProcName IDataParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
connectionOpen();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDASelectCommand = BuildQueryCommand(connection stroreProcName parameters);
sqlDAFill(dt);
connectionClose();
return dt;
}
}

  
然後在後台調用即可

復制代碼 代碼如下:

  
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemWeb;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemData;
namespace 練習
{
public partial class paging : SystemWebUIPage
{
int pagesize = ;
int pageindex = ;
protected void Page_Load(object sender EventArgs e)
{
if (!IsPostBack)
{
ViewState["pageindex"] = ;
LadaData();
GetListPageindex();
}
}
private void GetListPageindex()
{
BLLT_News bnews = new BLLT_News();
int totalcount = bnewsGetRecordCount("");
if (totalcount % pagesize == )
{
ViewState["lastpageindex"] = totalcount / pagesize;
}
else
{
ViewState["lastpageindex"] = totalcount / pagesize + ;
}
}
private void LadaData()
{
BLLT_News bnews = new BLLT_News();
DataTable dt = bnewsGetListDataTable(pagesize ConvertToInt(ViewState["pageindex"]));
thisGridViewDataSource = dt;
thisGridViewDataBind();
}
//第一頁
protected void btnFirst_Click(object sender EventArgs e)
{
ViewState["pageindex"] = ;
LadaData();
}
//上一頁
protected void btnPre_Click(object sender EventArgs e)
{
int pageindex = ConvertToInt(ViewState["pageindex"]);
if (pagesize>)
{
pageindex;
ViewState["pageindex"] = pageindex;
LadaData();
}
}
//下一頁
protected void btnNext_Click(object sender EventArgs e)
{
int pageindex = ConvertToInt(ViewState["pageindex"]);
if (pageindex<ConvertToInt(ViewState["lastpageindex"]))
{
pageindex++;
ViewState["pageindex"] = pageindex;
LadaData();
}
}
//最後一頁
protected void btnLast_Click(object sender EventArgs e)
{
ViewState["pageindex"] = ViewState["lastpageindex"];
LadaData();
}
//跳轉頁面
protected void btnSkip_Click(object sender EventArgs e)
{
int result;
if (intTryParse(txtPaginationText out result) == true)
{
ViewState["pageindex"] = txtPaginationTextTrim();
LadaData();
}
else
{
txtPaginationText = "請輸入合法的數字";
}
}
}
}


From:http://tw.wingwit.com/Article/program/net/201311/14272.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.