简体   繁体   English

页面回发上的Javascript事件

[英]Javascript event on page postback

Is there any javascript event which is triggered on postback? 是否有任何在回发时触发的javascript事件?

If not, how can I run client side code immediately after or before a page postback? 如果没有,我如何在页面回发之后或之前立即运行客户端代码?

I believe what you are looking for is the Sys.WebForms.PageRequestManager beginRequest Event 我相信你要找的是Sys.WebForms.PageRequestManager beginRequest事件

Excerpt: 摘抄:

The beginRequest event is raised before the processing of an asynchronous postback starts and the postback is sent to the server. 在开始处理异步回发之前引发beginRequest事件,并将回发发送到服务器。 You can use this event to call custom script to set a request header or to start an animation that notifies the user that the postback is being processed. 您可以使用此事件来调用自定义脚本来设置请求标头或启动动画,以通知用户正在处理回发。

Code Sample: (From the link) 代码示例:(来自链接)

<script type="text/javascript" language="javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    function BeginRequestHandler(sender, args)
    {
        var elem = args.get_postBackElement();
        ActivateAlertDiv('visible', 'AlertDiv', elem.value + ' processing...');
    }
    function EndRequestHandler(sender, args)
    {
        ActivateAlertDiv('hidden', 'AlertDiv', '');
    }
    function ActivateAlertDiv(visstring, elem, msg)
    {
        var adiv = $get(elem);
        adiv.style.visibility = visstring;
        adiv.innerHTML = msg;                     
    }
</script>

I hope that helps. 我希望有所帮助。 The PageRequestManager class seems to be little known about and little utilized. PageRequestManager类似乎鲜为人知并且很少使用。

You could add the javascript in your page load like this... 您可以像这样在页面加载中添加javascript ...

Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", 
    "alert('hello world');", true);

OR 要么

Page.ClientScript.RegisterStartupScript(this.GetType(), "alertScript",
   "function Hello() { alert('hello world'); }", true);

i don't know if you still need, but take a look at this: 我不知道你是否仍然需要,但看看这个:

Run javascript function after Postback 在Postback之后运行javascript函数

i solve my problem using this: 我使用这个来解决我的问题:

<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function (s, e) {
    alert('Postback!');
});

there are a lot of options too, like 还有很多选择,比如

$('#id').live('change', function (){});

$(document).ready(function () {});
ClientScriptManager.RegisterStartupScript(this.GetType(), "AKey", "MyFunction();", true);

and keep going. 并继续前进。 depends on what you need. 取决于你需要什么。

Regards =) 问候=)

The Page.ClientScript object has a RegisterOnSubmitStatement This fires after any input submits the form. Page.ClientScript对象具有RegisterOnSubmitStatement在任何输入提交表单后触发。 This may or may not be what you're looking for, but I've used it for notifying the user of unsaved changes in editable forms. 这可能是您正在寻找的,也可能不是,但我已经用它来通知用户可编辑表单中未保存的更改。

The advantage to using this over RegisterStartupScript is that with RegisterOnSubmitStatement, if a user navigates away and back using the browser, whatever script you've injected using RegisterStartupScript could possibly fire again, whereas RegisterOnSubmitStatement will only run if the user has submitted the form. 使用RegisterStartupScript的优势在于,使用RegisterOnSubmitStatement,如果用户使用浏览器导航和返回,则使用RegisterStartupScript注入的任何脚本都可能再次触发,而RegisterOnSubmitStatement仅在用户提交表单时才会运行。

使用AJAX,以及onComplete的事件处理程序。

The onsubmit event on the form tag 表单标记上的onsubmit事件

When using jQuery it's like this 使用jQuery时就像这样

$("#yourformtagid").submit(function () {
 ...
}

There isn't a javascript event triggered when a page loads after a postback, but you can add javascript to your html template (.aspx file) and only run it if the page was posted, like this: 在回发后加载页面时没有触发javascript事件,但是您可以将javascript添加到您的html模板(.aspx文件),并且仅在页面发布时运行它,如下所示:

<script type='text/javascript'>
    var isPostBack = '<%= this.IsPostBack%>' == 'True';
    if (isPostBack) {
        alert('It's a PostBack!');
    }
</script>

If you want to customize the javascript to run only under particular conditions (not just any postback), you can create a page-level variable (protected or public) in your page's class and do something similar: 如果您想自定义javascript只在特定条件下运行(而不仅仅是任何回发),您可以在页面的类中创建页面级变量(受保护或公共)并执行类似的操作:

var userClickedSubmit = '<%= this.UserClickedSubmit%>' == 'True';
if (userClickedSubmit) {
    // Do something in javascript
}

(Nothing against ClientScript.RegisterStartupScript , which is fine - sometimes you want to keep your javascript in the page template, sometimes you want to keep it in your page class.) (对于ClientScript.RegisterStartupScript没有任何意义,这很好 - 有时你想在页面模板中保留你的javascript,有时候你想把它保存在你的页面类中。)

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

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