Ajax&Atlas(二)调用Google API示例

前言<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

本示例用于练习使用Atlas实现ajax,所要做的是一个搜索页面,用到类Google 提供的WebService,关于google api 的获取以及注册方面的请参考使用C#调用GoogleApi一文。关于Atlas的获得请参考Ajax&atlas()调用Webservice

 

步骤

l         打开vs2005,使用atlas模板新建一个项目。在Default.aspx的源代码中在第一行的<%@ Page Language="C#" %>中添加属性Title=” Google Search”

l         将下载的the Google Web APIs Developer's Kit中的GoogleSearch.wsdl文件放入本地的服务器上,然后在VS中右键点击资源管理器中的引用,再选择添加Web引用,然后输入GoogleSearch.wsdl文件在本机服务器上的地址http://localhost/GoogleSearch.wsdl,更改Web引用名为GoogleSearch后,点击添加引用后返回。

l         添加对ajax.net的程序集AjaxPro.dll的引用,然后修改web.config文件,在其中添加如下内容:

None.gif < system .web >
None.gif
< httpHandlers >
None.gif            
< add  verb ="POST,GET"  path ="ajaxpro/*.ashx"  type ="AjaxPro.AjaxHandlerFactory, AjaxPro" />
None.gif        
</ httpHandlers >
None.gif
</ system.web >

l         然后新建一个用于存查询结果的类Result.cs,代码如下:

None.gif [Serializable]
None.gif
public   class  Result
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Result() dot.gif{ }
InBlock.gif    
private string title;
InBlock.gif    
private string summer;
InBlock.gif    
private string url;
InBlock.gif    
public string Title
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn this.title; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gifthis.title = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string Summer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn this.summer; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gifthis.summer = value; }
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string Url
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn this.url; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gifthis.url = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public Result(string title, string summer, string url)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.title = title;
InBlock.gif        
this.summer = summer;
InBlock.gif        
this.url = url;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

l         然后新建一个webForm,然后将其代码文件修改为如下内容:

None.gif public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        AjaxPro.Utility.RegisterTypeForAjax(
typeof(_Default));//注册自定义类型
InBlock.gif
        AjaxPro.Utility.RegisterTypeForAjax(typeof(Result));
ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif    [AjaxPro.AjaxMethod]
InBlock.gif    
public Result GetetResult(string s)//AjaxPro能识别的方法都要标记为[AjaxPro.AjaxMethod]
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        localhost.GoogleSearchService service 
= new localhost.GoogleSearchService();
InBlock.gif        localhost.GoogleSearchResult result 
= service.doGoogleSearch("NGYfW7dQFHKshnXPwvctLsaipk03YK2x", s, 010true""true"""""");
InBlock.gif        localhost.ResultElement[] re 
= result.resultElements;
InBlock.gif        Result res 
= new Result(re[0].title,re[0].summary,re[0].URL);       
InBlock.gif        
return res;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif}

l         最后回到webForm的源代码页面,进行客户端的处理。关键代码如下:

None.gif < input  id ="Text3"  type ="text"   />
None.gif    
< input  id ="Button3"  type ="button"  value ="button"  language ="javascript"  onclick ="result(document.getElementById('Text3').value)"   /></ div >
None.gif    
None.gif        
None.gif   
< br  />
None.gif    搜索结果:
< br  />
None.gif    
< hr  />
None.gif    
< table  style ="width: 333px" >
None.gif        
< tr >
None.gif            
< td  id ="Title" >
None.gif            
</ td >
None.gif            
< td >
None.gif            
</ td >
None.gif            
< td >
None.gif            
</ td >
None.gif        
</ tr >
None.gif        
< tr >
None.gif            
< td  colspan ="3"  id ="Url" >
None.gif            
</ td >
None.gif        
</ tr >
None.gif        
< tr >
None.gif            
< td  colspan ="3"  id ="Summer" >
None.gif            
</ td >
None.gif        
</ tr >
None.gif    
</ table >
None.gif    
< span  id ="result" >
None.gif        
None.gif    
</ span >
None.gif    
</ form >
ExpandedBlockStart.gifContractedBlock.gif
< script  type ="text/javascript" > dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
function result(result) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif     
var title=document.getElementById("Title");
InBlock.gif     
var url=document.getElementById("Url");
InBlock.gif     
var summer=document.getElementById("Summer");
InBlock.gif     
var ds=_Default.GetetResult(result).value
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (typeof(ds) == "object")dot.gif
InBlock.giftitle.innerHTML
=ds.Title;
InBlock.gifurl.innerHTML
=ds.Url;
InBlock.gifsummer.innerHTML
=ds.Summer;
InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
None.gif
</ script >

这样基本就完成了,启动后就能看见效果了。

另本人在vs2005下使用magicajax 0.3.0时,按网上说明对配置文件进行了修改,但是不知为什么,总是报未将对象引用设置为对象实例的错,下面是截图,望高手指点。

 

None.gif Unhandled Execution Error 
None.gifObject reference not set to an instance of an object.
None.gif  at MagicAjax.AjaxCallHelper.GetPageHiddenDictionary(Page page, String fieldName)
None.gif  at MagicAjax.AjaxCallHelper.Page_PreRender(Object sender, EventArgs e)
None.gif  at System.Web.UI.Control.OnPreRender(EventArgs e)
None.gif  at System.Web.UI.Control.PreRenderRecursiveInternal()
None.gif  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 0.00196226056663626 0.001046 


 

转载于:https://www.cnblogs.com/zmsx/archive/2006/03/05/343431.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值