The default action of the asp.net Button is to send the page back and execute the server code. You can also use the html button control input type = "button" on the page, but this HTML control cannot be referenced in the cs file.
Asp: the Button is available in the cs file. How can I make it only execute the client JavaScript code without sending back the server code?
First, we will introduce the two events OnClientClick and OnClick of asp: Button. The former OnClientClick is to execute the client JavaScript code, and the latter OnClick is the server code. First, execute postback and then run it on the server. OnClientClick must be prior to OnClick. If there is no OnClick in asp: Button, it will still be sent back when clicked.
When OnClientClick returns false, OnClick will not be executed. In this way, you can execute an Ajax Asynchronous Operation in OnClientClick.
Copy codeThe Code is as follows:
<Asp: Button ID = "btnSave" runat = "server" Text = "Save" OnClientClick = "AjaxDoSave (); return false;"/>
In this way, the Button becomes the input of the client, but can be referenced in the cs file.