The ASPNET button only executes the client code and does not echo back the page implementation idea

  • 2020-05-24 05:32:11
  • OfStack

The default behavior of Button for asp.net is to execute the server code after the page is sent back. You can also use html's button control input type="button" on the page, but this HTML control is not referenced in the cs file.

asp:Button is available in the cs file. How do you get it to execute only client JavaScript code without sending back to execute server-side code?

First of all, asp:Button's two events OnClientClick and OnClick are introduced. The former OnClientClick is to execute the client JavaScript code, and the latter OnClick is to execute the server code, postback is to execute, and OnClick is to run on the server. OnClientClick should precede OnClick. If asp:Button does not have OnClick, it will be sent back when clicked.

When OnClientClick returns false, OnClick does not execute. This allows one Ajax asynchronous operation to be performed on OnClientClick.
 
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="AjaxDoSave(); return false;" /> 

The Button becomes the client's input, but it can still be referenced in the cs file.

Related articles: