1. Trang chủ
  2. » Tất cả

knowledge

10 3 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 104 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

APRIL 2007, 11:01:05 Repeater and DataList controls offer a quick and flexible means of displaying data on a ASPX page.. There are several articles on various ASP.NET Resource Sites that

Trang 1

knowledge, skill, experience

about IT, computer, programming

Kiến thức tạo nên sức mạnh, năng lực đạp đổ bằng cấp !

Paging with Repeater control in ASP.NET [C#]

WEDNESDAY, 4 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

<script language="C#" runat="server">

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)

Trang 2

lnkNext.NavigateUrl=Request.CurrentExecutionFilePath

+ "?Page=" + Convert.ToString(CurPage+1);

Repeater1.DataSource=objPds;

Repeater1.DataBind();

}

< /script>

And in the body of the page:

<asp:label ID="lblCurrentPage" runat="server">

</asp:label>

<asp:HyperLink id="lnkPrev" runat="server"><< Prev</asp:HyperLink>

<asp:HyperLink id="lnkNext" runat="server">Next >></asp:HyperLink>

<asp:repeater ID="Repeater1" runat="server">

<itemtemplate>

<%# DataBinder.Eval(Container.DataItem, "Product") %>

</itemtemplate>

</asp:repeater>

• del.icio.us

Working with DropDownList and ListBox Controls in ASP.NET Linux nào hỗ trợ mạng tốt nhất????

Comments

Trang 3

Anonymous # 8 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

của DataAdapter để lấy một số hữu hạn maxRecords bản ghi (từ startRecord)đưa vào DataSet rồi sau đó dùng DataSet làm

nguồn dữ liệu cho Repeater

Cách 2: Phân trang dữ liệu ngay trong StoredProcedure để lấy dữ liệu ra.

Anonymous # 1 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 # 6 February 2010, 04:06

littlegirl writes:

Cũng tạm tạm :d

How to use Quote function:

1 Select some text

2 Click on the Quote link

Write a comment

Paging in Repeater control in ASP.NET

Trong file aspx :

Trang 4

<%@ Page Language="C#" AutoEventWireup="true"

CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<% -%>

<asp:Panel id="pnlResults" runat="server" Visible="true"

BorderColor="#C0C0C0" BorderStyle="solid" BorderWidth="1">

<asp:label id="lblCurrentPage" runat="server"></asp:label>

<br /><br />

<asp:Repeater ID="rptData" runat="server"

OnItemDataBound="rptData_ItemDataBound">

<HeaderTemplate>

</HeaderTemplate>

<ItemTemplate>

[<asp:HyperLink ID="hlnkTitle" runat="server"

NavigateUrl="http://www.dyelvn.com"><b>dyelvn.com</b></asp:HyperLink>] <asp:Label id="lblSummary" runat="server"><

%#Eval("Text") %></asp:Label>

<br />

</ItemTemplate>

<FooterTemplate>

</FooterTemplate>

</asp:Repeater>

<br /><br />

<asp:LinkButton ID="lnkPreviousPage" runat=server CssClass="o6" OnClick="lnkPreviousPage_Click">Previous Page</asp:LinkButton>

<asp:DropDownList ID="PageList" runat="server"

OnSelectedIndexChanged="PageList_SelectedIndexChanged"

AutoPostBack="true">

</asp:DropDownList>

<asp:LinkButton ID="lnkNextPage" runat=server CssClass="o6"

OnClick="lnkNextPage_Click">Next Page</asp:LinkButton></asp:Panel>

<asp:Panel ID="pnlNoResults" runat="server" Visible="false">

<div>

There are currently no items available

</div>

</asp:Panel>

<br />

<br />

<br />

-<br />

Diễn đàn <a

href="http://Tuoitredatviet.net">Tuoitredatviet.net</a>

Trang 5

</div>

</form>

</body>

</html>

Trong code behind:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using 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";

Trang 6

column.ReadOnly = false;

column.Unique = false;

// Add the column to the table

table.Columns.Add(column);

//add value

for (int i = 0; i <= 50; i++)

{

row = table.NewRow();

row["id"] = i;

row["Text"] = "Articles: " + i;

table.Rows.Add(row);

}

return table;

}

public void BindData()

{

pnlResults.Visible = false;

pnlNoResults.Visible = true;

DataTable DTArticles = dtb();

PagedDataSource objPDS = new PagedDataSource();

objPDS.AllowPaging = true;

objPDS.PageSize = 10;

//Set datasource

objPDS.DataSource = DTArticles.DefaultView;

if (objPDS.Count > 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();

// 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;

}

Trang 7

// 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

Trang 8

#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 0

else

return (int)o;

}

set

{

this.ViewState["_CurrentPage"] = value;

}

}

#endregion

}

Đây là bài 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 = 4

Dim curpage As Integer

If Not IsNothing(Request.QueryString("Page")) Then

curpage = Convert.ToInt32(Request.QueryString("Page"))

Else

curpage = 1

End If

pageds.CurrentPageIndex = curpage - 1

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 + _

Trang 9

"?Page=" + CStr(curpage + 1) End If

Repeater1.DataSource = pageds

Repeater1.DataBind()

End Sub

And in the body of the page:

<asp:label ID="lblCurrentPage" runat="server">

</asp:label>

<asp:HyperLink id="lnkPrev" runat="server"><< Prev</asp:HyperLink>

<asp:HyperLink id="lnkNext" runat="server">Next >></asp:HyperLink>

<asp:repeater ID="Repeater1" runat="server">

<itemtemplate>

<%# DataBinder.Eval(Container.DataItem, "Product") %>

</itemtemplate>

</asp:repeater>

Introduction

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 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.

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 = 4

Dim curpage As Integer

If Not IsNothing(Request.QueryString("Page")) Then

Trang 10

curpage = Convert.ToInt32(Request.QueryString("Page")) Else

curpage = 1

End If

pageds.CurrentPageIndex = curpage - 1

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

Ngày đăng: 17/04/2022, 22:03

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w