简体   繁体   English

从代码返回值到asp.net ajax endrequest

[英]Returning values from code to asp.net ajax endrequest

Basically, I have an asp.net ajax enabled usercontrol, and now I'd like to just return a simple string value, to be used in some js I'm firing after the ajax request is done. 基本上,我有一个asp.net ajax启用的usercontrol,现在我只想返回一个简单的字符串值,用于我在ajax请求完成后触发的一些js。

I've been fiddling around with the endrequest method, which I already set up to use for exception handling, which works fine. 我一直在使用endrequest方法,我已经设置了这个方法用于异常处理,它工作正常。 But is there no way to simply return a string value? 但是没有办法简单地返回字符串值吗?

First I thought I could do this through Response.Write(); 首先,我认为我可以通过Response.Write(); but args.get_response() doesn't like when you do that apparantly - can anyone point me in the right direction? 但args.get_response()并不喜欢当你这么做时 - 有人能指出我正确的方向吗?

Thanks! 谢谢!

You can use the ScriptManager.RegisterDataItem method with the Sys.WebForms.PageRequestManager. 您可以将ScriptManager.RegisterDataItem方法与Sys.WebForms.PageRequestManager一起使用。 Consider the following test page which when you click the button it registers data item with the script manager and at the client side it handles the endrequest and get that data item, 考虑以下测试页面,当您单击按钮时,它会向脚本管理器注册数据项,并在客户端处理endrequest并获取该数据项,

<%@ Page Language="C#" AutoEventWireup="true" Inherits="WebApplication1._Default" %>

<script runat="server">
    protected void btTest_Click(object sender, EventArgs e)
    {
        ScriptManager1.RegisterDataItem(btTest, "Your value to pass to the client");
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="upTest" runat="server">
            <ContentTemplate>
                <asp:Button ID="btTest" runat="server" Text="Click Me" OnClick="btTest_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
<script language="javascript" type="text/javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);

    function endRequestHandler(sender, args) {
        var dataItems = args.get_dataItems()['<%= btTest.ClientID %>'];
        if (dataItems != null)
            alert(dataItems);
    }
</script>
</html>

Maybe I don't understand your problem, but what usercontrol had to do with that additional js calls ? 也许我不明白你的问题,但是usercontrol与其他js调用有什么关系呢? You are just rendering that js from user control ? 你只是从用户控件渲染js?

Anyway, IMO You should use ASP.NET Web Handler (*.ashx) or HttpHandler to return string value and call that handler with that additional js. 无论如何,IMO您应该使用ASP.NET Web Handler(* .ashx)或HttpHandler来返回字符串值并使用其他js调用该处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM