日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Asp.net中DataGrid控件的自定义分页

發布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Asp.net中DataGrid控件的自定义分页 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

???????? 使用實現起來雖然比較方便,但是效率不高,每次都需要讀取所有頁(整個記錄集),而加載的只是其中一頁,造成了資源的浪費,記錄多又會使效率變得很低。下面通過DataGrid的自定義分頁功能來減少資源使用和提高效率。

???????? 實現的關鍵是設置AllowCustomPaging屬性位True,并把VirtualItemCount屬性設置位總的記錄數,給分頁提供依據,前臺的主要代碼如下:

<form id="Form1" method="post" runat="server">

??????????????????????????? <TABLE id="Table1" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"

???????????????????????????????????? border="1">

???????????????????????????????????? <TR>

?????????????????????????????????????????????? <TD>

??????????????????????????????????????????????????????? <asp:datagrid id="DataGrid1" runat="server" Width="100%" AllowPaging="True" AllowCustomPaging="True">

???????????????????????????????????????????????????????????????? <PagerStyle Font-Size="9pt" Mode="NumericPages"></PagerStyle>

??????????????????????????????????????????????????????? </asp:datagrid></TD>

???????????????????????????????????? </TR>

??????????????????????????? </TABLE>

?????????????????? </form>

這里使用的數據源還是假設為NorthwindCustomers表。

下面是訪問單頁的存儲過程,實現方式很多,不過這個是最普通的,

CREATE PROCEDURE [GetCustomersDataPage]

???????? @PageIndex INT,

???????? @PageSize? INT,

???????? @RecordCount INT OUT,

???????? @PageCount INT OUT

AS

SELECT @RecordCount = COUNT(*)? FROM?? Customers

SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)

DECLARE @SQLSTR NVARCHAR(1000)

IF @PageIndex = 0 OR @PageCount <= 1

???????? SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+

'? CustomerID, CompanyName,Address,Phone? FROM?? Customers ORDER BY CustomerID DESC'

ELSE IF???? @PageIndex = @PageCount - 1????????????

???????? SET @SQLSTR =N' SELECT * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+

'? CustomerID, CompanyName,Address,Phone? FROM?? Customers ORDER BY CustomerID ASC ) TempTable? ORDER BY CustomerID DESC'

ELSE?????????

??????? SET @SQLSTR =N' SELECT TOP? '+STR( @PageSize )+' * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+

'? CustomerID, CompanyName,Address,Phone? FROM?? Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC'

EXEC (@SQLSTR)

GO

獲取記錄數和頁數都采用存儲過程的輸出參數。

獲取數據源,這里返回一個DataSet

先定義了連個數據成員,

private int pageCount;//頁數

private int recordCount;//記錄數

//獲取單頁數據

private static DataSet GetCustomersData(int pageIndex,int pageSize,ref int recordCount,ref int pageCount)

{

???? string connString = ConfigurationSettings.AppSettings["ConnString"];

???? SqlConnection conn = new SqlConnection(connString);

???? SqlCommand comm = new SqlCommand("GetCustomersDataPage",conn);

???? comm.Parameters.Add(new SqlParameter("@PageIndex",SqlDbType.Int));

???? comm.Parameters[0].Value = pageIndex;

???? comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));

???? comm.Parameters[1].Value = pageSize;

???? comm.Parameters.Add(new SqlParameter("@RecordCount",SqlDbType.Int));

???? comm.Parameters[2].Direction = ParameterDirection.Output;

???? comm.Parameters.Add(new SqlParameter("@PageCount",SqlDbType.Int));

???? comm.Parameters[3].Direction = ParameterDirection.Output;

???? comm.CommandType = CommandType.StoredProcedure;

???? SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

???? DataSet ds = new DataSet();

???? dataAdapter.Fill(ds);

???? recordCount = (int)comm.Parameters[2].Value;

???? pageCount = (int)comm.Parameters[3].Value;

???? return ds;

}

//綁定數據到DataGrid,同時刷新數據總記錄數

private void DataGridDataBind()

{

???? DataSet ds = GetCustomersData(PageIndex,PageSize,ref recordCount,ref pageCount);

???? this.DataGrid1.VirtualItemCount = RecordCount;

???? this.DataGrid1.DataSource = ds;

???? this.DataGrid1.DataBind();

}

下面是分頁的幾個變量屬性

public int PageCount

{

???? get{return this.DataGrid1.PageCount;}

}

public int PageSize

{

???? get{return this.DataGrid1.PageSize;}

}

public int PageIndex

{

???? get{return this.DataGrid1.CurrentPageIndex;}

???? set{this.DataGrid1.CurrentPageIndex = value;}

}

public int RecordCount

{

???? get{return recordCount;}

}

注冊DataGrid分頁事件

//分頁事件處理

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

{

???? DataGrid dg = (DataGrid)source;

???? dg.CurrentPageIndex = e.NewPageIndex;

???? DataGridDataBind();

}

最好判斷當前頁面是否是第一次加載,防止重復加載兩次數據,

private void Page_Load(object sender, System.EventArgs e)

{

???? if(!Page.IsPostBack)

???? {

???????? DataGridDataBind();

???? }

}

顯示界面如下:


這個例子中沒有顯示分頁的一些參數,我們可以進一步對其進行改進。

總結

以上是生活随笔為你收集整理的Asp.net中DataGrid控件的自定义分页的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。