Run Javascript Function Before Postback

My ASPX page has a button with this code:

OnClick="save_Click" 

Is it possible to execute Javascript before postback, and if the result is true , save_click and go to save_click method?

+8
source share
4 answers

There is a property called "OnClientClick" . Here you can specify a function that will check (I assume), or just run regular javascript.

If your data is invalid, you can simply return false; from the method. This should cancel your postback

+7
source share

you must use a very well known method: return confirm ('bla bla bla')

adding this fragment to the onclick attribute of a button in the prerender method of a page or button, on the server side ...

+1
source share

http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx

 <%@ Page Language="C#" %> <script runat="server"> protected void Button1_Click(Object sender, EventArgs e) { Label1.Text = "Server click handler called."; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <body> <form id="form1" runat="server"> <asp:Button ID="Button1" Runat="server" OnClick="Button1_Click" OnClientClick="return confirm('Ready to submit.');" Text="Test Client Click" /> <br /> <asp:Label ID="Label1" Runat="server" text="" /> </form> </body> </html> 

Possible duplicate: Running ClientSide before ServerSide in ASP.NET

+1
source share

I changed the definition of the __doPostback function to do the following:

 <script type="text/javascript"> var originalDoPostback = __doPostBack; __doPostBack = function (p1, p2) { doSomethingCustomHere(); originalDoPostback(p1, p2); }; </script> 
+1
source share

All Articles