简体   繁体   English

从JavaScript触发ASP.NET事件

[英]firing ASP.NET Events from javascript

I'm doing some straight up asynchronous calls from javascript using the XMLHTTPRequest object. 我正在使用XMLHTTPRequest对象从JavaScript进行一些简单的异步调用。 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. 因此,定时甚至检查是否有更改,如果有更改(通常不会发生),则更新面板会回发,并且下拉列表会根据来自ajax响应文本的各种返回值重新绑定到新数据表。

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? 是否有更好,更清洁的方法来实现此目的,方法是删除隐藏按钮并进行直接的javascript调用,这将导致更新面板异步回发并运行服务器端方法?

Be very careful with UpdatePanels, they can be very heavy if not used properly as I explain here . 请特别注意UpdatePanels,如果使用不正确,它们可能会很沉重,正如我在此处说明的那样。

But the JavaScript for submitting a form is: 但是提交表单的JavaScript是:

__doPostBack('eventTarget','eventArguments');

So in your example you'd have something like: 因此,在您的示例中,您将具有以下内容:

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

You can remove the hidden button and call 您可以删除隐藏的按钮并致电

__doPostBack('upStatus',''); __doPostBack( 'upStatus', '');

This will cause an asynchronous update for that update panel 这将导致该更新面板的异步更新

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

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