Our favorite thing to do in asp.net is to drag a button and double-click it, and then enter the code. This is called the event return mechanism, belongs to the software development system category, but it is not the scope of web development, because the Web is based on TCP/IP protocol, but TCP/IP protocol is a stateless protocol, then ASP.net is how to introduce the incident return?
First, create a new Web site, and then add a webusercontrol named: Eventmodule. As follows:
Now drag a button and 3 labels inside the Eventmodule, as follows:
Then double-click the button and enter the following code:
this.Label1.Text = this.Button1.ID;
this.Label2.Text = this.Button1.ClientID;
this.Label3.Text = this.Button1.UniqueID;
Drag the eventmodule.ascx into the Default.aspx, press F5 Test, and double-click the button to see what happens. My situation is as follows:
Do you think it's confusing? Why are these three IDs different? So what is the connection between the event model mechanism and this? OK, we right click on the page, select View Source, find this line:
<input type= "Submit" Name= "Eventmodule1$button1" value= "Eventbutton" id= "Eventmodule1_button1"/>
Oh, what is this? People who know ASP.net will soon find this is the HTML code generated by the button, ASP.net developers may often ignore this HTML, because this is not hand-written, it is very convenient to use, isn't it? If this is the case, then we have no meaning in this lesson, to find out this is my favorite thing to do, if you plan to dig down, then please continue, I guarantee that this will give you a deep understanding of the asp.net, and when these fundamental realizations accumulate to a certain extent, you can write high-performance Web interactive Web sites.
What should we study after the problem? You can think about it. When someone clicks on the button and the server can respond to the appropriate event correctly, presumably there is a way to find the button on the server side, but by what means?
To create a comparison, for example, why the server knows that this button is not another button that triggers the event, we drag another button in the Eventmodule.ascx and double-click it to give it an OnClick event, as follows:
Now insert a breakpoint into the event code for one of the above button (note that the breakpoint is only useful in F5 mode), as follows:
F5 test, click the button and it will return to VS2005 debug mode. The Local Variables window finds the form element under->this->base->request, such as the following expansion:
Notice where the green highlights are highlighted, and here you can see that even if we put two button, there is only one button returned to the server via the TCP/IP protocol, and the key is that button at the client's name value: EventModule1 $Button 1. That's why I'm the first to let you see the source code (note: EventModuel1 is the Eventmodule.ascx ID created automatically on Default.aspx). Well, the truth should be clearer, so how did asp.net find the button through Eventmodule1$button1? First we can do the following analysis Default.aspx has a child under the name Eventmodule1,eventmodule1 has a button, named Button1. So "$" becomes the identifier we look for the child control, because the IDs of all the child controls in the same level page must be different, so each object corresponds to a UniqueID, that is, Eventmodule1$button1, Then ASP.net will be able to pinpoint our corresponding control in this way and activate its corresponding OnClick event.