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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How can I change div innerHtml (I think that I need js) after MyBut_Click was executed?
See the 2nd paragraph of my answer.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.