从 javascript 触发 ASP.NET 事件

发布于 2024-07-10 01:06:49 字数 1016 浏览 4 评论 0原文

我正在使用 XMLHTTPRequest 对象从 javascript 进行一些直接异步调用。 成功后,使用某些返回值,我想在更新面板上执行异步回发并运行一些服务器端方法。 这是我现在如何实现它的:

<script language="javascript">
      function AjaxCallback_Success(objAjax) {
        if (objAjax.responseText == "refresh") {
                document.getElementById('<%= btnHidden.ClientID %>').click();
         }
      }
    </script>
    <asp:UpdatePanel ID="upStatus" runat="server">
    <ContentTemplate>
<asp:Button ID="btnHidden" runat="server" style="display: none;" OnClick="SomeMethod" />
    <asp:DropDownList ID="ddlStatus" field="Orders_Status" parent="Orders" runat="server">
    </asp:DropDownList>
    </ContentTemplate>
    </asp:UpdatePanel>

这与工作流程有关。 如果在您处理订单时,有人为其开具发票,则状态下拉列表中的可用选项实际上会发生变化。 因此,定时甚至会检查更改,如果发生更改(通常不会发生),更新面板会发回,并且下拉列表会根据 ajax 响应文本的各种返回值重新绑定到新数据表。

我的原始代码实际上比这复杂得多,但我已经进行了足够的抽象以使我的概念更加清晰。 有没有更好、更干净的方法来做到这一点,通过删除隐藏按钮并进行直接的 javascript 调用,这将导致更新面板异步回发并运行服务器端方法?

I'm doing some straight up asynchronous calls from javascript using the XMLHTTPRequest object. On success, with certain return values, I would like to do an asynchonous post back on an update panel and run some server side methods. This is about how I'm implementing it now:

<script language="javascript">
      function AjaxCallback_Success(objAjax) {
        if (objAjax.responseText == "refresh") {
                document.getElementById('<%= btnHidden.ClientID %>').click();
         }
      }
    </script>
    <asp:UpdatePanel ID="upStatus" runat="server">
    <ContentTemplate>
<asp:Button ID="btnHidden" runat="server" style="display: none;" OnClick="SomeMethod" />
    <asp:DropDownList ID="ddlStatus" field="Orders_Status" parent="Orders" runat="server">
    </asp:DropDownList>
    </ContentTemplate>
    </asp:UpdatePanel>

This has to do with work flow. If while you are working on an order, someone invoices it, then the options available in the status drop down actually changes. So a timed even checks for changes and if there is a change, which wouldn't normally happen, the update panel posts back and the drop down list gets re-bound to a new data table based on various return values from the ajax response text.

My original code is actually much more complicated than this, but I've abstracted just enough to make my concept clearer. Is there a better, cleaner way to do this by dropping the hidden button and making a straight javascript call that will cause an update panel to asynchonously postback and run a server side method?

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

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

发布评论

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

评论(2

瑾夏年华 2024-07-17 01:06:49

使用 UpdatePanel 时要非常小心,如果使用不当,它们可能会非常重,正如我所解释的 在这里

但是提交表单的 JavaScript 是:

__doPostBack('eventTarget','eventArguments');

所以在你的例子中你会有类似的东西:

__doPostBack('<%= btnHidden.ClientID %>','');

Be very careful with UpdatePanels, they can be very heavy if not used properly as I explain here.

But the JavaScript for submitting a form is:

__doPostBack('eventTarget','eventArguments');

So in your example you'd have something like:

__doPostBack('<%= btnHidden.ClientID %>','');
御弟哥哥 2024-07-17 01:06:49

您可以删除隐藏按钮并调用

__doPostBack('upStatus','');

这将导致该更新面板的异步更新

You can remove the hidden button and call

__doPostBack('upStatus','');

This will cause an asynchronous update for that update panel

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