How do you run JavaScript after a Form has been successfully posted and returned?
<% Html.BeginForm(); %>
....
<% Html.EndForm(); %>
How do you run JavaScript after a Form has been successfully posted and returned?
<% Html.BeginForm(); %>
....
<% Html.EndForm(); %>
In a traditional (non-ajax) POST you are getting a new page back, so javascript will be run the same way you would on any other page.
If you are asking how do you POST and reuse the same view as a GET and run javascript only on the POST, than the short answer is you don't. You will want to use a different view for the POST that has the additional javascript that needs to be run. You can reuse most of your view code through partial page views.
If you are posting back via jQuery there is a Success attribute in which you can put jScript to run a function.
$.ajax({
type: "POST",
url: "MyPage.aspx/jQueryDoStuff",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(msg) {
DoPostAjaxStuffHere(msg);
},
error: function(XMLHttpRequest, textStatus, errorThrown) { }
});
Also note the Error attribute which can be handy also.
I post most of my forms asynchronously, using JQuery's ajax function. When the page I posted to processes the post, it returns some JSON, which my original posting javascript reads (as a success callback), and then makes a decision what to do. This is the way most web apps are being written these days, you're alternative is to redirect to some page that has an embedded script block.