1. For Web controls only, the OnClick event executes the code in the client.
<%@ Page Language="C #"AutoEventWireup="true"codebehind="Click.aspx.cs"Inherits="Onclickandonserverclick.click" %><!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Headrunat= "Server"><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> <Scriptsrc= "A.js"></Script> <title></title></Head><Body> <formID= "Form1"runat= "Server"> <%--onclick="Myonclick ()"--%> <%--OnServerClick="Btn_clickserver"--%> <inputtype= "button"onclick= "Myonclick ()"ID= "BTN1"value= "button" /> </form></Body></HTML>
You can write the event on an HTML page or in the called JS file (A.js here).
A.js:
function Myonclick () { alert ("triggers the onclick event!) ");} function del () { alert ("OK");}
Operation Result:
2.onserverclick event, this is the method of executing the service side.
<type= "button" onserverclick= "Btn_clickserver" ID = "BTN1" value= "button" runat= "Server"/>
Corresponding events are supplemented in the background:
Public void btn_clickserver (object Sender,eventargs e) { string' <script>alert (' You triggered the Onclickserver event ');</script> " ; Response.Write (str); Response.End (); }
Execution Result:
Note that when the OnClick event is used with the OnServerClick () event, the OnClick event executes first, but be careful not to add the runat= "server" attribute and the no-client JS file cannot be called.
OnClick Events and OnServerClick events