使用ajax.net实现datagrid的客户端分页
来源:互联网 发布:大数据思维与决策equp 编辑:程序博客网 时间:2024/06/05 15:16
使用ajax.net实现datagrid的客户端分页
其实就是使用RenderControl 方法,摘自一个老外的blog
GenericAjaxWS.asmx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;
namespace GenricAjaxWS
{
[WebService(Namespace="http://localhost/GenricAjaxWS/")]
public class GenricAjaxWS : System.Web.Services.WebService
{
SqlConnection con;
SqlDataAdapter da;
SqlCommand cmd;
DataSet ds;
public GenricAjaxWS()
{
InitializeComponent();
con= new SqlConnection(ConfigurationSettings.AppSettings["Connect"]);
da=new SqlDataAdapter("",con);
cmd=new SqlCommand("",con);
ds=new DataSet("TahaSchema");
}
Component Designer generated code#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
/**//// <summary>
/// this function accepts TSql statment and returns dataset
/// </summary>
[WebMethod]
public string getGrid(string sqlStr,int pageIndex)
{
da.SelectCommand.CommandText=sqlStr;
da=new SqlDataAdapter(sqlStr,con);
da.Fill(ds,"myTable");
DataGrid dataGrid = new DataGrid();
dataGrid.AutoGenerateColumns = true;
dataGrid.DataSource = ds.Tables["myTable"].DefaultView;
dataGrid.AllowPaging = true;
dataGrid.PageSize = 4;
dataGrid.PagerStyle.Visible = false;
dataGrid.CurrentPageIndex = pageIndex;
try
{
dataGrid.DataBind();
}
catch(Exception)
{
}
StringWriter wr = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(wr);
dataGrid.RenderControl(writer);
string gridHtml = wr.ToString();
wr.Close();
writer.Close();
DropDownList ddl_Pager = new DropDownList();
ddl_Pager.Attributes.Add("onchange","goToPage(this.value)");
string pager="";
for(int i=0;i<dataGrid.PageCount;i++)
{
ListItem lItem = new ListItem(i.ToString(),i.ToString());
ddl_Pager.Items.Add(lItem);
if(i==pageIndex)
{
pager += "[background-color:#ffdd11;width" +
":20px;align:center/"><a href=/"#/" onclick" +
"=/"goToPage('"+i+"')/">"+i+"</a>]";
}
else
{
pager += "[width:20px;align:center/">" +
"<a href=/"#/" onclick=/"goToPage" +
"('"+i+"')/" >"+i+"</a>]";
}
}
ddl_Pager.SelectedIndex = pageIndex;
wr = new StringWriter();
writer = new HtmlTextWriter(wr);
ddl_Pager.RenderControl(writer);
string pagerHtml = "<input type='button'" +
" value='<' onclick='goToPrevPage()'>";
pagerHtml += wr.ToString();
pagerHtml += "<input type='button' value='>'" +
" onclick='this.disabled=goToNextPage()'>";
wr.Close();
writer.Close();
return pager+pagerHtml+"<br>"+gridHtml;
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;
namespace GenricAjaxWS
{
[WebService(Namespace="http://localhost/GenricAjaxWS/")]
public class GenricAjaxWS : System.Web.Services.WebService
{
SqlConnection con;
SqlDataAdapter da;
SqlCommand cmd;
DataSet ds;
public GenricAjaxWS()
{
InitializeComponent();
con= new SqlConnection(ConfigurationSettings.AppSettings["Connect"]);
da=new SqlDataAdapter("",con);
cmd=new SqlCommand("",con);
ds=new DataSet("TahaSchema");
}
Component Designer generated code#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
/**//// <summary>
/// this function accepts TSql statment and returns dataset
/// </summary>
[WebMethod]
public string getGrid(string sqlStr,int pageIndex)
{
da.SelectCommand.CommandText=sqlStr;
da=new SqlDataAdapter(sqlStr,con);
da.Fill(ds,"myTable");
DataGrid dataGrid = new DataGrid();
dataGrid.AutoGenerateColumns = true;
dataGrid.DataSource = ds.Tables["myTable"].DefaultView;
dataGrid.AllowPaging = true;
dataGrid.PageSize = 4;
dataGrid.PagerStyle.Visible = false;
dataGrid.CurrentPageIndex = pageIndex;
try
{
dataGrid.DataBind();
}
catch(Exception)
{
}
StringWriter wr = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(wr);
dataGrid.RenderControl(writer);
string gridHtml = wr.ToString();
wr.Close();
writer.Close();
DropDownList ddl_Pager = new DropDownList();
ddl_Pager.Attributes.Add("onchange","goToPage(this.value)");
string pager="";
for(int i=0;i<dataGrid.PageCount;i++)
{
ListItem lItem = new ListItem(i.ToString(),i.ToString());
ddl_Pager.Items.Add(lItem);
if(i==pageIndex)
{
pager += "[background-color:#ffdd11;width" +
":20px;align:center/"><a href=/"#/" onclick" +
"=/"goToPage('"+i+"')/">"+i+"</a>]";
}
else
{
pager += "[width:20px;align:center/">" +
"<a href=/"#/" onclick=/"goToPage" +
"('"+i+"')/" >"+i+"</a>]";
}
}
ddl_Pager.SelectedIndex = pageIndex;
wr = new StringWriter();
writer = new HtmlTextWriter(wr);
ddl_Pager.RenderControl(writer);
string pagerHtml = "<input type='button'" +
" value='<' onclick='goToPrevPage()'>";
pagerHtml += wr.ToString();
pagerHtml += "<input type='button' value='>'" +
" onclick='this.disabled=goToNextPage()'>";
wr.Close();
writer.Close();
return pager+pagerHtml+"<br>"+gridHtml;
}
}
}
AjaxFuncs.js
//Variable Declarations
/////////////////////////////////////////////////////////////////
var xmlhttp;
/////////////////////////////////////////////////////////////////
//fillGrid
//This Function Takes three parameters
//first parameter is the id of a DIV tag to which you want
//to po***te the Grid
//Second Paramaeter is the Sql String
//Third Parameter is the selected page index
/////////////////////////////////////////////////////////////////
var ph;
var fillGrid_Str_SQL="";
var currentPageIndex ;
function fillGrid(myPH,str_Sql,pageIndex){
ph = window.do***ent.getElementById(myPH);
fillGrid_Str_SQL = str_Sql;
currentPageIndex = pageIndex;
var url = "http://localhost/GenricAjaxWS/GenricAjaxWS" +
".asmx/getGrid?sqlStr="+str_Sql+
"&pageIndex="+pageIndex;
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=fillGrid_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
//code for IE
else if (window.ActiveXObject)
{
try
{
xmlhttp= new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
if(xmlhttp)
{
try
{
xmlhttp.onreadystatechange=fillGrid_Change;
xmlhttp.open("GET",url,false);
xmlhttp.send();
}
catch(e){}
}
}
}
/////////////////////////////////////////////////////////////
//fillGrid_Change
/////////////////////////////////////////////////////////////
function fillGrid_Change()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
//var rows=xmlhttp.responseXML.
// selectNodes(".//TahaSchema//TahaTable");
var row = xmlhttp.responseXML.selectNodes(".//");
ph.innerHTML = row[1].text;
}
}
function goToPage(pageIndex){
fillGrid(ph.id,fillGrid_Str_SQL,pageIndex)
}
function goToNextPage(){
try{
fillGrid(ph.id,fillGrid_Str_SQL,
p***Int(currentPageIndex)+1);
return false;
}
catch(e){
return true;
}
}
function goToPrevPage(){
fillGrid(ph.id,fillGrid_Str_SQL,
p***Int(currentPageIndex)-1)
}
/////////////////////////////////////////////////////////////////
//fillGrid
//This Function Takes three parameters
//first parameter is the id of a DIV tag to which you want
//to po***te the Grid
//Second Paramaeter is the Sql String
//Third Parameter is the selected page index
/////////////////////////////////////////////////////////////////
var ph;
var fillGrid_Str_SQL="";
var currentPageIndex ;
function fillGrid(myPH,str_Sql,pageIndex){
ph = window.do***ent.getElementById(myPH);
fillGrid_Str_SQL = str_Sql;
currentPageIndex = pageIndex;
var url = "http://localhost/GenricAjaxWS/GenricAjaxWS" +
".asmx/getGrid?sqlStr="+str_Sql+
"&pageIndex="+pageIndex;
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=fillGrid_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
//code for IE
else if (window.ActiveXObject)
{
try
{
xmlhttp= new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
if(xmlhttp)
{
try
{
xmlhttp.onreadystatechange=fillGrid_Change;
xmlhttp.open("GET",url,false);
xmlhttp.send();
}
catch(e){}
}
}
}
/////////////////////////////////////////////////////////////
//fillGrid_Change
/////////////////////////////////////////////////////////////
function fillGrid_Change()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
//var rows=xmlhttp.responseXML.
// selectNodes(".//TahaSchema//TahaTable");
var row = xmlhttp.responseXML.selectNodes(".//");
ph.innerHTML = row[1].text;
}
}
function goToPage(pageIndex){
fillGrid(ph.id,fillGrid_Str_SQL,pageIndex)
}
function goToNextPage(){
try{
fillGrid(ph.id,fillGrid_Str_SQL,
p***Int(currentPageIndex)+1);
return false;
}
catch(e){
return true;
}
}
function goToPrevPage(){
fillGrid(ph.id,fillGrid_Str_SQL,
p***Int(currentPageIndex)-1)
}
Now add a page AjaxGrid.htmlto the project.
AjaxGrid.html
<html>
<head>
<title>AjaxGrid</title>
<script language="javascript"
type="text/javascript" src="ajaxFuncs.js"></script>
</head>
<body onload="fillGrid('myPH','select * from sales',1)">
<form id="Form1" method="post" runat="server">
<div id="myPH" ></div>
</form>
</body>
</html>
<head>
<title>AjaxGrid</title>
<script language="javascript"
type="text/javascript" src="ajaxFuncs.js"></script>
</head>
<body onload="fillGrid('myPH','select * from sales',1)">
<form id="Form1" method="post" runat="server">
<div id="myPH" ></div>
</form>
</body>
</html>
- 使用ajax.net实现datagrid的客户端分页
- 使用ajax.net实现datagrid的客户端分页
- ASP.NET使用EasyUI-DataGrid + ashx + JQuery Ajax:实现数据的增删查改,查询和分页!
- .NET中使用datagrid实现的简单分页效果
- .NET中使用datagrid实现的简单分页效果
- .NET中使用datagrid实现的简单分页效果
- .NET中使用datagrid实现的简单分页效果
- ASP.NET中使用DataGrid同时实现分页与排序
- datagrid的分页实现
- ASP.NET+JQuery实现AJAX的分页
- 利用Ajax实现DataGrid无刷新分页
- 利用Ajax实现DataGrid无刷新分页
- 利用Ajax实现DataGrid无刷新分页
- ASP.NET中自定义DataGrid分页设置的实现
- 使用客户端行为来丰富 ASP.NET 的 DataGrid
- 使用客户端行为来丰富 ASP.NET 的 DataGrid
- 使用客户端行为来丰富 ASP.NET 的 DataGrid
- 使用 ASP.NET Atlas PageNavigator控件实现客户端分页导航
- C#写的将阿拉伯数字转换成中文大写金额代码
- WRITE,ALV与FORM不同的输出方式
- Acegy + Spring 2.0 and Maven[摘]
- SQL语言基本语句介绍
- 当选择关键字不是MSEG的索引关键字时,用EKBE代替MSEG可以提高查询速度N倍!
- 使用ajax.net实现datagrid的客户端分页
- Overview of English study method
- WebWork入门教程--创建你第一个WebWork action
- 基本多文种平面--unicode编码
- 成员指针
- The Venture Capital Aptitude Test (翻译)
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 因主机名更改造成oracle控制台登录错误:ora-12545,ora-12541
- 使用DataGrid的两个小技巧(1)