Hi,
Is there any javascript event which is triggered on postback?
If not, how can I run client side code immediately after or before a page postback?
Many thanks in adavance, Bruno
-
Use AJAX, with an event handler for the
onComplete
. -
The onsubmit event on the form tag
When using jQuery it's like this
$("#yourformtagid").submit(function () { ... }
Daok : JQuery? Not sure the OP want to use a complete framework just to do its task ;)TFD : But every site should have jQuery just in case :-)Sky Sanders : dude, that was not cool. -
You could add the javascript in your page load like this...
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('hello world');", true);
OR
Page.ClientScript.RegisterStartupScript(this.GetType(), "alertScript", "function Hello() { alert('hello world'); }", true);
-
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:
<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:
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.) -
The Page.ClientScript object has a RegisterOnSubmitStatement This fires after any input submits the form. 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.
Bruno : Thanks, I will try this approach!
0 comments:
Post a Comment