前言
本示例用于练习使用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文件,在其中添加如下内容:
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro"/>
</httpHandlers>
</system.web>
l 然后新建一个用于存查询结果的类Result.cs,代码如下:
[Serializable]
public class Result
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Result()
{ }
private string title;
private string summer;
private string url;
public string Title
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this.title; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this.title = value; }
}
public string Summer
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this.summer; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this.summer = value; }
![](/Images/OutliningIndicators/InBlock.gif)
}
public string Url
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this.url; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this.url = value; }
}
public Result(string title, string summer, string url)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.title = title;
this.summer = summer;
this.url = url;
}
}
![](/Images/OutliningIndicators/None.gif)
l 然后新建一个webForm,然后将其代码文件修改为如下内容:
public partial class _Default : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));//注册自定义类型
AjaxPro.Utility.RegisterTypeForAjax(typeof(Result));
}
[AjaxPro.AjaxMethod]
public Result GetetResult(string s)//AjaxPro能识别的方法都要标记为[AjaxPro.AjaxMethod]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
localhost.GoogleSearchService service = new localhost.GoogleSearchService();
localhost.GoogleSearchResult result = service.doGoogleSearch("NGYfW7dQFHKshnXPwvctLsaipk03YK2x", s, 0, 10, true, "", true, "", "", "");
localhost.ResultElement[] re = result.resultElements;
Result res = new Result(re[0].title,re[0].summary,re[0].URL);
return res;
}
}
l 最后回到webForm的源代码页面,进行客户端的处理。关键代码如下:
<input id="Text3" type="text" />
<input id="Button3" type="button" value="button" language="javascript" onclick="result(document.getElementById('Text3').value)" /></div>
<br />
搜索结果:<br />
<hr />
<table style="width: 333px">
<tr>
<td id="Title">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="3" id="Url">
</td>
</tr>
<tr>
<td colspan="3" id="Summer">
</td>
</tr>
</table>
<span id="result">
</span>
</form>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script type="text/javascript">![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
function result(result)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var title=document.getElementById("Title");
var url=document.getElementById("Url");
var summer=document.getElementById("Summer");
var ds=_Default.GetetResult(result).value
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (typeof(ds) == "object")
{
title.innerHTML=ds.Title;
url.innerHTML=ds.Url;
summer.innerHTML=ds.Summer;
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
这样基本就完成了,启动后就能看见效果了。
另本人在vs2005下使用magicajax 0.3.0时,按网上说明对配置文件进行了修改,但是不知为什么,总是报未将对象引用设置为对象实例的错,下面是截图,望高手指点。
Unhandled Execution Error
Object reference not set to an instance of an object.
at MagicAjax.AjaxCallHelper.GetPageHiddenDictionary(Page page, String fieldName)
at MagicAjax.AjaxCallHelper.Page_PreRender(Object sender, EventArgs e)
at System.Web.UI.Control.OnPreRender(EventArgs e)
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 0.00196226056663626 0.001046