AJAX+ASP.NET高效分页
一般处理程序页
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using PublicTool;
namespace OA.Module.WorkReport
{
/// <summary>
/// pageData 的摘要说明
/// </summary>
public class pageData : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
Model.User u = Personal.ReadAuthCookie();
string uid = u.Id.ToString();
int start = Convert.ToInt32(context.Request.Params["start"]);
int rtype = Convert.ToInt32(context.Request.Params["type"]);
string type = "";
if (rtype == 3)
{
type = " r.Type>0 and Managers like '%|" + uid + "|%')";
}
else if (rtype == 1)
{
type = " r.Type=1 and Managers like '%|" + uid + "|%')";
}
else if (rtype == 2)
{
type = "r.Type=2 and Managers like '%|" + uid + "|%')";
}
BLL.Report br = new BLL.Report();
DataTable dt = br.getdata(start, type).Tables[0];
string getstring = "";
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
int rid = Convert.ToInt32(dt.Rows[i][0]);//汇报ID
string ntype = dt.Rows[i][1].ToString();//中文类型
int types = Convert.ToInt32(dt.Rows[i][2]);//数字类型
string rname = dt.Rows[i][3].ToString();//作者
DateTime dat = Convert.ToDateTime(dt.Rows[i][4]);//添加时间
string title = dt.Rows[i][5].ToString();//标题
string str = "<tr>"
+ "<td>" + title + "</td>"
+ "<td>" + ntype + "</td>"
+ "<td>" + rname + "</td>"
+ "<td>" + dat + "</td>"
+ "<td><a href='ReadWeek.aspx?reportid=" + rid + "&type=" + types + "' target='_blank'>点评</a></td>"
+ "</tr>";
getstring += str;
}
}
context.Response.Write(getstring);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
ASPX页面代码
<script type="text/javascript">
$(window).load(function () {
$("#months").hide();
$("#weeks").hide();
});
function show(did) {
$("#alls").show();
$("#months").hide();
$("#weeks").hide();
$("#alls").get(0).selectedIndex = 0;
getdata(0, parseInt(did));
}
function myweek(did) {
$("#alls").hide();
$("#months").hide();
$("#weeks").show();
$("#weeks").get(0).selectedIndex = 0;
getdata(0, parseInt(did));
} function mymonth(did) {
$("#alls").hide();
$("#months").show();
$("#weeks").hide();
$("#months").get(0).selectedIndex = 0;
getdata(0, parseInt(did));
}
function getdata(start, type) {
$.ajax({
type: "GET",
url: "pageData.ashx",
data: "start=" + start + "&type=" + type,
success: function (msg) {
var data;
if (msg != null && msg != "") {
$("#filldata").html(msg);
} else {
$("#filldata").html("暂无信息");
}
}
});
}
function changePage() {
var p = $("#alls").val();
getdata(p*10-10, 3);
}
function weeks() {
var p = $("#weeks").val();
getdata(p * 10 - 10, 1);
}
function months() {
var p = $("#months").val();
getdata(p * 10 - 10, 2);
}
</script>
<link href="../../CSS/OaIndex.css" rel="stylesheet" type="text/css" />
<link href="../../CSS/leave.css" rel="stylesheet" type="text/css" />
<link href="../../CSS/permission.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div class="p-right">
<div class="a-title">
<span onclick="show(3)" style=" cursor:pointer">我收到的工作汇报</span> <span onclick="myweek(1)" style=" cursor:pointer">我收到的周报</span> <span onclick="mymonth(2)" style=" cursor:pointer">我收到的月报</span><select id="alls" onchange="changePage()" runat="server"></select><select id="weeks" onchange="weeks()" runat="server"></select><select id="months" onchange="months()" runat="server"></select></div>
<table>
<tr>
<th width="30%" class="left">
标题
</th>
<th>
类型
</th>
<th>
申请人
</th>
<th>
填写时间
</th>
<th>
操作
</th>
</tr>
</table>
<div id="alldata" runat="server">
<table class="m-table" id="filldata">
<asp:Repeater ID="WRR" runat="server" OnItemDataBound="WRR_ItemDataBound">
<ItemTemplate>
<tr>
<td class="left">
<asp:Literal ID="rename" runat="server"></asp:Literal>
<asp:HiddenField ID="reportid" runat="server" Value='<%#eval_r("Title")%>' />
</td>
<td>
<%#DataBinder.eval_r(Container.DataItem,"cType")%><asp:HiddenField ID="workid" runat="server"
Value='<%#eval_r("ReportId") %>' />
</td>
<td>
<asp:Label ID="managerL" runat="server" Text="Label"></asp:Label>
<asp:HiddenField ID="manager" runat="server" Value='<%#eval_r("UserId") %>' />
</td>
<td>
<%#DataBinder.eval_r(Container.DataItem,"AddTime")%>
</td>
<td>
<a href="ReadWeek.aspx?reportid=<%#DataBinder.eval_r(Container.DataItem,"ReportId")%>&type=<%#DataBinder.eval_r(Container.DataItem,"Type") %>"
target="_blank">点评</a>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
</body>
ASPX后台页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OA.PersonalPage;
using PublicTool;
namespace OA.Module.WorkReport
{
public partial class UnderReport : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddate();
}
}
private void binddate()
{
string sqlt = "Managers like '%|" + PersonalID + "|%' order by AddTime desc";
BLL.Report brp = new BLL.Report();
DataTable td = brp.GetList(sqlt).Tables[0];
int count = td.Rows.Count;
int realc = count / 10 + 1;
if (realc > 0)
{
for (int i = 1; i <= realc; i++)
{
ListItem lt = new ListItem("第" + i + "页", i.ToString());
alls.Items.Add(lt);
weeks.Items.Add(lt);
months.Items.Add(lt);
}
}
PagedDataSource pds = new PagedDataSource();
pds.DataSource = td.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
pds.CurrentPageIndex = 0;
WRR.DataSource = pds;
WRR.DataBind();
}
}
}
关键SQL语句
select * from(select r.ReportId,r.Type,u.RealName,r.AddTime,r.Title,ROW_NUMBER() over(Order by r.AddTime)rownum from Report as r left outer join [User] as u on r.UserId=u.Id where r.Type>0 and Managers like '%|101|%')t where t.rownum>@startRowsIndex and t.rownum<=@startRowsIndex+@MaximunIndex
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using PublicTool;
namespace OA.Module.WorkReport
{
/// <summary>
/// pageData 的摘要说明
/// </summary>
public class pageData : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
Model.User u = Personal.ReadAuthCookie();
string uid = u.Id.ToString();
int start = Convert.ToInt32(context.Request.Params["start"]);
int rtype = Convert.ToInt32(context.Request.Params["type"]);
string type = "";
if (rtype == 3)
{
type = " r.Type>0 and Managers like '%|" + uid + "|%')";
}
else if (rtype == 1)
{
type = " r.Type=1 and Managers like '%|" + uid + "|%')";
}
else if (rtype == 2)
{
type = "r.Type=2 and Managers like '%|" + uid + "|%')";
}
BLL.Report br = new BLL.Report();
DataTable dt = br.getdata(start, type).Tables[0];
string getstring = "";
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
int rid = Convert.ToInt32(dt.Rows[i][0]);//汇报ID
string ntype = dt.Rows[i][1].ToString();//中文类型
int types = Convert.ToInt32(dt.Rows[i][2]);//数字类型
string rname = dt.Rows[i][3].ToString();//作者
DateTime dat = Convert.ToDateTime(dt.Rows[i][4]);//添加时间
string title = dt.Rows[i][5].ToString();//标题
string str = "<tr>"
+ "<td>" + title + "</td>"
+ "<td>" + ntype + "</td>"
+ "<td>" + rname + "</td>"
+ "<td>" + dat + "</td>"
+ "<td><a href='ReadWeek.aspx?reportid=" + rid + "&type=" + types + "' target='_blank'>点评</a></td>"
+ "</tr>";
getstring += str;
}
}
context.Response.Write(getstring);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
ASPX页面代码
<script type="text/javascript">
$(window).load(function () {
$("#months").hide();
$("#weeks").hide();
});
function show(did) {
$("#alls").show();
$("#months").hide();
$("#weeks").hide();
$("#alls").get(0).selectedIndex = 0;
getdata(0, parseInt(did));
}
function myweek(did) {
$("#alls").hide();
$("#months").hide();
$("#weeks").show();
$("#weeks").get(0).selectedIndex = 0;
getdata(0, parseInt(did));
} function mymonth(did) {
$("#alls").hide();
$("#months").show();
$("#weeks").hide();
$("#months").get(0).selectedIndex = 0;
getdata(0, parseInt(did));
}
function getdata(start, type) {
$.ajax({
type: "GET",
url: "pageData.ashx",
data: "start=" + start + "&type=" + type,
success: function (msg) {
var data;
if (msg != null && msg != "") {
$("#filldata").html(msg);
} else {
$("#filldata").html("暂无信息");
}
}
});
}
function changePage() {
var p = $("#alls").val();
getdata(p*10-10, 3);
}
function weeks() {
var p = $("#weeks").val();
getdata(p * 10 - 10, 1);
}
function months() {
var p = $("#months").val();
getdata(p * 10 - 10, 2);
}
</script>
<link href="../../CSS/OaIndex.css" rel="stylesheet" type="text/css" />
<link href="../../CSS/leave.css" rel="stylesheet" type="text/css" />
<link href="../../CSS/permission.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div class="p-right">
<div class="a-title">
<span onclick="show(3)" style=" cursor:pointer">我收到的工作汇报</span> <span onclick="myweek(1)" style=" cursor:pointer">我收到的周报</span> <span onclick="mymonth(2)" style=" cursor:pointer">我收到的月报</span><select id="alls" onchange="changePage()" runat="server"></select><select id="weeks" onchange="weeks()" runat="server"></select><select id="months" onchange="months()" runat="server"></select></div>
<table>
<tr>
<th width="30%" class="left">
标题
</th>
<th>
类型
</th>
<th>
申请人
</th>
<th>
填写时间
</th>
<th>
操作
</th>
</tr>
</table>
<div id="alldata" runat="server">
<table class="m-table" id="filldata">
<asp:Repeater ID="WRR" runat="server" OnItemDataBound="WRR_ItemDataBound">
<ItemTemplate>
<tr>
<td class="left">
<asp:Literal ID="rename" runat="server"></asp:Literal>
<asp:HiddenField ID="reportid" runat="server" Value='<%#eval_r("Title")%>' />
</td>
<td>
<%#DataBinder.eval_r(Container.DataItem,"cType")%><asp:HiddenField ID="workid" runat="server"
Value='<%#eval_r("ReportId") %>' />
</td>
<td>
<asp:Label ID="managerL" runat="server" Text="Label"></asp:Label>
<asp:HiddenField ID="manager" runat="server" Value='<%#eval_r("UserId") %>' />
</td>
<td>
<%#DataBinder.eval_r(Container.DataItem,"AddTime")%>
</td>
<td>
<a href="ReadWeek.aspx?reportid=<%#DataBinder.eval_r(Container.DataItem,"ReportId")%>&type=<%#DataBinder.eval_r(Container.DataItem,"Type") %>"
target="_blank">点评</a>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
</body>
ASPX后台页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OA.PersonalPage;
using PublicTool;
namespace OA.Module.WorkReport
{
public partial class UnderReport : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddate();
}
}
private void binddate()
{
string sqlt = "Managers like '%|" + PersonalID + "|%' order by AddTime desc";
BLL.Report brp = new BLL.Report();
DataTable td = brp.GetList(sqlt).Tables[0];
int count = td.Rows.Count;
int realc = count / 10 + 1;
if (realc > 0)
{
for (int i = 1; i <= realc; i++)
{
ListItem lt = new ListItem("第" + i + "页", i.ToString());
alls.Items.Add(lt);
weeks.Items.Add(lt);
months.Items.Add(lt);
}
}
PagedDataSource pds = new PagedDataSource();
pds.DataSource = td.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
pds.CurrentPageIndex = 0;
WRR.DataSource = pds;
WRR.DataBind();
}
}
}
关键SQL语句
select * from(select r.ReportId,r.Type,u.RealName,r.AddTime,r.Title,ROW_NUMBER() over(Order by r.AddTime)rownum from Report as r left outer join [User] as u on r.UserId=u.Id where r.Type>0 and Managers like '%|101|%')t where t.rownum>@startRowsIndex and t.rownum<=@startRowsIndex+@MaximunIndex