微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的第一个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#tipdot.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.gifusing System.Collections;
None.gifusing System.ComponentModel;
None.gifusing System.Data;
None.gifusing System.Drawing;
None.gifusing System.Web;
None.gifusing System.Web.SessionState;
None.gifusing System.Web.UI;
None.gifusing System.Web.UI.WebControls;
None.gifusing System.Web.UI.HtmlControls;
None.gifusing System.Data.sqlClient;
None.gif
None.gif
None.gifnamespace AJAX现实分页无刷新
ExpandedBlockStart.gifContractedBlock.gifdot.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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐