How is a server-side control event triggered in Asp.net?

Source: Internet
Author: User

When I was a beginner at ASP. NET, I was impressed by the powerful functions of various controls. Of course, it is also easy to get dizzy. Simply remembering the names of those controls is not a simple task, besides, there are so many attributes, events, functions, and configuration steps ...... haha. Later, I felt that these things were of little value and were rarely used in enterprise-level development.

The purpose of writing this article is not to "degrade" or deliberately "exclude" server-side controls in webform. Here we will not discuss the advantages and disadvantages too much.

But it is helpful for beginners: in fact, many controls have similar usage. It is relatively easy to use other widgets after mastering one type. For example, it is not difficult to use listview after using the gridview. Do not "float the control surface", because once you form a certain way of thinking, it will be difficult to improve in the future. Of course, when you understand the nature of the Web and the internal mechanisms of these controls, you will find it too simple to look back at those things. This is another question of "Sharpen the knife first or cut firewood first.
 
We often drag a server control to the ASPX page (such as a button), and double-click it to see that a click event is registered in the background code. Alternatively, you can right-click the control and choose Properties From the shortcut menu. Then, locate the lightning icon-event and register the event. This is the "event-driven mechanism" of the legendary webform ". If you have been familiar with VB or Windows development, you may feel more comfortable.

How are these events triggered? Can I simulate it? Why do I sometimes click the button without triggering background events?

In Asp.net, server events triggered by clients are divided into two situations:

1. The buttons in webcontrols and the type in htmlcontrols are the htmlinputbutton of submit.

The two buttons are displayed on the client:

< input name="Submit1" id="Submit1" type="submit" value=”Submit”>

This is the form submission button. After you click it, it will be sent to the server as a parameter. The parameter is like this: Name attribute of the control = value of the control. The example above is submit1 = submit. The server will know that the button has been clicked based on the key of the received control's name attribute, so that the click event of this button is triggered on the server.
2. htmlinputbutton with the type of button in htmlcontrols and all other control events, such as linkbutton click and textbox change events:
After these events are generated on the client, they are sent to the server through a unified mechanism.
1. First, the Asp.net page framework uses two den domains to store the events triggered by the Control and event parameters:

<! -Indicates the control that triggers the event. Generally, the control name --> <input type = "hidden" name = "_ eventtarget" value = ""/>

<! -Indicates the parameter used to trigger an event. Generally, when a control has more than two events, used to identify the event --> <input type = "hidden" name = "_ eventargument" value = ""/>

2. The server generates a JavaScript method to handle the sending of all these events. The Code is as follows:

< script type="javascript">  < !--  function __doPostBack(eventTarget, eventArgument) {  var theform = document.WebForm2;  theform.__EVENTTARGET.value = eventTarget;  theform.__EVENTARGUMENT.value = eventArgument;  theform.submit();  }  // -->  < /script>

3. Each control that will cause a server event will call the above Code in the response client event
(The browser only recognizes static content such as HTML, JS, CSS, and images. No matter what your Aspx, PHP, and JSP pages are, they will eventually become HTML and be presented to users)
For example, in htmlcontrols, The Click Event of the htmlinputbutton whose type is button

<! -Call the client's Click Event _ dopostback. The eventtarget parameter is 'button2', indicating that the event is triggered by the 'button2' control with the name. The eventargument is empty, the htmlinputbutton with the type of button has only one server event triggered by the client --> <input language = "JavaScript" onclick = "_ dopostback ('button2 ','') "Name =" button2 "id =" button2 "type =" button "value =" button "/>

Another example is the change event of the Textbox Control.

<! -The onchange event of the client calls _ dopostback. The eventtarget parameter is 'textbox1', indicating the event triggered by the 'textbox1' control, the Textbox Control only has one server event textchanged triggered by the client, therefore, the server will trigger the textchanged event of the textbox-> <input name = "textbox1" type = "text" id = "textbox1" onchange = "_ dopostback ('textbox1', '')" Language = "JavaScript"/>

4. after the client triggers an event, it calls the _ dopostback method. The eventtarget and eventargument parameters of the triggered control source are respectively paid to the two hidden domains _ eventtarget and _ eventargument, and then submits the form, on the server side, determine which control event is triggered Based on _ eventtarget and _ eventargument.
 
5. Since we have learned about the "Famous" function _ dopostback, we can also find a way to "leeching. Many people on the Forum often ask how to use common HTML controls (TAGS) and js to call background methods/to stimulate background events. Haha. Can I use "leeching" soon? Of course, there are still many implementation methods, so I will not go into details here.
 
After talking about this, I believe that the reader has understood the so-called "event mechanism" in Asp.net. Or that sentence,Web development is nothing more than "Request/processing/response".

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.