ใน 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
Tue 4 Dec 2007 09:48:40

















