พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com
ASP, ASP.NET Articles / DataList & Repeater paging
Administrator
Fri 6 Oct 2006 04:17:56
การทำ paging ใน GridView ช่วยให้เราสามารถทำการเขียนการแสดงแบบ paging ได้ง่ายๆ แต่ถ้าเราต้องการใช้ datalist หรือ repeater ล่ะ

ใน ASP.NET นั้นมี class ที่ช่วยเหลือในการทำ paging คือ PagedDataSource เพื่อใช้ในการสร้างการแสดงผลแบบ paging โดยมี properties ที่สำคัญคือ

DataSource (IEnumerable) เป็น source ของข้อมูล
PageSize (int) เป็น จำนวน record ที่แสดงต่อ 1 หน้า
AllowPaging (bool) เป็น flag ที่ใช้บ่งบอกว่าใช้ paging หรือไม่
PageCount (int) เป็นจำนวนหน้าที่ได้จากการแบ่งแล้ว
CurrentPageIndex (int) เป็นหมายเลขของหน้าที่ต้องการแสดง

การใช้งานเพียงทำการระบุ Datasource โดยตัวแปรชนิด IEnumerable โดยปกติแล้วจะทำการนำข้อมูลจาก DataTable ได้โดยตรงเช่น

PagedDataSource pd = new PagedDataSource();
pd.DataSource = dt.DefaultView; // dt คือตัวแปรชนิด DataTable
pd.PageSize = 15; //ทำการระบุจำนวน record ที่ต้องการแสดงต่อ 1 หน้า
pd.AllowPaging = true; //ทำการเซ็ท flag เพื่อบอกว่าต้องการให้ทำ paging ด้วย

if(Request.QueryString["pid"] != null){
    pd.CurrentPageIndex = Convert.ToInt32(Request.QueryString["pid"]) - 1; //กรณีที่มรการระบุหน้าให้ไปหน้าที่ต้องการ โดยตัวแปร pid ในกรณีนี้เริ่มที่ 1 ดังนั้นจึงต้องทำการลบออก 1 ด้วย
}else{
    pd.CurrentPageIndex = 0; //กรณีที่ไม่มีการระบุหน้าให้เริ่มที่หน้าแรก
}

DataList1.DataSource = pd; //ทำการผ่านค่าตัวแปร PagedDataSource เป็น DataSource ให้กับ DataList
DataList1.DataBind();

เป็นการทำ paging สำหรับ DataList และ Repeater อย่างง่ายๆครับ
อาม
อาม
Mon 26 Nov 2007 17:32:12

ผมจะแก้ไขข้อมูลใขฐานข้อมูลทำไงครับ

ead>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataSourceID="SqlDataSource1">
            <Fields>
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="TFIRST" HeaderText="TFIRST" SortExpression="TFIRST" />
                <asp:BoundField DataField="TLAST" HeaderText="TLAST" SortExpression="TLAST" />
                <asp:CommandField ShowEditButton="True" />
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
            ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>"
            SelectCommand="SELECT ID, TFIRST, TLAST FROM REGIS.STUDENT WHERE (ID = '284950404030')"
            UpdateCommand="UPDATE REGIS.STUDENT SET TFIRST = TFIRST, TLAST = TLAST WHERE (ID = '284950404030')">
            <UpdateParameters >
            <asp:Parameter Name ="TFIRST" />
            <asp:Parameter Name ="TLAST" />
            </UpdateParameters>
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>

อาม
อาม
Mon 26 Nov 2007 17:33:27
ไม่รู้ทำไงครับ
webmaster
webmaster
Tue 4 Dec 2007 09:48:40
ต้องทำการ handle event แล้วทำการเขียนคำสั่งในการ update database อีกทีครับ
Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login