问题
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?
回答1:
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 %>','');
回答2:
You can remove the hidden button and call
__doPostBack('upStatus','');
This will cause an asynchronous update for that update panel
来源:https://stackoverflow.com/questions/344881/firing-asp-net-events-from-javascript