今天看书,看到另外一种实现分页的代码(天轰穿老师的视频是用PagedDataSource类来实现分页的),但我看了很久还是看不出它是如何实现分页的,是我悟性低,还是书本的代码错了,贴出来共大家讨论.代码如下:
GetNews存储过程
ALTER PROCEDURE GetNews
@SortID int
AS
if @SortID=0
select
N.ItemID,N.Title,NS.SortName,N.Content,N.Dot,N.CreatedDate,N.ExpireDate
from
News AS N,NewsSort AS NS
where
N.SortID=NS.SortID
and ExpireDate>GetDate()
order by
N.ItemID desc
else
select
N.ItemID,N.Title,NS.SortName,N.Content,N.Dot,N.CreatedDate,N.ExpireDate
from
News AS N,NewsSort AS NS
where
N.SortID=NS.SortID
and ExpireDate>GetDate()
and N.SortID=@SortID
order by
N.ItemID desc
@SortID int
AS
if @SortID=0
select
N.ItemID,N.Title,NS.SortName,N.Content,N.Dot,N.CreatedDate,N.ExpireDate
from
News AS N,NewsSort AS NS
where
N.SortID=NS.SortID
and ExpireDate>GetDate()
order by
N.ItemID desc
else
select
N.ItemID,N.Title,NS.SortName,N.Content,N.Dot,N.CreatedDate,N.ExpireDate
from
News AS N,NewsSort AS NS
where
N.SortID=NS.SortID
and ExpireDate>GetDate()
and N.SortID=@SortID
order by
N.ItemID desc
App_Code/NewsDB.cs
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11public class NewsDB
12{
13 public NewsDB()
14 {
15 //
16 // TODO: Add constructor logic here
17 //
18 }
19 public DataSet CurrentPageNews(int currentPage, int pageSize, int SortID, out int totalPage, out int records)
20 {
21 SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
22 SqlDataAdapter myDataAdapter = new SqlDataAdapter("GetNews", myConnection);
23 myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
24 SqlParameter parameterSortID = new SqlParameter("@SortID", SqlDbType.Int, 4);
25 parameterSortID.Value = SortID;
26 myDataAdapter.SelectCommand.Parameters.Add(parameterSortID);
27
28 DataSet allNews = new DataSet();
29 myDataAdapter.Fill(allNews);
30
31 DataSet dsCurrentNews = new DataSet();
32
33 records = allNews.Tables[0].Rows.Count;
34
35 int startIndex = (currentPage - 1) * pageSize;
36 int endIndex = startIndex + pageSize;
37
38 totalPage = records / pageSize;
39
40 if (totalPage * pageSize < records)
41 {
42 totalPage++;
43 }
44
45 if (endIndex > records)
46 {
47 endIndex = records;
48 }
49
50 dsCurrentNews = allNews.Clone();
51
52 for (int i = startIndex; i < endIndex; i++)
53 {
54 DataRow row = dsCurrentNews.Tables[0].NewRow();
55 row.ItemArray = allNews.Tables[0].Rows[i].ItemArray;
56 dsCurrentNews.Tables[0].Rows.Add(row);
57 }
58 return dsCurrentNews;
59 }
60}
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11public class NewsDB
12{
13 public NewsDB()
14 {
15 //
16 // TODO: Add constructor logic here
17 //
18 }
19 public DataSet CurrentPageNews(int currentPage, int pageSize, int SortID, out int totalPage, out int records)
20 {
21 SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
22 SqlDataAdapter myDataAdapter = new SqlDataAdapter("GetNews", myConnection);
23 myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
24 SqlParameter parameterSortID = new SqlParameter("@SortID", SqlDbType.Int, 4);
25 parameterSortID.Value = SortID;
26 myDataAdapter.SelectCommand.Parameters.Add(parameterSortID);
27
28 DataSet allNews = new DataSet();
29 myDataAdapter.Fill(allNews);
30
31 DataSet dsCurrentNews = new DataSet();
32
33 records = allNews.Tables[0].Rows.Count;
34
35 int startIndex = (currentPage - 1) * pageSize;
36 int endIndex = startIndex + pageSize;
37
38 totalPage = records / pageSize;
39
40 if (totalPage * pageSize < records)
41 {
42 totalPage++;
43 }
44
45 if (endIndex > records)
46 {
47 endIndex = records;
48 }
49
50 dsCurrentNews = allNews.Clone();
51
52 for (int i = startIndex; i < endIndex; i++)
53 {
54 DataRow row = dsCurrentNews.Tables[0].NewRow();
55 row.ItemArray = allNews.Tables[0].Rows[i].ItemArray;
56 dsCurrentNews.Tables[0].Rows.Add(row);
57 }
58 return dsCurrentNews;
59 }
60}
Default.aspx.cs
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10
11
12
13public partial class _Default : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (!IsPostBack)
18 {
19 int totalPage;
20 int records;
21 NewsDB newsDB = new NewsDB();
22 DataList1.DataSource = newsDB.CurrentPageNews(1, 10, 0, out totalPage, out records);
23 DataList1.DataBind();
24
25 }
26 }
27}
28
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10
11
12
13public partial class _Default : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (!IsPostBack)
18 {
19 int totalPage;
20 int records;
21 NewsDB newsDB = new NewsDB();
22 DataList1.DataSource = newsDB.CurrentPageNews(1, 10, 0, out totalPage, out records);
23 DataList1.DataBind();
24
25 }
26 }
27}
28