Paging DataList, GridView, Repeater (using CollectionPager)
Wednesday, 17. September 2008, 09:57:30
Bước 2: Tại trang .aspx Thêm đoạn sau vào đầu trang Code:
<%@ Register TagPrefix="cc1" Namespace="SiteUtils"
Assembly="CollectionPager" %>
- Kéo 1 DataList vào trong trang đó đặt ( ví dụ có tên DataList1)
<asp:DataList ID="DataList1" runat="server">
</asp:DataList>
<CC1:COLLECTIONPAGER id="CollectionPager1" runat="server">
</CC1:COLLECTIONPAGER>
Tại trang aspx.cs
Lấy DataSet ds ( lấy dự liệu trong Database)
Code:
CollectionPager1.PageSize = 5; // số items hiển thị trên một trang
CollectionPager1.DataSource = ds.Tables[0].DefaultView;
CollectionPager1.BindToControl = DataList1;
DataList1.DataSource = CollectionPager1.DataSourcePaged;
Chúc các bạn thành công!
siêu tầm














heocon's # 15. October 2008, 17:02
Bạn có thể sử dụng PagedDataSource, tham khảo đoạn code sau nhé
void BindList()
{
PagedDataSource objPage = new PagedDataSource();
try
{
SqlConnection connect = new SqlConnection(ConfigurationManager.AppSettings["Conn"]);
string select = "select * from vw_sp";
SqlDataAdapter adapter = new SqlDataAdapter(select, connect);
DataSet dts = new DataSet();
adapter.Fill(dts, "dts");
objPage.AllowPaging = true;
objPage.DataSource = dts.Tables[0].DefaultView;
objPage.PageSize = 12;
objPage.CurrentPageIndex = CurrentPage;
lbtnNext.Enabled = !objPage.IsLastPage;
lbtnPrev.Enabled = !objPage.IsFirstPage;
ListSP.DataSource = objPage;
ListSP.DataBind();
TotalRecord = dts.Tables[0].Rows.Count;
}
catch (Exception)
{
}
finally
{
objPage = null;
}
}
protected void lbtnPrev_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
BindList();
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
BindList();
}
Anonymous # 28. November 2008, 11:05
a oi cho e hoi chut. o trag home cua e hien thi 2 fan la tin tuc va san pham nhug ap dug cua a khi chuyen trag o fan san pham hoac tin tuc thi fan con lai cug chuyen theo. lam cach nao de chug tach biet nhau dc ha a? thaks
heocon's # 29. November 2008, 17:11
Ví dụ :
- Sản phẩm bạn để là : pagesp
- Tin tức bạn để là : pagett
Chỉ cần như vậy là ok rùi.
Anonymous # 1. December 2008, 01:50
rất cám ơn a! e đã làm đc rùi. e muốn hỏi a thêm đc k? nếu mà muốn hiển thị sản phẩm thành 2 cột hoặc 3 cột thì làm thế nào? e mới học về cái này nên cũg chưa bít. júp e nhé! thaks a!
heocon's # 2. December 2008, 12:51
Để cảm ơn tớ vui lòng click vào đây nhé
Anonymous # 3. December 2008, 01:32
nhug ma e dug repeater! lam the nao de hien thi thah nhiu cot bi h ha a???
heocon's # 3. December 2008, 14:14
Anonymous # 26. December 2008, 05:49
cai pagedatasource hay lam ah! nhung CurrenPage po de o dau sao k thay khai bao gi het vay?
heocon's # 26. December 2008, 08:46
Anonymous # 3. January 2009, 05:30
anh ơi, giúp dùm em, nó báo lỗi rùi, hic
Compiler Error Message: CS0103: The name 'ds' does not exist in the current context
Source Error:
Line 17: {
Line 18: CollectionPager1.PageSize = 5; // số items hiển thị trên một trang
Line 19: CollectionPager1.DataSource =ds.Table[0].DefaultView;
Line 20: CollectionPager1.BindToControl = dtlSanPham;
Line 21: dtlSanPham.DataSource = CTH48CLs.SanPham.DanhSach2(Request.QueryString["MaNSP"].ToString());
Source File: d:\Wesitebanhang\SanPhamTheoNhom.aspx.cs Line: 19
heocon's # 15. January 2009, 10:35
Line 18: CollectionPager1.PageSize = 5; // số items hiển thị trên một trang
Line 19: CollectionPager1.DataSource =ds.Table[0].DefaultView;
Line 20: CollectionPager1.BindToControl = dtlSanPham;
Line 21: dtlSanPham.DataSource = CTH48CLs.SanPham.DanhSach2(Request.QueryString["MaNSP"].ToString());
Dòng 19 & 21 phải sửa thành:
Line 19: CollectionPager1.DataSource = "Datatable ở chỗ này nhé bạn";
Line 21: dtlSanPham.DataSource = CollectionPager1.DataSourcePage;
Hy vọng là ko muộn đối với bạn.
Chúc thành công!
Anonymous # 22. September 2009, 09:45
bài viết rất hay và có ích ! cảm ơn pro nhiều !
Anonymous # 15. October 2009, 08:54
mình cũng đã down ở trang nc ngoài cái tool này. bây giờ áp dụng thì nó báo lỗi này nè bạn?
bạn xem hộ mình cái!
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0029: Cannot implicitly convert type 'System.Web.UI.WebControls.Repeater' to 'string'
Source Error:
Line 21: DataSet dts = tintuc1.laytintuc_1();
Line 22: CollectionPager1.DataSource =dts.Tables[0].DefaultView;
Line 23: CollectionPager1.BindToControl = Repeater1;
Line 24:
Line 25: CollectionPager1.PageSize=10;
Anonymous # 25. November 2009, 18:30
CollectionPager1.PageSize = 5;
CollectionPager1.DataSource = DataTable;
CollectionPager1.BindToControl = DataList1;
DataList1.DataSource = CollectionPager1.DataSourcePaged;
potay bạn ơi! Mình đã thay đổi giống bạn nói,nhưng nó vẫn báo lỗi.Mong bạn chỉ chi tiết giúp mình,thks!
Anonymous # 3. December 2009, 08:28
Anonymous writes:
CollectionPager1.PageSize = 5;
CollectionPager1.DataSource = DataTable;
CollectionPager1.BindToControl = DataList1;
DataList1.DataSource = CollectionPager1.DataSourcePaged;
potay bạn ơi! Mình đã thay đổi giống bạn nói,nhưng nó vẫn báo lỗi.Mong bạn chỉ chi tiết giúp mình,thks!
theo minh cho ban sai do la cai "datatable"
co le ban chua khai bao datatable table=new datatable;
sau do:CollectionPager1.DataSource = table;
hic! dang hoc cai nay ko bit minh co lam dung ko!
Anonymous # 3. December 2009, 08:56
anh oi! cho em hoi them ve PagedDataSource va CollectionPager!
no khac nhau the nao? va cach su dung the nao?
neu co the anh cho em biet nick yahoo duoc ko ah! em dang la sinh vien co nhiu cai ko bit!
Anonymous # 8. December 2009, 18:46
DataSet ds = new DataSet();
CollectionPager1.PageSize = 5; // số items hiển thị trên một trang
CollectionPager1.DataSource = ds.Tables[0].DefaultView; >>>>>>
CollectionPager1.BindToControl = DataList1;
DataList1.DataSource = CollectionPager1.DataSourcePaged;
}
bác pro ơi help me ở chổ đó với T_T
Anonymous # 8. December 2009, 18:48
sau khi code nhu vay no bao loi la [[Cannot find table 0.]]
Anonymous # 16. December 2009, 10:09
hic anh ay ko tra loi!
hixhix