【问题标题】:Can a void AjaxManager_AjaxRequest send JSON data back to the browser?void AjaxManager_AjaxRequest 可以将 JSON 数据发送回浏览器吗?
【发布时间】:2012-08-23 19:35:40
【问题描述】:

AjaxManager_AjaxRequest 可以只修改其 UpdatePanel 中的控件,还是也可以在响应中发回 JSON 数据。理想情况下只是 JSON 数据。

所以在我的 ascx 中有

protected void Page_Load(object sender, EventArgs e)
{
    RadAjaxManager radAjaxManager = RadAjaxManager.GetCurrent(Page);

    if (radAjaxManager != null)
    {
        radAjaxManager.AjaxRequest += AjaxManager_AjaxRequest;
    }

}

private void AjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
   // Somehow tweak it so response is just a JSON object.
}

我刚刚继承了一些遗留代码,这是我实现目标的最快方式,无需创建网络服务来正确执行此操作。

有没有办法按照我的要求去做?

【问题讨论】:

    标签: c# asp.net ajax json radajaxmanager


    【解决方案1】:

    是的,这可能。实际上存在两种可用的解决方案。第一个是使用 jQuery ajax 方法发送 ajax 请求(您可以使用 $telerik.$ 访问 jQuery 功能)。这种方法的唯一不足是目标服务器端方法必须是静态的,并且您不能访问页面的 ViewState 以及服务器控件的属性值。另一种方法是在AjaxManager_AjaxRequest 方法中使用ScriptManager 的RegisterDataItem 方法将JSON 序列化对象传递回客户端,并在客户端上Sys.WebForms.PageRequestManager 的endRequest 事件处理程序中获取它。

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script type="text/javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
    
        function endRequestHandler(sender, args) {
            var dataItems = args.get_dataItems();
            if (dataItems && dataItems["<%= RadAjaxManager1.UniqueID %>"]) {
                alert(dataItems["<%= RadAjaxManager1.UniqueID %>"].Response);
            }
        }
    </script>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" />
    
    
    void AjaxManager_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
    {
        ScriptManager1.RegisterDataItem(radAjaxManager , new JavaScriptSerializer().Serialize(new { Response = "Hello, RadAjaxManager!" }), true);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-17
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 2011-08-07
      • 2014-10-31
      • 2014-09-01
      相关资源
      最近更新 更多