字典!AJAX?

今天同事给看了一个比较有意思的网站 www.objectGraph.com 全E文看不懂!不太明白是做什么的,里面有一个 AJAX Dictionary 比较有意思,并且有讲解其机制,自己动手做了一下效果不错呵呵!
下面就是关键代码:

ASP.net page

None.gif <% @Page Language = " C# " %>
None.gif
<% @Import Namespace = " System.Data " %>
None.gif
<% @Import Namespace = " System.Data.SqlClient " %>
None.gif
<% @Import Namespace = " System.Configuration " %>
None.gif
None.gif
< script runat = " server " >
None.gif
None.gif    
public   void  Page_Load( object  sender,EventArgs args)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
string keyword=Request["k"];
InBlock.gif        
if(keyword!=null && keyword.Trim()!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sql="select top 10* from WordList where word like '"+keyword.Trim().Replace("'","''")+"%'";
InBlock.gif            SqlConnection conn
=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
InBlock.gif            conn.Open();
InBlock.gif            DataTable dt
=new DataTable();
InBlock.gif            SqlCommand command
=new SqlCommand(sql,conn);
InBlock.gif            SqlDataAdapter adapter
=new SqlDataAdapter(command);
InBlock.gif            adapter.Fill(dt);
InBlock.gif            conn.Close();
InBlock.gif
InBlock.gif            
foreach(DataRow row in dt.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string meaning=row["Meaning"].ToString();
InBlock.gif                Response.Write(
"<strong>"+row["Word"].ToString()+"</strong> <i>");
InBlock.gif                  Response.Write(
"row["Type"].ToString()+"</i>"+meaning+"<br>");
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif
</ script >
None.gif

XMLHttpRequest object in the HTML page
None.gif < html >
None.gif    
< head >
None.gif        
< script >
None.gif
var  req;
None.gif
None.gif
function  Initialize()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        req
=new ActiveXObject("Msxml2.XMLHTTP");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            req
=new ActiveXObject("Microsoft.XMLHTTP");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch(oc)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            req
=null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if(!req&&typeof XMLHttpRequest!="undefined")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        req
= new
InBlock.gif    XMLHttpRequest();
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}
  function
None.gifSendQuery(key)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif    Initialize(); varurl
="http://www.objectgraph.com/dictionary/dict.aspx?k="+key;
InBlock.gif
InBlock.gif    
if(req!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        req.onreadystatechange 
= Process;
InBlock.gif        req.open(
"GET", url, true);
InBlock.gif        req.send(
null);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
function  Process()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if (req.readyState == 4)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
// only if "OK"
InBlock.gif
            if (req.status == 200)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(req.responseText=="")
InBlock.gif                    HideDiv(
"autocomplete");
InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ShowDiv(
"autocomplete");
InBlock.gif                    document.getElementById(
"autocomplete").innerHTML =req.responseText;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                document.getElementById(
"autocomplete").innerHTML=
InBlock.gif                    
"There was a problem retrieving data:<br>"+req.statusText;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}

None.gif
None.gif
function  ShowDiv(divid)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
if (document.layers) document.layers[divid].visibility="show";
InBlock.gif   
else document.getElementById(divid).style.visibility="visible";
ExpandedBlockEnd.gif}

None.gif
None.gif
function  HideDiv(divid)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
if (document.layers) document.layers[divid].visibility="hide";
InBlock.gif   
else document.getElementById(divid).style.visibility="hidden";
ExpandedBlockEnd.gif}

None.gif
None.gif
function  BodyLoad()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    HideDiv(
"autocomplete");
InBlock.gif    document.form1.keyword.focus();
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
</ script >
None.gif    
</ head >
None.gif    
< body onload = " BodyLoad(); " >
None.gif        
< form name = " form1 " >
None.gif        
< input name = " keyword "  onKeyUp = " SendQuery(this.value) "  style = " WIDTH:500px "  autocomplete = " off " >
None.gif            
< div align = " left "  class = " box "  id = " autocomplete "  style = " WIDTH:500px;BACKGROUND-COLOR:#ccccff " ></ div >
None.gif        
</ form >
None.gif
None.gif    
</ body >
None.gif
</ html >

详细请见: http://www.objectgraph.com/dictionary/how.html

转载于:https://www.cnblogs.com/orin-chan/archive/2006/02/25/337559.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值