C#分页
作者:互联网
存储过程:
create Proc PRoleInfo
@PageIndex int,
@PageSize int,
@Count int out,
@Name varchar(22)
as
select top(@pageSize) * from 表名 where 字段IDnot in
(
select top(@pageSize*(@pageIndex-1)) 字段ID
from 表名
order by Fid
)
order by 字段ID
select @count=COUNT(1) from 表名
后台方法:
public static DataTable GetTablePro(string PRoleInfo, int pageindex, int pagesize, out int RowCount, string name)
{
using (SqlConnection conn = new SqlConnection(ConnString))
{
conn.Open();
SqlCommand comm = new SqlCommand(PRoleInfo, conn);
comm.CommandType = CommandType.StoredProcedure;
SqlParameter[] para =
{
new SqlParameter("@pageindex",DbType.Int32),
new SqlParameter("@pagesize",DbType.Int32),
new SqlParameter("@name",DbType.String),
new SqlParameter("@RowCount",DbType.Int32)
};
para[0].Value = pageindex;
para[1].Value = pagesize;
para[2].Value = name;
para[3].Direction = ParameterDirection.Output;
comm.Parameters.AddRange(para);
SqlDataAdapter adapter = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
adapter.Fill(dt);
RowCount = Convert.ToInt32(para[3].Value);
return dt;
}
}
控制器:
public ActionResult Shows(int pageindex=1,string name="")
{
int pagesize = 5;
int RowCount = 0;
if (pageindex < 1)
{
pageindex = 1;
}
if (Session["Total"] != null && pageindex > Convert.ToInt32(Session["Total"]))
{
pageindex = Convert.ToInt32( Session["Total"]);
}
var list = show.Workshopshow(pageindex, pagesize, out RowCount, name).ToList();
int Total = RowCount % pagesize == 0 ? RowCount / pagesize : (RowCount / pagesize) + 1;
//pageindex = (RowCount + RowCount - 1) / RowCount;
Session["Total"] = Total;
//Session["Total"] = pageindex;
Session["name"] = name;
ViewBag.result = pageindex;
return View(list);
}
页面:
@{
ViewBag.Title = "Shows";
}
@using (Html.BeginForm())
{
<table class="table table-striped"><tr><th>车间名称: <input type="text" name="name" /> <input type="submit" value="搜索" /></th></tr></table>
}
<table class="table table-striped" border="1">
<tr>
<td>工厂编号</td>
<td>车间编号</td>
<td>车间中文名称</td>
<td>车间英文名称</td>
<td>信息来源</td>
<td>登录人编号</td>
<td>登录日期</td>
<td>最后修改人编号</td>
<td>最后修改人日期</td>
<td>状态</td>
<td>删除</td>
</tr>
@{
foreach (var item in Model)
{
<tr>
<td>@item.FactoryCode</td>
<td>@item.WorkshopCode</td>
<td>@item.WorkshopName</td>
<td>@item.WorkshopName_EN</td>
<td>@item.Source</td>
<td>@item.CreaterNo</td>
<td>@item.CreatedDate</td>
<td>@item.ModifierNo</td>
<td>@item.ModifiedDate</td>
<td>@item.Status</td>
<td><a href="#">删除</a></td>
</tr>
}
}
</table>
<table style=" margin:0 auto">
<tr>
<td>
<ul class="pagination pagination-lg">
<li>
<li><a href="/Texts/Shows?name=@Session["name"]&pageindex=1">首页</a></li>
<li>
<a href="/Texts/Shows?name=@Session["name"]&pageindex=@(ViewBag.result-1)">«</a>
</li>
@{
for (int i = 0; i < Convert.ToInt32(Session["Total"]); i++)
{
if ((i + 1) == ViewBag.result)
{
<li>
<a href="/Texts/Shows?pageindex=@Session["name"]@(i+1)" style="font-size:25px;color:red">@(i + 1)</a>
</li>
}
else
{
<li>
<a href="/Texts/Shows?pageindex=@Session["name"]@(i+1)">@(i + 1)</a>
</li>
}
}
}
<li><a href="/Texts/Shows?name=@Session["name"]& pageindex=@(ViewBag.result+1)">»</a></li>
<li><a href="/Texts/Shows?name=@Session["name"]& pageindex=@Session["Total"]">尾页</a></li>
</ul>
</td>
</tr>
</table>
标签:pageindex,分页,pagesize,RowCount,C#,item,int,new 来源: https://www.cnblogs.com/Love20181128/p/14963473.html