knowledge, skill, experience about IT, computer, programming Kiến thức tạo nên sức mạnh, lực đạp đổ cấp ! • Blog • Photos • Favorites • Unite • Links • Friends • About Paging with Repeater control in ASP.NET [C#] WEDNESDAY, APRIL 2007, 11:01:05 Repeater and DataList controls offer a quick and flexible means of displaying data on a ASPX page But they offer no paging functionality built in The DataGrid control has in-built paging but it's structure is more rigid There are several articles on various ASP.NET Resource Sites that offer solutions, but I'm going to show you how to use the PagedDataSource class Repeater and DataList controls offer a quick and flexible means of displaying data on a ASPX page But they offer no paging functionality built in The DataGrid control has in-built paging but it's structure is more rigid There are several articles on various ASP.NET Resource Sites that offer solutions, but I'm going to show you how to use the PagedDataSource class The PagedDataSource class encapsulates the properties of the DataGrid control that allow it to perform paging But we can use the class with Repeater and DataList controls to perform paging in much the same way as a DataGrid First, we need to add some code to the ASPX page, you can download a demo by clicking the link at bottom public void Page_Load(Object src,EventArgs e) { DataSet ds; //Instanciate Dataset PagedDataSource objPds = new PagedDataSource(); objPds.DataSource = ds.Tables[0].DefaultView; objPds.AllowPaging = true; objPds.PageSize = 5; int CurPage; if (Request.QueryString["Page"] != null) CurPage=Convert.ToInt32(Request.QueryString["Page"]); else CurPage=1; objPds.CurrentPageIndex = CurPage-1; lblCurrentPage.Text = "Page: " + CurPage.ToString(); if (!objPds.IsFirstPage) lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1); if (!objPds.IsLastPage) lnkNext.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage+1); Repeater1.DataSource=objPds; Repeater1.DataBind(); } < /script> And in the body of the page: > • • • • • • digg Facebook Twitter del.icio.us StumbleUpon reddit Working with DropDownList and ListBox Controls in ASP.NET Linux hỗ trợ mạng tốt nhất???? Comments Anonymous # April 2007, 09:59 Anonymous writes: Suu tam them: Cách 1: Sử dụng phương thức Fill ( dataSet As DataSet, startRecord As Integer, maxRecords As Integer, srcTable As String ) As Integer DataAdapter để lấy số hữu hạn maxRecords ghi (từ startRecord)đưa vào DataSet sau dùng DataSet làm nguồn liệu cho Repeater Cách 2: Phân trang liệu StoredProcedure để lấy liệu Anonymous # April 2009, 12:57 Anonymous writes: rat hay, cam on nhe Anonymous # 26 April 2009, 15:50 Anonymous writes: ddd Anonymous # 30 November 2009, 08:10 khuccui2002 writes: Thanks Anonymous # February 2010, 04:06 littlegirl writes: Cũng tạm tạm :d How to use Quote function: Select some text Click on the Quote link Write a comment Paging in Repeater control in ASP.NET Trong file aspx : Untitled Page [dyelvn.com] < %#Eval("Text") %> Previous Page Next Page There are currently no items available - Diễn đàn Tuoitredatviet.net Trong code behind: using using using using using using using using using System; System.Data; System.Configuration; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } #region Data Loading public DataTable dtb() { // Create a new DataTable System.Data.DataTable table = new DataTable("Table"); // Declare variables for DataColumn and DataRow objects DataColumn column; DataRow row; // Create new DataColumn, set DataType, // ColumnName and add to DataTable column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "id"; column.ReadOnly = true; column.Unique = true; // Add the Column to the DataColumnCollection table.Columns.Add(column); // Create second column column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "Text"; column.AutoIncrement = false; column.Caption = "Text"; column.ReadOnly = false; column.Unique = false; // Add the column to the table table.Columns.Add(column); //add value for (int i = 0; i 0) { pnlResults.Visible = true; pnlNoResults.Visible = false; // Set the PagedDataSource's current page objPDS.CurrentPageIndex = CurrentPage; //- 1; lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objPDS.PageCount.ToString(); // -more -// Clear out all of the items in the DropDownList PageList.Items.Clear(); // Add a ListItem for each page for (int i = 0; i < objPDS.PageCount; i++) { // Add the new ListItem ListItem pageListItem = new ListItem(string.Concat("Page ", i + 1), i.ToString()); PageList.Items.Add(pageListItem); // select the current item, if needed if (i == CurrentPage) pageListItem.Selected = true; } // - // Disable Prev or Next buttons if necessary lnkPreviousPage.Enabled = !objPDS.IsFirstPage; lnkNextPage.Enabled = !objPDS.IsLastPage; rptData.DataSource = objPDS; rptData.DataBind(); } } #endregion #region Repeater Events protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { //Unimportant Code Ommitted } } #endregion #region Control Evnets protected void PageList_SelectedIndexChanged(object sender, EventArgs e) { // Jump to the specified page //rptData.PageIndex = Convert.ToInt32(PageList.SelectedValue); CurrentPage = Convert.ToInt32(PageList.SelectedValue) ; } //Reload control BindData(); protected void lnkNextPage_Click(object sender, EventArgs e) { //Set viewstate variable to the next page CurrentPage += 1; } //Reload control BindData(); protected void lnkPreviousPage_Click(object sender, EventArgs e) { //Set viewstate variable to the previous page CurrentPage -= 1; //Reload control BindData(); } #endregion #region Properties public int CurrentPage { get { //Look for current page in ViewState object o = this.ViewState["_CurrentPage"]; if (o == null) return 0; // default page index of else return (int)o; } set { this.ViewState["_CurrentPage"] = value; } } #endregion } Đây viết khác Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Dim myConnection As New _ SqlConnection(ConfigurationSettings.AppSettings("connectionString")) Dim myDA As New SqlClient.SqlDataAdapter("Select * from dtsDefect", _ myConnection) myDA.Fill(ds, "t1") pageds.DataSource = ds.Tables("t1").DefaultView pageds.AllowPaging = True pageds.PageSize = Dim curpage As Integer If Not IsNothing(Request.QueryString("Page")) Then curpage = Convert.ToInt32(Request.QueryString("Page")) Else curpage = End If pageds.CurrentPageIndex = curpage - lblCurrpage.Text = "Page: " + curpage.ToString() If Not pageds.IsFirstPage Then lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + _ "?Page=" + CStr(curpage - 1) End If If Not pageds.IsLastPage Then lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + _ End If "?Page=" + CStr(curpage + 1) Repeater1.DataSource = pageds Repeater1.DataBind() End Sub And in the body of the page: > Introduction and DataList controls offer a quick and flexible means of displaying data on a ASPX page But they offer no paging functionality built in The DataGrid control has inbuilt paging but its structure is more rigid There are several articles on various ASP.NET Resource Sites that offer solutions, but I'm going to show you how to use the PagedDataSource class Repeater The PagedDataSource class encapsulates the properties of the DataGrid control that allow it to perform paging But we can use the class with Repeater and DataList controls to perform paging in much the same way as a DataGrid Collapse Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Dim myConnection As New _ SqlConnection(ConfigurationSettings.AppSettings("connectionString")) Dim myDA As New SqlClient.SqlDataAdapter("Select * from dtsDefect", _ myConnection) myDA.Fill(ds, "t1") pageds.DataSource = ds.Tables("t1").DefaultView pageds.AllowPaging = True pageds.PageSize = Dim curpage As Integer If Not IsNothing(Request.QueryString("Page")) Then curpage = Convert.ToInt32(Request.QueryString("Page")) Else curpage = End If pageds.CurrentPageIndex = curpage - lblCurrpage.Text = "Page: " + curpage.ToString() If Not pageds.IsFirstPage Then lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + _ "?Page=" + CStr(curpage - 1) End If If Not pageds.IsLastPage Then lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + _ "?Page=" + CStr(curpage + 1) End If Repeater1.DataSource = pageds Repeater1.DataBind() End Sub