Summary of differences between different OnClick events in Asp. Net (onserverclick, onclientclick)

Source: Internet
Author: User

1. HTML controls, such as The onclick (client) and onserverclick (server) Events of IMG input buttons. Pay attention to runat = "server "...

Onclick is executed before onserverclick.
2. WEB controls, such as <asp: button and other onclientclick (client) and onclick (server) events...

Here, onclientclick is executed before onclick.
The onclientclick event comes from 2.0, which is used to replace the following code...

In short, the client click event is executed before the server click event. That is to say, first execute the client's Javascript and then execute the server's related code on the server side.

Appendix: html controls, html server controls, and web User Controls
The key to the convenience and quickness of asp.net development is that it has a set of powerful control libraries, including web server controls, web user controls, web custom controls, html server controls, and html controls. Here we will mainly talk about the differences between html controls, html server controls, and web server controls.
1. html controls: These are commonly used html language tags that exist on previous static pages and other webpages and cannot be controlled on the server, it can only be controlled on the client through javascript, vbscript, and other programming languages. <Input type = "button" id = "btn" value = "button"/>
2. html server Control: in fact, it is the control composed of the html control plus runat = "server. the differences between them are that html controls run on the client, while html server controls run on the server. For more information, see ASP. when the web page is executed, the system checks whether the annotation has the runat attribute. If the annotation is not set, the Html annotation will be treated as a string and sent to the string stream for sending to the client, the browser of the client will explain it. If the Html annotation has the set runat = "server" attribute, the Page object will put the control into the controller, and the server code will be able to control it, after the control is executed, convert the execution result of the Html Server Control to Html annotation, the stream is sent to the client as a string for explanation. <input id = "Button" type = "button" value = "button" runat = "server"/>
3. web Server Control: Also known as asp.net Server Control, is a basic element of Web Form programming and also exclusive to asp.net. It generates one or more html controls based on the client, rather than directly describing html elements. For example, <asp: Button ID = "Button2" runat = "server" Text = "Button"/> what is the difference between it and the html server Control? For more information, see the following:
1) The Asp.net Server Control provides a more unified programming interface. For example, each Asp.net Server Control has the Text attribute.
2) Hide the differences between the client, so that the programmer can focus more on the business, instead of considering whether the client browser is ie, firefox, or a mobile device.
3) The Asp.net server control can be saved to ViewState, so that the page can be saved during the process of uploading the page from the client back to the server or downloading it from the server to the client.
4) different event processing models: Html tagging and Html Server Control event processing are on the client page, while the Asp.net server control is on the server. For example:
<Input id = "Button4" type = "button" value = "button" runat = "server"/> is an Html server Control. Click this button, the page will not be uploaded back to the server, because we have not defined a mouse click event for it.
<Input id = "Button4" type = "button" value = "button" runat = "server" onserverclick = "test"/> we added an onserverclick event to the Html server Control, click this button to send the page back to the server and run the test (object sender, EventArgs e) method.
<Asp: Button ID = "Button2" runat = "server" Text = "Button"/> is an Asp.net server Control and we do not define a click for it. However, when we click, the page is also sent back to the server.
It can be seen that Html tagging and Html server control events are triggered by pages, while Asp.net server controls are sent back to the server by pages for processing.

Onclick, onclientclick and onserverclick

The following uses HTML buttons (system.web.ui.html controls) and ASP. NET Server buttons (

System. web. ui. webcontrols) as an example:
1. HTML controls, such as The onclick (client) and onserverclick (server) Events of IMG input buttons,

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

Onclick is executed before onserverclick.
2. WEB controls, such as <asp: button and other onclientclick (client) and onclick (server) events...

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

Here, onclientclick is executed before onclick.
The onclientclick event comes from 2.0, which is used to replace the following code...

Btn. Attributes. Add ("onclick", "SideScript ");

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

Execute the relevant code on the server.

Differences between Onclick and OnserverClick events in ASP. NET

For the server Button control (that is, the Button of the <asp: Button> type ):
Server Response event: OnClick
Client Response Property: OnClientClick

For html button controls (that is, <input type = "button" runat = "server">)
Server Response event: OnServerClick
Client RESPONSE event: onclick
Asp: the Button type is onclick.
<Input type = "button" runat = "server"/> onserverclick
One is a. net Control ~ One is converting html controls into server-side controls
The results are exactly the same. It should be a client event with onclick in the input. Microsoft can only use onserverclick as the server response name.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.