views:

442

answers:

2

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?

+3  A: 

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 %>','');
Slace
What I do actually works, but I wanted to bypass the hidden button. What is going on is the drop down list is being rebound to new list data, if per-chance the data has changed, which is what the XMLHTTPRequest response reveals. I don't know if that is clearer. I tend to be overly verbose.
stephenbayer
You can call __doPostBack without arguments but then you don't have event handler fire because the object model doesn't know the source of the event. You get Page_Load, etc but no other event handlers
Slace
+1  A: 

You can remove the hidden button and call

__doPostBack('upStatus','');

This will cause an asynchronous update for that update panel

I do understand that, but how do I get it to call an event handler method?
stephenbayer
I guess I don't need to, I think I'm over complicating things, if i put some logic in the page load event, it should get run when the update panel posts back... then maybe use a hidden field set by the ajax return function, to pass the required information.
stephenbayer
I'm 100% sure what you are trying to do but you can check the script managers IsAsyncPostback property, both client and server side, and do whatever it is you need to do in that instance
Sorry, I meant to say that I am not 100% sure what you are trying to do.