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

发布于 2024-10-12 04:31:30 字数 252 浏览 2 评论 0原文

基本上,我有一个启用了 asp.net ajax 的用户控件,现在我只想返回一个简单的字符串值,以便在 ajax 请求完成后在我触发的一些 js 中使用。

我一直在摆弄 endrequest 方法,我已经设置该方法用于异常处理,效果很好。但是有没有办法简单地返回一个字符串值呢?

首先我想我可以通过 Response.Write(); 来做到这一点但 args.get_response() 不喜欢你这样做 - 谁能指出我正确的方向?

谢谢!

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.

I've been fiddling around with the endrequest method, which I already set up to use for exception handling, which works fine. But is there no way to simply return a string value?

First I thought I could do this through Response.Write(); but args.get_response() doesn't like when you do that apparantly - can anyone point me in the right direction?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泼猴你往哪里跑 2024-10-19 04:31:30

您可以将 ScriptManager.RegisterDataItem 方法与 Sys.WebForms.PageRequestManager 结合使用。考虑以下测试页面,当您单击按钮时,它会向脚本管理器注册数据项,并在客户端处理最终请求并获取该数据项,

<%@ 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>

You can use the ScriptManager.RegisterDataItem method with the 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,

<%@ 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>
那请放手 2024-10-19 04:31:30

也许我不明白你的问题,但是用户控件与额外的 js 调用有什么关系?您只是从用户控件渲染该 js 吗?

无论如何,IMO 您应该使用 ASP.NET Web Handler (*.ashx) 或 HttpHandler 返回字符串值并使用该附加 js 调用该处理程序。

Maybe I don't understand your problem, but what usercontrol had to do with that additional js calls ? You are just rendering that js from user control ?

Anyway, IMO You should use ASP.NET Web Handler (*.ashx) or HttpHandler to return string value and call that handler with that additional js.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文