Running ClientSide before ServerSide in ASP.NET

I am using ASP.NET 3.5.

When the user clicks on say btnSubmit, I want to execute some JavaScript code first, and then execute some C # / VB.NET code.

Is it possible? If so, how to do it?

Thanks in advance!

0
javascript c # code-behind
source share
5 answers

It is very simple:

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> 
+2
source share

Ask to execute javascript and then call the web service using xmlhttprequest from javascript

0
source share
0
source share

Of course, you just add the onClick event, all JS codes are executed before the postback.

If the code is intended for verification, and you decide that you do not want to submit, you can return false and it will not publish.

 <asp:Button OnClientClick="" /> 
0
source share

Thanks for the answer guys!

To execute a function from the code behind you would do it in VB.NET

  Protected Sub btnSubmit_Click(blah blah) Handles btnSubmit.Click ClientScript.RegisterStartupScript(Me.GetType(), "hiya", "Message()", True) lblLabel.Text = "Hello my name is Etienne!" End Sub 
0
source share

All Articles