ASP.NET Register Script After Partial Page Postback (UpdatePanel)

我是研究僧i 提交于 2019-12-12 07:24:06

问题


I have a simple form (textbox, submit button) which is wrapped in an update panel.

<asp:UpdatePanel ID="ReplyUpdatePanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
       <asp:Textbox id="ReplyTextBox" runat="server"/>
       <asp:Button id="SubmitButton" OnClick="SubmitButton_Click" runat="server"/>
    <ContentTemplate>
</asp:UpdatePanel>

So, when i click the button, the server-side click event is fired (SubmitButton_Click), stuff happens to the db, and the page is re-rendered (asynchronously).

Here's my issue - i need to execute some JavaScript after all the "stuff happens to the db".

In other words, i need to create some JavaScript whose data/parameters are based on server-side logic.

I've tried this:

Page.RegisterStartupScript

and this

ScriptManager.RegisterStartupScript

Neither work (nothing happens).

Now i now i can hook into the .add_pageLoaded function using the client-side AJAX libary (to execute client-side scripts once partial update is complete), but the problem is i need data from the server that is created on the button click event.

Ie:

Sys.Application.add_init(function () {
                Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function (sender, args) {
                    var panels = args.get_panelsUpdated();
                    for (var i = 0; i < panels.length; i++) {
                        // check panels[i].id and do something
                    }
                });
            });

The only "hack" i can think of at the moment is to do the above, but call a web service, getting all the data again then executing my script. I say "hack" because i shouldnt need to do an asynchronous postback, hook into the after-partial-postback event handler then call the server again just to get the info that was previously posted.

Seems like a common problem. And no, i cannot remove the UpdatePanel (even though i would love to), don't want to waste time arguing why.

Any non-hacky ideas?

EDIT

Clarification on the data i need sent to script:

I type some text in the textbox, click submit, then the server creates a database record and returns an object, which has properties like ID, Name, URL, Blah, etc. These are the values that the script requires.

So if i were to call a web service from the client-code, in order to get the values that were just created, i would need to do some hacks (get last record modified that has the value of the textbox). Not ideal, and neither is two AJAX calls for one form post. (update panel postback, then web service call).


回答1:


Instead of add_pageLoaded you'll want add_endRequest here, like this:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, args) {
  //check here...
});

The difference is that endRequest runs when any partial postback comes back.



来源:https://stackoverflow.com/questions/3571173/asp-net-register-script-after-partial-page-postback-updatepanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!