0

I have button:

html:

 <asp:Button ID="MyBut" runat="server" OnClick="MyBut_Click" CssClass="MyBut" />

C#:

 protected void MyBut_Click(object sender, EventArgs e)
        {....}

JS:

$(document).ready(function () {
    $(".MyBut").click(function () { alert("!"); });
})

Now js function is executed first, after that C# method is executed . Can C# method be first?

Olena
  • 293
  • 3
  • 6
  • 12

2 Answers2

0

So someone clicks a button on a page, the JS fires first and then the C#? This is the only possible sequence of events. The JS on the page that handles the click of the HTML button, fires the event to the web server, and then C# runs on the web server.

DaveDev
  • 41,155
  • 72
  • 223
  • 385
0

Not really. The C# method is called in code-behind (you are using ASP.NET), and that gets called only upon page reload when the server executes the back-end code to form the updated page.

What you could do is to have the server-side code (the C# code) generate client-side JavaScript that would then be executed as the page reloads in the user's browser. That code would then be executed, of course, after the server-side C# code. See Inject Javascript from asp.net code behind files for an example.

Another option would be that you use AJAX to call the server-side code (so instead of code-behind). Then you can determine the sequence yourself. For more on AJAX with ASP.NET, see http://www.asp.net/ajax.

Community
  • 1
  • 1
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76