ASP.NET脚本注册部分网页提交(的UpdatePanel)后 [英] ASP.NET Register Script After Partial Page Postback (UpdatePanel)

查看:113
本文介绍了ASP.NET脚本注册部分网页提交(的UpdatePanel)后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表格(文本框,提交按钮),它被包裹在一个更新面板。

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>

所以,当我按一下按钮,服务器端点击事件被触发(SubmitButton_Click),事情发生到数据库,并在页面重新渲染(异步)。

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".

在换句话说,我需要创建一些JavaScript其数据/参数是基于服务器端的逻辑。

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

我试过这样:

Page.RegisterStartupScript

ScriptManager.RegisterStartupScript

无论是工作(没有任何反应)。

Neither work (nothing happens).

现在我现在我可以使用客户端AJAX libary钩到 .add_pageLoaded 功能(执行客户端脚本,一旦局部更新完成后),但问题是,我从接通按钮点击事件创建的服务器需要的数据。

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.

即:

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
                    }
                });
            });

唯一的黑客我能想到的,此刻是做之上,但调用Web服务,再次让所有的数据然后执行我的脚本。我说黑客,因为我不应该需要做的异步回发,勾入后部分回发事件处理程序,然后再调用服务器只是为了获得那是previously张贴的信息。

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.

似乎是一个共同的问题。不,我无法删除的UpdatePanel(虽然我很想),不想浪费时间争论为什么。

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.

任何非哈克想法?

修改

这是我需要发送到脚本数据澄清:

Clarification on the data i need sent to script:

我在文本框中键入一些文字,点击提交,那么服务器会创建一个数据库记录,并返回一个对象,它有一个像ID,名称,网址,胡说等特性这些值的脚本需要。

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.

所以,如果我是调用从客户端 - code Web服务,才能得到刚刚创建的价值观,我需要做一些黑客(获得最后一个记录修改具有的价值文本框)。不理想,也不是二AJAX调用一个表单提交。 (更新面板回发,然后调用Web服务)。

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).

推荐答案

而不是 add_pageLoaded 的你会想要的 add_endRequest 这里,像这样的:

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

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

的差别在于 endRequest 运行时,任何局部回传回来。

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

这篇关于ASP.NET脚本注册部分网页提交(的UpdatePanel)后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆