asp.net2.0中使用客户端回调实现无刷新应用!

在asp.net1.1中我们可以通过JS调用Web服务来实现无刷新应用,现在asp.net2.0可以用Client Callback的方法实现,这样变得更加方便和容易使用了.客户端回调功能的实现需要两步:
1.为控件或页面实现ICallbackEventHandler接口,该接口有一个方法public string RaiseCallbackEvent(string eventArgument),这个方法的原理是接收客户端发送过来的参数,然后返回一个结果字符串给客户端进行处理.例如:
public partial class ShowReview_ascx : ICallbackEventHandler
{
    public string RaiseCallbackEvent(string eventArgument)
    {
        int reviewId = int.Parse(eventArgument);
        Discussion discussion = AspNetCommerce.DiscussionManager.GetDiscussion(reviewId);
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("<table class='{0}>", "ShowReview_ascx_DisTable");
        foreach (DiscussionItem item in discussion)
        {
            //输出html
        }
        sb.Append("</table>");
        return sb.ToString();
    }
}
该方法接收客户端的传来的一个参数,转换为 reviewId,根据reviewId查询出相关数据以html形式返回给客户端.
2.实现两个客户端脚本,并使用Page.GetCallbackEventReference说明客户端如何处理返回结果.
例如:
Page.GetCallbackEventReference(this, "topicid", "HandleResultFromServer", "context", "HandleErrorResultFromServer")
this表示实现ICallbackEventHandler的控件的实例,
topicid表示客户端传入的字符串参数数
HandleResultFromServer表示处理成功调用后的结果的客户端脚本函数名
context表示回调的启动方法处与处理回调结果的方法之间的传递参数
HandleErrorResultFromServer表示处理失败调用后的结果的客户端脚本函数名
部分客户端脚本如:
    void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack || !this.EnableViewState)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("function loadtree(topicid){");
            sb.Append("var targetImg = document.all('img_'+topicid);");
            sb.Append("var targetDiv = document.all('tr_'+topicid);");
            sb.Append("if (targetDiv.style.display != 'block'){");
            sb.Append("targetDiv.style.display = \"block\";");
            sb.AppendFormat("targetImg.src = \"{0}\";", AspNetCommerce.CommerceContext.GetThemesImagePathForImgTag("ShowReview_ascx/Expand-Open.gif"));
            sb.Append("targetImg.alt = \"点击收回\";");
            sb.Append("var context = topicid;");
            sb.AppendFormat("{0}", Page.GetCallbackEventReference(this, "topicid", "HandleResultFromServer", "context", "HandleErrorResultFromServer"));
            sb.Append("}else{");
            sb.Append("targetDiv.style.display = \"none\";");
            sb.AppendFormat("targetImg.src = \"{0}\";", AspNetCommerce.CommerceContext.GetThemesImagePathForImgTag("ShowReview_ascx/Expand-Closed.gif"));
            sb.Append("targetImg.alt = \"点击查看讨论\";");
            sb.Append("}");
            sb.Append("}");
            sb.Append("function HandleResultFromServer(result,context)");
            sb.Append("{");
            sb.Append("var targetTd = document.all('td_'+context);");
            sb.Append("targetTd.innerHTML = result");
            //sb.Append("alert(result);");
            sb.Append("}");
            sb.Append("function HandleErrorResultFromServer(result,context)");
            sb.Append("{");
            sb.Append("var targetTd = document.all('td_'+context);");
            sb.AppendFormat("targetTd.innerHTML = '<font color=red>{0}</font>'",
                "数据读错误,请重新再试或通知管理员!");
            sb.Append("}");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ClientCallBack", sb.ToString(), true);
        }
    }

你可能感兴趣的:(asp.net)