您的位置:首页 > 编程语言 > ASP

Asp.net用ajax技术实现无刷新分页

2010-10-31 09:37 721 查看
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// db 的摘要说明
/// </summary>
public class db
{
public db()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//public SqlConnection Conn()
//{

// SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);// 连接数据库
// return conn;
//}

//返回DATASET
public static DataSet sta()
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);//连接数据库
conn.Open();
//SqlCommand com = new SqlCommand("PageCut", conn); com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter MyCommand = new SqlDataAdapter("PageCut", conn);
//SqlDataReader dbr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure;

SqlParameter strGetFields = new SqlParameter("@strGetFields",SqlDbType.VarChar);
strGetFields.Value = "*";//字段
MyCommand.SelectCommand.Parameters.Add(strGetFields);

SqlParameter fldName = new SqlParameter("@fldName",SqlDbType.VarChar);
fldName.Value = "id";//排序字段
MyCommand.SelectCommand.Parameters.Add(fldName);

SqlParameter PageSize = new SqlParameter("@PageSize",SqlDbType.Int);
PageSize.Value = 10;//每页大小
MyCommand.SelectCommand.Parameters.Add(PageSize);

SqlParameter PageIndex = new SqlParameter("@PageIndex",SqlDbType.Int);
PageIndex.Value =1;//当前页
MyCommand.SelectCommand.Parameters.Add(PageIndex);

SqlParameter strWhere = new SqlParameter("@strWhere",SqlDbType.VarChar);
strWhere.Value = "";//条件
MyCommand.SelectCommand.Parameters.Add(strWhere);

SqlParameter Counts = new SqlParameter("@Counts",SqlDbType.Int);
Counts.Value = 1;//总记录数
Counts.Direction = ParameterDirection.Output;//因为这里要返回,所以要设置一下,返回计算过后的结果
MyCommand.SelectCommand.Parameters.Add(Counts);
MyCommand.Fill(ds);
return ds;

}
public static SqlDataAdapter dt(int Pagesize)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);//连接数据库
conn.Open();
//SqlCommand com = new SqlCommand("PageCut", conn); com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter MyCommand = new SqlDataAdapter("PageCut", conn);
//SqlDataReader dbr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure;

SqlParameter strGetFields = new SqlParameter("@strGetFields", SqlDbType.VarChar);
strGetFields.Value = "*";//字段
MyCommand.SelectCommand.Parameters.Add(strGetFields);

SqlParameter fldName = new SqlParameter("@fldName", SqlDbType.VarChar);
fldName.Value = "id";//排序字段
MyCommand.SelectCommand.Parameters.Add(fldName);

SqlParameter PageSize = new SqlParameter("@PageSize", SqlDbType.Int);
PageSize.Value = 10;//每页大小
MyCommand.SelectCommand.Parameters.Add(PageSize);

SqlParameter PageIndex = new SqlParameter("@PageIndex", SqlDbType.Int);
PageIndex.Value = Pagesize;//当前页
MyCommand.SelectCommand.Parameters.Add(PageIndex);

SqlParameter strWhere = new SqlParameter("@strWhere", SqlDbType.VarChar);
strWhere.Value = "";//条件
MyCommand.SelectCommand.Parameters.Add(strWhere);

SqlParameter Counts = new SqlParameter("@Counts", SqlDbType.Int);
Counts.Value = 1;//总记录数
Counts.Direction = ParameterDirection.Output;//因为这里要返回,所以要设置一下,返回计算过后的结果
MyCommand.SelectCommand.Parameters.Add(Counts);
conn.Close();
return MyCommand;

}

public static SqlDataReader sdr(int Pagesize)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);//连接数据库
conn.Open();
SqlCommand com = new SqlCommand("PageCut", conn);
com.CommandType = CommandType.StoredProcedure;

SqlParameter strGetFields = new SqlParameter("@strGetFields", SqlDbType.VarChar);
strGetFields.Value = "*";//字段
com.Parameters.Add(strGetFields);

SqlParameter fldName = new SqlParameter("@fldName", SqlDbType.VarChar);
fldName.Value = "id";//排序字段
com.Parameters.Add(fldName);

SqlParameter PageSize = new SqlParameter("@PageSize", SqlDbType.Int);
PageSize.Value = 10;//每页大小
com.Parameters.Add(PageSize);

SqlParameter PageIndex = new SqlParameter("@PageIndex", SqlDbType.Int);
PageIndex.Value = Pagesize;//当前页
com.Parameters.Add(PageIndex);

SqlParameter strWhere = new SqlParameter("@strWhere", SqlDbType.VarChar);
strWhere.Value = "";//条件
com.Parameters.Add(strWhere);

SqlParameter Counts = new SqlParameter("@Counts", SqlDbType.Int);
Counts.Value = 1;//总记录数
Counts.Direction = ParameterDirection.Output;//因为这里要返回,所以要设置一下,返回计算过后的结果
com.Parameters.Add(Counts);
SqlDataReader sdr =com.ExecuteReader();
return sdr;

}

public static SqlDataReader sdr()
{
throw new Exception("The method or operation is not implemented.");
}
}

建立一个.aspx页(Default.aspx)调用db.cs类:

public partial class _Default : System.Web.UI.Page
{
db db = new db();
public string qq;
public int pageze=1;
public int temp;
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
getveiw(1);

next();

}

}
// public override void VerifyRenderingInServerForm(Control control)
// {

// Confirms that an HtmlForm control is rendered for

// }
public void next()
{

if (Request.QueryString["next"] == "next")
{
temp = Convert.ToInt32(Request.QueryString["name"]);
getveiw(temp);

}

}

public void getveiw(int pagesize)
{
SqlDataAdapter sta = db.dt(pagesize);
DataSet ds=new DataSet();
sta.Fill(ds);
GridView1.DataSource=ds;
this.GridView1.DataBind();

}

在这里为了方便我们姑且用gridview来绑定数据(我们也可以将数据以XML文档存储)。建立一个HTML新页(HTMLPage.htm)用 AJAX技术调用.aspx页。XMLHttpRequest提供了两个用来访问服务器响应的属性:responseText和responseXML。在这里由于我运用gridview存储数据。所以我们运用responseText属性(它返回的是一个串,即html串)。假如我们运用XML来存储数据的话,那我们应该用responseXML属性了。好了.html的代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页</title>
<script type="text/javascript">
var xmlHttp

var page
function crrate()
{
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
//初始化为第一页
function star()
{page=1;
crrate()
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET","Default.aspx",true)
xmlHttp.send(null)
}
//下一页
function next()
{page=page+1
str= document.getElementById("Text1").value;
document.getElementById("txtHint").innerHTML="";
var url="Default.aspx?next=next&name="+page;
crrate();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null)
}
//跳转到某一页
function go()
{
str= document.getElementById("Text1").value;
page=str;
document.getElementById("txtHint").innerHTML="";
var url="Default.aspx?next=next&name="+page;
crrate();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null)
}
//上一页
function prve()
{
page=page-1
str= document.getElementById("Text1").value;
document.getElementById("txtHint").innerHTML="";
var url="Default.aspx?next=next&name="+page;
crrate();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null)

}
//解析服务器响应提供的串
function stateChanged()
{
if (xmlHttp.readyState==4)
{if(xmlHttp.status==200){
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
}
setInterval('star()');
</script>
</head>
<body>
<div id="txtHint"></div>
</body>
<input id="Text1" type="text" style="width:20px"/><input id="Button3" type="button"
value="go" onclick="go()"/><input id="Button1" type="button" value="下一页" onclick="next()"/><input
id="Button2" type="button" value="上一页" onclick="prve()" />
</html>

代码说明(关于ajax的知识点我就不细说了):url="Default.aspx?next=next&name="+page;将 next=next&name="+page传给.aspx页其中page是要显示的页码。.aspx页接收后将运行: if (Request.QueryString["next"] == "next")
{
temp = Convert.ToInt32(Request.QueryString["name"]);
getveiw(temp);

}语句很简单。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: