views:

39

answers:

5

I am using ASP.NET 3.5.

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

Is this possible? If so how would one do it?

Thanks in advanced!

A: 

Have the JavaScript execute and then call a web service with xmlhttprequest from the javascript

WOPR
Not making using of web services......
Etienne
A: 

There is onClientClick property - check this out http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx

Alex Krupnov
A: 

Of course, you simply add an onClick event all JS code is executed before the postback.

If the code is for validation and you decide you don't want to submit you can return false and it won't post.

<asp:Button OnClientClick="" />
Sruly
+2  A: 

This 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>
Joel Martinez
A: 

Thanks for the answer guys!

To execute a function from code behind one would do this 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
Etienne