我的第一个AJAX程序

AJAX实现分页无刷新
搜索页:

None.gif<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="AJAX现实分页无刷新.WebForm1" %>
None.gif
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
None.gif
<HTML>
None.gif    
<HEAD>
None.gif        
<title>WebForm1</title>
None.gif        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
None.gif        
<meta name="CODE_LANGUAGE" Content="C#">
None.gif        
<meta name="vs_defaultClientScript" content="JavaScript">
None.gif        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
ExpandedBlockStart.gifContractedBlock.gif        
<style type="text/css">tabledot.gif{font-size=15px;}
ExpandedBlockStart.gifContractedBlock.gif    span#tip
dot.gif{background-color:#f00;color:#fff;padding=2px;margin-left:30px;dispaly:inline;}
None.gif        
</style>
None.gif        
<script type="text/javascript">
None.gif            
//var content = document.getElementById("content");
None.gif
            
None.gif            function getPage(page)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{            
InBlock.gif            var key     
= document.Form1.title.value;
InBlock.gif            
InBlock.gif            tip.style.display 
= "inline";
InBlock.gif            var xmlhttp;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
trydot.gif{
InBlock.gif                xmlhttp 
= new ActiveXObject("Msxml2.xmlhttp");                
InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
catch(e)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
trydot.gif{
InBlock.gif                    xmlhttp 
= new ActiveXObject("Microsoft.xmlhttp");                            
InBlock.gif                    
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
catch(e)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
trydot.gif{
InBlock.gif                            xmlhttp  
= new XMLHttpRequest();
InBlock.gif                            alert(
"no ActiveXObject");
ExpandedSubBlockStart.gifContractedSubBlock.gif                        }
catch(e)dot.gif{}
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif            }
    
ExpandedSubBlockStart.gifContractedSubBlock.gif            xmlhttp.onreadystatechange 
= function()dot.gif{                
InBlock.gif                
if(xmlhttp.readyState ==4)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    tip.style.display
= "none";
InBlock.gif                    
if(xmlhttp.status == 200)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{            
InBlock.gif                        
//alert(xmlhttp.responseText)    ;
InBlock.gif
                        content.innerHTML = xmlhttp.responseText;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        alert(xmlhttp.status);
ExpandedSubBlockEnd.gif                    }
                    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            xmlhttp.open(
"get","show.aspx?page=" + page + "&key = " + key );
InBlock.gif            
//alert("show.aspx?page=" + page + "&key = " + key);
InBlock.gif
            xmlhttp.send(null);
InBlock.gif            
ExpandedBlockEnd.gif        }

None.gif        
</script>
None.gif    
</HEAD>
None.gif    
<body>
None.gif        
<form id="Form1" method="post" runat="server">
None.gif            
<FONT face="宋体">
None.gif                
<TABLE id="Table1" style="WIDTH: 496px; HEIGHT: 75px" cellSpacing="1" cellPadding="1" width="496"
None.gif                    border
="0">
None.gif                    
<TR>
None.gif                        
<TD>
None.gif                            
<P align="center">AJAX实现分页无刷新</P>
None.gif                        
</TD>
None.gif                    
</TR>
None.gif                    
<TR>
None.gif                        
<TD style="HEIGHT: 12px">找查关键字:<INPUT type="text" id="title"><INPUT type="button" value="查找" onclick="getPage(1)"><span id="tip" style="display:none">正在查找dot.gif</span>
None.gif                        
</TD>
None.gif                    
</TR>
None.gif                    
<TR>
None.gif                        
<TD><span id="content"></span></TD>
None.gif                    
</TR>
None.gif                
</TABLE>
None.gif            
</FONT>
None.gif        
</form>
None.gif    
</body>
None.gif
</HTML>
None.gif


 请求处理页

None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Web;
None.gif
using System.Web.SessionState;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.SqlClient;
None.gif
None.gif
None.gif
namespace AJAX现实分页无刷新
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// show 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class show : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int pagesize = 5;
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int page =1;
InBlock.gif    
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                page 
= Convert.ToInt32(Request.QueryString["page"]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch(Exception ee)dot.gif{}
InBlock.gif
InBlock.gif            
string key = Request.QueryString["key"];
InBlock.gif            
//string key = "p";
InBlock.gif
            int pageCount = getPageCount(key);
InBlock.gif
InBlock.gif            System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
InBlock.gif            sb.Append(
"select top " + pagesize.ToString() +"* from products where productName like '%" + key + "%'");
InBlock.gif            
if(page>1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Response.Write("DDDD");
InBlock.gif
                sb.Append("and productid not in (select top " + Convert.ToString(pagesize*(page-1)) + " productID from products where productName like '%" + 
InBlock.gif                    key 
+ "%' order by productid)");
ExpandedSubBlockEnd.gif            }

InBlock.gif            sb.Append(
" order by productid ");
InBlock.gif            
//Response.Write(key);
InBlock.gif            
//邦定datagrid
InBlock.gif
            DataGrid dg = new DataGrid();
InBlock.gif            dg.AutoGenerateColumns 
= false;
InBlock.gif            dg.AllowPaging 
= false;
InBlock.gif            HyperLinkColumn hlc ;
InBlock.gif            BoundColumn bc;            
InBlock.gif
InBlock.gif            hlc 
= new HyperLinkColumn();
InBlock.gif            hlc.DataTextField 
= "productName";
InBlock.gif            hlc.DataNavigateUrlField 
= "productID";
InBlock.gif            hlc.DataNavigateUrlFormatString 
= "xxx.aspx?id={0}";
InBlock.gif            hlc.HeaderText 
= "产品名字";
InBlock.gif            hlc.ItemStyle.Width 
= Unit.Pixel(400);
InBlock.gif            hlc.Target 
= "_blank";
InBlock.gif            dg.Columns.Add(hlc);
InBlock.gif
InBlock.gif            bc 
= new BoundColumn();
InBlock.gif            bc.HeaderText 
= "产品ID";
InBlock.gif            bc.DataField 
= "productID";
InBlock.gif            bc.ItemStyle.Width 
= Unit.Pixel(200);
InBlock.gif            dg.Columns.Add(bc);
InBlock.gif
InBlock.gif            dg.DataSource 
= getData(sb.ToString());
InBlock.gif            dg.DataBind();
InBlock.gif            
InBlock.gif            Response.Write(getStringByControl(dg) 
+ "<div id='pager'>" + this.GenPager(page , pagesize , pageCount)+ "</div>" );
InBlock.gif            
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string GenPager(int page , int pagesize , int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
InBlock.gif            
int pageCount = (int)Math.Ceiling((double)count/pagesize);
InBlock.gif            
int start = ((page-1)/10)*10+1;
InBlock.gif            
for(int i = start;i<=pageCount && i<start+10;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(i == page) dot.gif{sb.Append("<span title='当前页'>[" + i.ToString() + "]</span>");}
ExpandedSubBlockStart.gifContractedSubBlock.gif                
elsedot.gif{sb.Append("<a href='javascript:getPage(" + i.ToString() +")' title='第" + i.ToString() + "页'>[" + i.ToString() + "]</a>");}
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(start >1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Insert(
0,"<a href='javascript:getPage(1)' title ='首页'>||首页</a>");
InBlock.gif                sb.Insert(
0,"<a href='javascript:getPage(" + Convert.ToString(start -1+ " )' title='第" + Convert.ToString(start-1+ "页'>&lt;&lt;</a>" );
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
if(start +10 <pageCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(
"<a href='javascript:getPage(" + Convert.ToString(start + 10+ ")' title ='第" + Convert.ToString(start +10+ "页'>&gt;&gt;</a>");
InBlock.gif                sb.Append(
"<a href='javascript:getPage(" + pageCount.ToString() + ")' title ='(末页)'>||末页</a>");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return sb.ToString();
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string getStringByControl(System.Web.UI.Control c)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
InBlock.gif            System.IO.StringWriter write 
= new System.IO.StringWriter(sb);
InBlock.gif            System.Web.UI.HtmlTextWriter htw 
= new HtmlTextWriter(write);
InBlock.gif            c.RenderControl(htw);
InBlock.gif            
return sb.ToString();            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private DataTable getData(string sqlText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlDataAdapter da 
= new SqlDataAdapter(sqlText , "server=.;uid=sa;pwd=;database=northwind;");
InBlock.gif            DataTable dt 
= new DataTable();
InBlock.gif            da.Fill(dt);
InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private int getPageCount(string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection conn 
= new SqlConnection("server=.;uid=sa;pwd=;database=northwind;");
InBlock.gif            SqlCommand cmd 
= new SqlCommand("select count(*) from  products where productName like '%" + key + "%'" , conn);
InBlock.gif            conn.Open();
InBlock.gif            
return Convert.ToInt32(cmd.ExecuteScalar());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

终于实现了我的第一个AJAX程序...........

转载于:https://www.cnblogs.com/zzj-46000452/archive/2006/08/14/zzj-46000452.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值