asp.net mvc1 分页
以下是需要显示的页面html代码,model就不用介绍了
可能页码代码有些幼稚,臃肿,请指正!
代码思想:
1, 每页显示10条
2, 点击页码的时候就会从数据库中取从页码*10到页码*10+10的数据
3, 点击“>”时,跳转到下个十页的第一页
4, 点击“>>”,跳转到最后一页
5, 在点击吗的时候,我会计算页面吗当前值的起始位置和最后数字,比如:我点击9页,那么页面上显示的是1-10页,而不是9到19页
以下是页面显示数据的代码:
<%using (Html.BeginForm())
{ %>
<TABLE style="width:100%;">
<%for (int i = 0; i < Model.RoguMoelS.Count; i++) %>
<%{ %>
<TR>
<td align=center style="width:100%;">
<table style="width:700px;">
<tr>
<TD rowspan="2" style="width:180px;height:15px" valign=top> <%= Model.RoguMoelS[i].OperateTime%> </TD>
<td style="height:15px" align=left>
<% if (!string.IsNullOrEmpty(Model.RoguMoelS[i].Equipname))
{ %>
<%=Model.RoguMoelS[i].Equipname%>
<% } %>
<%=Model.RoguMoelS[i].PointName%>
</td>
</tr>
<tr>
<td style="height:15px" align=left>
<% if (!string.IsNullOrEmpty(Model.RoguMoelS[i].Message))
{ %>
<%=Model.RoguMoelS[i].Message%>
<% } %>
<% if (!string.IsNullOrEmpty(Model.RoguMoelS[i].NewState))
{ %>
<%=Model.RoguMoelS[i].NewState%>
<% } %>
<%=Model.RoguMoelS[i].Failinfo%>
</td>
</tr>
</table>
</td>
</TR>
<%} %>
{ %>
<TABLE style="width:100%;">
<%for (int i = 0; i < Model.RoguMoelS.Count; i++) %>
<%{ %>
<TR>
<td align=center style="width:100%;">
<table style="width:700px;">
<tr>
<TD rowspan="2" style="width:180px;height:15px" valign=top> <%= Model.RoguMoelS[i].OperateTime%> </TD>
<td style="height:15px" align=left>
<% if (!string.IsNullOrEmpty(Model.RoguMoelS[i].Equipname))
{ %>
<%=Model.RoguMoelS[i].Equipname%>
<% } %>
<%=Model.RoguMoelS[i].PointName%>
</td>
</tr>
<tr>
<td style="height:15px" align=left>
<% if (!string.IsNullOrEmpty(Model.RoguMoelS[i].Message))
{ %>
<%=Model.RoguMoelS[i].Message%>
<% } %>
<% if (!string.IsNullOrEmpty(Model.RoguMoelS[i].NewState))
{ %>
<%=Model.RoguMoelS[i].NewState%>
<% } %>
<%=Model.RoguMoelS[i].Failinfo%>
</td>
</tr>
</table>
</td>
</TR>
<%} %>
以下是页码的代码:
<% int Statrpage = 1; if (Model.CurrentPage != 0) Statrpage = (Model.CurrentPage % 10 == 0 ? (Model.CurrentPage / 10 >= 1 ? Model.CurrentPage / 10 - 1 : Model.CurrentPage / 10) * 10 +1: (Model.CurrentPage / 10) * 10 + 1); %>
<% if (Model.CurrentPage > 10) %>
<%{ %>
<%=Html.ActionLink("<<", "Index", new { id = 1 }, new { onclick = "ChangeClass()" })%>
<%=Html.ActionLink("<", "Index", new { id = Statrpage - (Statrpage % 10) - 9 }, new { onclick = "ChangeClass()" })%>
<%} %>
<% for (int i = Statrpage; i <= Statrpage + 9; i++)
{%>
<%if (i <= (Model.totalCount % 10 == 0 ? Model.totalCount / 10 : Model.totalCount / 10 + 1) && Model.totalCount > 0) %>
<%=Html.ActionLink(i.ToString(), "Index", new { id = i }, new { onclick = "ChangeClass()" })%>
<%} %>
<% if (Model.CurrentPage + 10 <= Model.totalCount / 10 + (10 - Model.totalCount / 10 % 10) && Model.totalCount > 10) %>
<%{ %>
<%=Html.ActionLink(">", "Index", new { id = Statrpage + 10 }, new { onclick = "ChangeClass()" })%>
<%=Html.ActionLink(">>", "Index", new { id = Model.totalCount % 10 == 0 ? Model.totalCount / 10 : Model.totalCount / 10 + 1 }, new { onclick = "ChangeClass()" })%>
<%} %>
<% if (Model.CurrentPage > 10) %>
<%{ %>
<%=Html.ActionLink("<<", "Index", new { id = 1 }, new { onclick = "ChangeClass()" })%>
<%=Html.ActionLink("<", "Index", new { id = Statrpage - (Statrpage % 10) - 9 }, new { onclick = "ChangeClass()" })%>
<%} %>
<% for (int i = Statrpage; i <= Statrpage + 9; i++)
{%>
<%if (i <= (Model.totalCount % 10 == 0 ? Model.totalCount / 10 : Model.totalCount / 10 + 1) && Model.totalCount > 0) %>
<%=Html.ActionLink(i.ToString(), "Index", new { id = i }, new { onclick = "ChangeClass()" })%>
<%} %>
<% if (Model.CurrentPage + 10 <= Model.totalCount / 10 + (10 - Model.totalCount / 10 % 10) && Model.totalCount > 10) %>
<%{ %>
<%=Html.ActionLink(">", "Index", new { id = Statrpage + 10 }, new { onclick = "ChangeClass()" })%>
<%=Html.ActionLink(">>", "Index", new { id = Model.totalCount % 10 == 0 ? Model.totalCount / 10 : Model.totalCount / 10 + 1 }, new { onclick = "ChangeClass()" })%>
<%} %>
以下是control里面用到的代码:
public ActionResult Index(int? id)
{
int it = 1;
if (id != null)
it =Convert.ToInt32(id.ToString());
if (Request["id"] != null)
it = int.Parse(Request["id"].ToString());
var model = new RoguEditModel { RoguMoelS = GetData(it).ToList(), CurrentPage = it, totalCount = TotalCount };
return View(model);
}
public List<RoguModel> GetData(int page)
{
//int sixe = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Opejnl_Dat));
List<RoguModel> RoguList = new List<RoguModel>();
try
{
//log.Error("yunyong" + DateTime.Now.ToString());
RoguModel Rogu;
Opejnl_Dat[] table = GetDataFromTaotaldata();//CGetShareMemoryData.GetOpejnl_Dat();
if (table!=null)
CheckTable(ref table);
List<Opejnl_Dat> tabelfile = GetListFromFile();
Opejnl_Dat[] TableFileArr ;
if (tabelfile != null)
{
TableFileArr = tabelfile.ToArray();
CheckTable(ref TableFileArr);
tabelfile = TableFileArr.ToList();
}
List<Opejnl_Dat> totalList = new List<Opejnl_Dat>();
if (table != null)
totalList = table.ToList();
if (tabelfile != null)
totalList.AddRange(tabelfile);
table = totalList.ToArray();
if (table != null)
{
TotalCount = table.Length;
for (int i = (page == 0 ? page : page - 1) * 10; i < (page == 0 ? page : page - 1) * 10 + 10; i++)
{
if (i < table.Length)
{
//Rogu = new RoguModel();
if (table[i].msg_no != 0)
{
Rogu = GetRogu(table[i]);
RoguList.Add(Rogu);
}
}
}
}
else
{
Rogu = new RoguModel();
RoguList.Add(Rogu);
}
}
catch (Exception ex)
{
log.Error("Can not Load data! " + ex.StackTrace);
return RoguList;
}
//log.Error("yunyong"+DateTime.Now.ToString());
return RoguList;
}
{
int it = 1;
if (id != null)
it =Convert.ToInt32(id.ToString());
if (Request["id"] != null)
it = int.Parse(Request["id"].ToString());
var model = new RoguEditModel { RoguMoelS = GetData(it).ToList(), CurrentPage = it, totalCount = TotalCount };
return View(model);
}
public List<RoguModel> GetData(int page)
{
//int sixe = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Opejnl_Dat));
List<RoguModel> RoguList = new List<RoguModel>();
try
{
//log.Error("yunyong" + DateTime.Now.ToString());
RoguModel Rogu;
Opejnl_Dat[] table = GetDataFromTaotaldata();//CGetShareMemoryData.GetOpejnl_Dat();
if (table!=null)
CheckTable(ref table);
List<Opejnl_Dat> tabelfile = GetListFromFile();
Opejnl_Dat[] TableFileArr ;
if (tabelfile != null)
{
TableFileArr = tabelfile.ToArray();
CheckTable(ref TableFileArr);
tabelfile = TableFileArr.ToList();
}
List<Opejnl_Dat> totalList = new List<Opejnl_Dat>();
if (table != null)
totalList = table.ToList();
if (tabelfile != null)
totalList.AddRange(tabelfile);
table = totalList.ToArray();
if (table != null)
{
TotalCount = table.Length;
for (int i = (page == 0 ? page : page - 1) * 10; i < (page == 0 ? page : page - 1) * 10 + 10; i++)
{
if (i < table.Length)
{
//Rogu = new RoguModel();
if (table[i].msg_no != 0)
{
Rogu = GetRogu(table[i]);
RoguList.Add(Rogu);
}
}
}
}
else
{
Rogu = new RoguModel();
RoguList.Add(Rogu);
}
}
catch (Exception ex)
{
log.Error("Can not Load data! " + ex.StackTrace);
return RoguList;
}
//log.Error("yunyong"+DateTime.Now.ToString());
return RoguList;
}

浙公网安备 33010602011771号