SoFunction
Updated on 2025-03-03

Summary of the differences between different OnClick events (onserverclick, onclientclick)

In short, the client click event is executed before the server click event. In other words, first execute the client's Javascript, and then execute the server's relevant code on the server.

Attachment: html control, html server control and web user control
The reason why development is convenient and fast now is that it has a powerful control library, including web server controls, web user controls, web custom controls, html server controls and html controls, etc. Here we mainly talk about the difference between html control, html server control and web server control.
1. HTML control: It is what we usually call html language tags. These language tags exist in previous static pages and other web pages. They cannot be controlled on the server side. They can only be controlled on the client side through program languages ​​such as javascript and vbscript. <input type="button" value="button"/>
2. HTML server control: In fact, it is a control composed of runat="server" based on the HTML control. The difference between them is that the running mode is different. HTML control runs on the client side, while HTML server control runs on the server side. Referring to other materials, it is said: When the web page is executed, the marker will be checked for the runat attribute. If the marker is not set, the Html marker will be regarded as a string and sent to the string stream to wait for sending to the client, and the client's browser will explain it; if the Html marker has the set runat="server" attribute, the Page object will put the control into the controller, and the server-side code can control it. After the control is executed, the execution result of the Html server control will be converted into the Html marker, and then sent to the client as a string stream for explanation <input type="button" value="button" runat="server" />
3. Web server control: also known as server control, it is a basic element of Web Form programming and is also unique. It will generate one or more html controls according to the client's situation, rather than directly describing the html element. For example: <asp:Button ID="Button2" runat="server" Text="Button"/> So what is the difference between it and the html server control? Refer to the information of other web pages as follows:
1) Server controls provide a more unified programming interface, such as each server control has a Text property.
2) Hide the differences between clients, so programmers can focus more on business without considering whether the client's browser is ie, firefox, or mobile devices.
3) The server control can save the status to ViewState, so that the page can be saved during the process of being returned from the client to the server or downloaded from the server to the client.
4) The event processing model is different. The event processing of Html annotations and Html server controls are both on the client's page, while the server controls are on the server. For example:
<input type="button" value="button" runat="server"/> is an Html server control. When we click this button, the page will not be returned to the server because we did not define the mouse click event for it.
<input type="button" value="button" runat="server" onserverclick="test" />We have added an onserverclick event to the Html server control. Clicking this button page will be sent back to the server side and the test(object sender, EventArgs e) method is executed.
<asp:Button ID="Button2" runat="server" Text="Button" /> is a server control, and we do not define a click for it, but when we click, the page will also be sent back to the server side.
It can be seen from this that the events of the Html annotation and the Html server control are triggered by the page, while the server control is sent back to the server by the page and handled by the server.

The difference between onclick, onclientclick and onserverclick

Below are HTML buttons ( ) and server buttons (

) As an example:
1. HTML controls, such as onclick (client) and onserverclick (server) events of IMG input buttons,

Note runat="server". . .
<input type="button" name="btn" value="button" runat="server" onserverclick="ServerSideEvent" onclick="SideScript" />

The onclick here is executed before the onserverclick.
Controls such as the onclientclick (client) and onclick (server) events of the <asp:button button, etc. . .

<asp:Button ID="btn" text="button" runat="server" OnClick="SideScript" OnClientClick="ClientSideScript" />

The onclientclick here is executed before the onclick.
The onclientclick event comes from 2.0 and is used to replace the following code. . .

("onclick","SideScript");

In short, the client click event is executed before the server click event. That is, first execute the client's Javascript, and then

Execute the server-related code to the server side.

The difference between Onclick and OnserverClick events

For server button controls (i.e. buttons of type <asp:Button>):
Server response event: OnClick
Client response attribute: OnClientClick

For html button control (i.e. <input type="button" runat="server">)
Server response event: OnServerClick
Client response event: onclick
asp:Button type is onclick
<input type="button" runat="server" /> type is onserverclick
One is a .net control ~ the other is an html control converted into a server-side control
The effect is exactly the same, it should be a client event with input itself with onclick. Microsoft can only use onserverclick as the name of the server-side response.