求一个DataGrid无刷新分页例子,Ajax

cherry_j 2008-04-28 03:49:51
求一个DataGrid无刷新分页例子,Ajax

多谢
...全文
201 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
staywithc 2008-10-24
  • 打赏
  • 举报
回复
不错,顶
xuyiazl 2008-04-28
  • 打赏
  • 举报
回复




/// <summary>
/// 执行查询操作
/// </summary>
/// <param name="strSql"></param>
/// <param name="ds">返回数据集</param>
/// <returns></returns>
public DataSet ExecuteSelectCmmond(string strSQL, DataSet ds)
{
cn.Open();
SqlCommand cm = new SqlCommand();
cm.CommandText = strSQL;
cm.Connection = cn;
SqlDataAdapter da = new SqlDataAdapter(cm);
try
{
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cm.Dispose();
cn.Close();
}
}
/// <summary>
/// 执行查询数据库操作返回多少条记录 
/// </summary>
/// <param name="strSql"></param>
public int ExecuteScalarScalarCount(string strSql)
{
cn.Open();
SqlCommand cm = new SqlCommand();
cm.CommandText = strSql;
cm.Connection = cn;
try
{
int count = (int)cm.ExecuteScalar();
return count;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cm.Dispose();
cn.Close();
}
}
}

xuyiazl 2008-04-28
  • 打赏
  • 举报
回复

ajaxServers.aspx



using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Text;

public partial class ajaxServers : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;User ID=sa;pwd=sa");
protected int pageSize = 10;//定义每页需要显示的记录数
protected void Page_Load(object sender, EventArgs e)
{
//获取查询字符串表示的页数,默认为1
int page = 1;
//利用异常机制确保转换string到int类型不会出错
try
{
page = Convert.ToInt32(Request.QueryString["page"]);
//获得查询条件
string key = Request.QueryString["key"];
if (key == null) key = string.Empty;
//过滤特殊符号,避免SQL注入
key = key.Replace("'", "").Replace("\"", "");
//查询总记录数
int count = Convert.ToInt32(ExecuteScalarScalarCount("select count(*) from Orders where ShipCountry like '%" + key + "%'"));
//防止恶意传参
int pageCount = (int)Math.Ceiling((double)count / pageSize);
if (page > pageCount && pageCount != 0)
{
page = pageCount;
}
////实现自定义的分页逻辑 access可用sqlserver也可用
//StringBuilder sb = new StringBuilder("select top " + pageSize.ToString() + " * from info where title like '%" + key + "%'");
////如果不是第一页
//if (page > 1)
//{
// sb.Append(" and id not in(select top " + (pageSize * (page - 1)).ToString() + " id from info where title like '%" + key + "%' order by [date],id desc)");
//}
//sb.Append(" order by [date],id desc");

//实现自定义的分页逻辑 sqlserver可用
int newTop = Convert.ToInt32(pageSize) * Convert.ToInt32(page); //pageSize定义每页显示条数
int oldTop = newTop - Convert.ToInt32(pageSize);
StringBuilder sb = new StringBuilder("select * from (select top " + newTop + " * from [Orders] where ShipCountry like '%" + key + "%' order by OrderID DESC) a where a. OrderID not in(select top " + oldTop + " OrderID from [Orders] where ShipCountry like '%" + key + "%' order by OrderID DESC) order by OrderID DESC");

DataSet ds = new DataSet();
ds = ExecuteSelectCmmond(sb.ToString(), ds);

//定义gridvew
//GridView gv = new GridView();
//gv.DataSource = ds.Tables[0].DefaultView;
//gv.DataBind();
//Response.Write(this.GetStringByControl(gv) + "<div id='pager'>" + this.GenPager(page, pageSize, count) + "</div>");
Response.Write(this.StringDataSet(ds) + "<div id='pager'>" + this.GenPager(page, pageSize, count) + "</div>");
}
catch (Exception ex)
{
Response.Write("<a style='color:red'>操作异常!</a>");
}
}
/// <summary>
/// 构造数据表
/// </summary>
/// <param name="ds">数据集</param>
/// <returns>string</returns>
public string StringDataSet(DataSet ds)
{
StringBuilder sb = new StringBuilder();
//构造显示数据的table 且表头
sb.Append("<TABLE cellSpacing=1 cellPadding=2 width=800 align=center border=0>");
sb.Append("<TR><TD bgColor=#e8edf4 style='width:16%' align='center'>OrderID</TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:16%' align='center'>CustomerID</TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:16%' align='center'>OrderDate</TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:16%' align='center'>RequiredDate</TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:20%' align='center'>ShipAddress</TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:16%' align='center'>ShipCountry</TD></TR>");
//循环遍历表的每一行构造table
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
sb.Append("<TR><TD bgColor=#efefef>" + ds.Tables[0].Rows[i]["OrderID"].ToString() + "</TD>");
sb.Append("<TD bgColor=#efefef>" + ds.Tables[0].Rows[i]["CustomerID"].ToString() + "</TD>");
sb.Append("<TD bgColor=#efefef>" + ds.Tables[0].Rows[i]["OrderDate"].ToString() + "</TD>");
sb.Append("<TD bgColor=#efefef>" + ds.Tables[0].Rows[i]["RequiredDate"].ToString() + "</TD>");
sb.Append("<TD bgColor=#efefef>" + ds.Tables[0].Rows[i]["ShipAddress"].ToString() + "</TD>");
sb.Append("<TD bgColor=#efefef>" + ds.Tables[0].Rows[i]["ShipCountry"].ToString() + "</TD></TR>");
}
//构造表尾
sb.Append("<TR><TD bgColor=#e8edf4 style='width:16%;height:4px'></TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:16%;height:4px'></TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:16%;height:4px'></TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:16%;height:4px'></TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:20%;height:4px'></TD>");
sb.Append("<TD bgColor=#e8edf4 style='width:16%;height:4px'></TD></TR>");
sb.Append("</TABLE>");
return sb.ToString();
}

/// <summary>
/// 通过Asp.net控件获得其相应的HTML字符串表示
/// </summary>
/// <param name="c">Asp.net控件</param>
/// <returns></returns>
public string GetStringByControl(System.Web.UI.Control c)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter writer = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(writer);
c.RenderControl(htw);
return sb.ToString();
}

/// <summary>
/// 获得分页代码
/// </summary>
/// <param name="page">第几页</param>
/// <param name="pageSize">每页显示多少条数据</param>
/// <param name="count">数据总量</param>
/// <returns>StringBuilder</returns>
public string GenPager(int page, int pageSize, int count)
{
StringBuilder sb = new StringBuilder();
int pageCount = (int)Math.Ceiling((double)count / pageSize);
int start = ((page - 1) / 10) * 10 + 1;
for (int i = start; i <= pageCount && i < start + 10; i++)
{
if (i == page)
sb.Append("<span title='当前第" + i.ToString() + "页'>[" + i.ToString() + "]</span>");
else
sb.Append("<a href='javascript:getPage(" + i + ")' title='第" + i.ToString() + "页'>[" + i.ToString() + "]</a>");
}
if (start > 1)
{
sb.Insert(0, "<a href='javascript:getPage(" + (start - 1).ToString() + ")' title='第" + (start - 1).ToString() + "页'> |<<</a>");
sb.Insert(0, "<a href='javascript:getPage(" + (1).ToString() + ")' title='首页'>||<<</a>");
}
if (start + 10 < pageCount)
{
sb.Append("<a href='javascript:getPage(" + (start + 10).ToString() + ")' title='第" + (start + 10).ToString() + "页'>>>|</a> ");
sb.Append("<a href='javascript:getPage(" + pageCount.ToString() + ")' title='第" + pageCount.ToString() + "页(末页)'>>>||</a>");
}
if (pageCount != 0)
{
sb.Append("  您的位置:" + page + "/" + pageCount + ",总共" + count + "条记录");
}
return sb.ToString();
}

xuyiazl 2008-04-28
  • 打赏
  • 举报
回复


<!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>
<style>
* {
margin:0;padding:0;font-size:12px;font-family:Verdana, Arial, "宋体";}
</style>

<script type="text/javascript">
<!--
function getPage(page){
document.getElementById("tip").style.display="inline";
var key=document.getElementById("title").value;
//创建浏览器兼容的XMLHttpRequest对象
var xmlhttp;
try{
xmlhttp= new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
try{
xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){
try{
xmlhttp= new XMLHttpRequest();
}catch(e){}
}
}
//定义XMLHttpRequest对象的事件处理程序
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
//关闭显示条
document.getElementById("tip").style.display="none";
if(xmlhttp.status==200){
//当加载成功时将内容显示于页面
document.getElementById("content").innerHTML=xmlhttp.responseText;
}else{
//否则弹出错误信息
alert(xmlhttp.status);
}
}
}
//创建一个连接
xmlhttp.open("get","ajaxServers.aspx?page="+page+"&key="+key);
//发送请求
xmlhttp.send(null);
}
-->
</script>
</head>
<body>
<div align="center">
<label for="title">
标题关键字:</label><input type="text" id="title" name="title">
<input type="button" value="查询" onclick="getPage(1)">
<span id="tip" style="display: none">正在加载……</span>
<div id="content" >
</div>
</div>
</body>
</html>




cherry_j 2008-04-28
  • 打赏
  • 举报
回复
谁有现成的例子啊
tyzs2001 2008-04-28
  • 打赏
  • 举报
回复
你可以看看 msdn 里关于 icallbackeventreference,icallbackhandler 的介绍,里面有事例
wang520d 2008-04-28
  • 打赏
  • 举报
回复
VS2005的话用UPDATEPANEL控件
cherry_j 2008-04-28
  • 打赏
  • 举报
回复
谁帮忙啊

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧