程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> C#關於分頁顯示,

C#關於分頁顯示,

編輯:關於.NET

C#關於分頁顯示,


---<PS:本人菜鳥,大手子還請高台貴手>

以下是我今天在做分頁時所遇到的一個分頁顯示問題,使用拼寫SQL的方式寫的,同類型可參考哦~

 

-----------------------------------------------------------------------------------------------------------

---------------------------------------------Model-Page----------------------------------------------------

-----------------------------------------------------------------------------------------------------------
public class page<T>
{
public int PageIndex { get; set; }
public int PageSzie { get; set; }
public int DbCount { get; set; }
public List<T> Dblist { get; set; }

}

 

----------------------------------------------------------------------------------------------------

---------------------------------------------DAL----------------------------------------------------

----------------------------------------------------------------------------------------------------
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=CMS;Integrated Security=True");//數據庫連接字符串

/// <summary>
/// 查詢總數
/// </summary>
/// <returns></returns>
public int SelectCount()
{
con.Open();

string sql = "SELECT COUNT(*) FROM AdvertNews";
SqlCommand com = new SqlCommand(sql, con);
int Count = Convert.ToInt32(com.ExecuteScalar());

con.Close();
return Count;
}

/// <summary>
/// 廣告新聞 分頁顯示
/// </summary>
/// <param name="PageIndex"></param>
/// <param name="PageSize"></param>
/// <returns></returns>
public List<AdvertNews> Adv_Show(int PageIndex, int PageSize)
{
con.Open();

string sql = "";
sql += "SELECT * FROM (";
sql += "SELECT ROW_NUMBER() OVER(ORDER BY AdvertNews_Id)id,AdvertNews_Id,AdvertNews_Title,AdvertNews_Content,AdvertNews_Writer,AdvertNews_Date,AdvertNews_State,AdvertNews_Url FROM AdvertNews";
sql += ")T WHERE T.id >=" + ((PageIndex - 1) * PageSize + 1);
sql += "AND T.id <=" + PageIndex * PageSize;
SqlCommand com = new SqlCommand(sql, con);

SqlDataReader reader = com.ExecuteReader();

List<AdvertNews> list = new List<AdvertNews>();

while (reader.Read())
{
AdvertNews a = new AdvertNews();
a.AdvertNews_Id = Convert.ToInt32(reader["AdvertNews_Id"]);
a.AdvertNews_Title = reader["AdvertNews_Title"].ToString();
a.AdvertNews_Content = reader["AdvertNews_Content"].ToString();
a.AdvertNews_Writer = reader["AdvertNews_Writer"].ToString();
a.AdvertNews_Date = Convert.ToDateTime(reader["AdvertNews_Date"]);
a.AdvertNews_State = Convert.ToInt32(reader["AdvertNews_State"]);
a.AdvertNews_Url = reader["AdvertNews_Url"].ToString();

list.Add(a);
}
con.Close();

return list;
}

 

 

----------------------------------------------------------------------------------------------------

---------------------------------------------BLL----------------------------------------------------

----------------------------------------------------------------------------------------------------

AdvertNews_Dal dal = new AdvertNews_Dal();

public page<AdvertNews> Adv_Show(int PageIndex, int PageSize)
{
page<AdvertNews> p = new page<AdvertNews>();
p.PageIndex = PageIndex;
p.PageSzie = PageSize;
p.DbCount = dal.SelectCount();
p.Dblist = dal.Adv_Show(PageIndex, PageSize);

return p;
}

 

----------------------------------------------------------------------------------------------------

---------------------------------------------Controller---------------------------------------------

----------------------------------------------------------------------------------------------------

AdvertNews_Bll adv_bll = new AdvertNews_Bll();
public ActionResult Adv_Show()
{
return View(adv_bll.Adv_Show(1,8));
}

public ActionResult Adv_Second_Show(int id)
{
return View("Adv_Show", adv_bll.Adv_Show(id, 8));
}

 

----------------------------------------------------------------------------------------------

---------------------------------------------前台---------------------------------------------

----------------------------------------------------------------------------------------------

 

<table>
<tr>
<td>新聞標號</td>
<td>新聞標題</td>
</tr>
@foreach (var item in Model.Dblist)
{
<tr>
<td>@item.AdvertNews_Id</td>
<td>@item.AdvertNews_Title</td>
</tr>
}
</table>

<div >
<a href="/AdvertNews/Adv_Show">首頁</a>
@if (Model.PageIndex > 1)
{
<a href="/AdvertNews/Adv_Second_Show/@(Model.PageIndex-1)">上一頁</a>
}
@if (Model.DbCount % Model.PageSzie == 0)
{
if (Model.PageIndex < Model.DbCount / Model.PageSzie)
{
<a href="/AdvertNews/Adv_Second_Show/@(Model.PageIndex+1)">下一頁</a>
}
}
else
{
if (Model.PageIndex < Model.DbCount / Model.PageSzie + 1)
{
<a href="/AdvertNews/Adv_Second_Show/@(Model.PageIndex+1)">下一頁</a>
}
}
@if (Model.DbCount % Model.PageSzie == 0)
{
<a href="/AdvertNews/Adv_Second_Show/@(Model.DbCount/Model.PageSzie)">尾頁</a>
}
else
{
<a href="/AdvertNews/Adv_Second_Show/@(Model.DbCount/Model.PageSzie+1)">尾頁</a>
}
</div>
<div >
@{
var page = 0;
if (Model.DbCount % Model.PageSzie == 0)
{
page = Model.DbCount / Model.PageSzie;
}
else
{
page = Model.DbCount / Model.PageSzie + 1;
}
<span>
數據總條數:@Model.DbCount &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
總頁數為:@page &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
當前頁:@Model.PageIndex
</span>
}
</div>

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved