代码如下:
Calling Web Service
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
namespace WebPartCollection
{
[Guid("d2b37a6c-650c-4ef4-962c-85297f15b0b5")]
public class AsyWebServiceWebPart : System.Web.UI.WebControls.WebParts.WebPart, ICallbackEventHandler
{
public AsyWebServiceWebPart()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Page.IsPostBack)
{
return;
}
string serverCallJs = Page.ClientScript.GetCallbackEventReference(this, "",
"AsyWebServiceWebPart_Callback", "", "AsyWebServiceWebPart_Callback", true);
string clientCallbackJs = "function AsyWebServiceWebPart_Callback(returnValue,context)"+
"{document.getElementById('"+this.ClientID+"_content').innerHTML=returnValue;}\n";
Page.ClientScript.RegisterStartupScript(this.GetType(), "loadWether", clientCallbackJs + serverCallJs + ";\n", true);
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<div id='" + this.ClientID + "_content' style='text-align:center'>loading…</div>");
}
#region ICallbackEventHandler 成员
string GetCallbackResult()
{
WebXML.WeatherWebService weathreWS = new CodeArt.SharePoint.WebXML.WeatherWebService();
string[] data = weathreWS.getWeatherbyCityName("上海");
return data[5] + "<br/>" + data[6] + "<br/>" + data[7];
}
void RaiseCallbackEvent(string eventArgument)
{
}
#endregion
}
}