Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do something javascript before asp.net anypostback

I wanna do something in javascript before page goes for post back.

How to run a javascript function before any asp.net postback?

$('form').submit(function () {
      alert('hello');
});

It doesn't work... :(

like image 442
Emech Avatar asked Mar 11 '12 12:03

Emech


People also ask

Does ASP.NET need JavaScript?

ASP.NET requires the use of the client-side Javascript, as that's how ASP.NET handles its Events via PostBacks.

Does JavaScript do PostBacks?

How to Raise a Postback from JavaScript? To do this, we need to just call the __doPostBack() function from our javascript code. When the above function is called, it will raise a postback to server.

Can you use .NET with JavaScript?

NET code can do virtually anything when combined with HTML and CSS but JavaScript is still very important and the reason lies in the different domains in which each language operates.


1 Answers

I find the way, in the asp.net forums and it was a some code in codebehind.

Just add this to your Page_Load event handler, changing the javaScript string to what you want to happen.

string scriptKey = "OnSubmitScript";
string javaScript = "alert('RegisterOnSubmitStatement fired');";
this.ClientScript.RegisterOnSubmitStatement(this.GetType(), scriptKey, javaScript);
like image 107
Emech Avatar answered Sep 29 '22 08:09

Emech