ASP. NET server controls can be divided into two types, one is the Web server controls, located in the System. web. UI. in the WebControls namespace, the other type is the HTML Server Control, which is located in the System. web. UI. htmlControls namespace.
The Web server Control looks like this <asp: control_name runat = "server" ......>, the HTML server Control looks like this
So why is the control executed on the server after runat = "server" is added to the HTML Tag?
First, let's take a look at how the Web Server Control is added. First, add a Button-type control Button1 on the Default. aspx page.
Then open the Default. aspx. designer. cs file, and we will see the code that actually adds the Button1 object.
Similarly, we add the <p id = "p1" runat = "server"> </p> HTML tag to the page.
Then open the Default. aspx. designer. cs file again. The following code is added:
This is the truth behind the HTML server Control. When we add the runat = "server" attribute to the tag <p id = "p1"> </p>, the background code will create a server-side control HtmlGenericControl object p1.
In this way, we can understand why the HTML Tag becomes a server control simply after the attribute runat = "server" is added. This is because the actual control of this tag is an instance p1 of HtmlGenericControl.
The relationship between HTML tags and corresponding server controls is listed below
In the preceding example, the p tag belongs to "other HTML elements not specified by the specific HTML server control" and is controlled by the HtmlGeneric control. <A> labels or <input> and other labels all have specific server-side controls. The remaining ones include: most labels, such as <p> tag, <div> tag, and <span> tag, are controlled by the HtmlGeneric control.
After runat = "server" is added to the HTML Tag, the background code automatically adds the corresponding HTML server control object to make it a control executed on the server.
This is the secret behind the HTML Server Control.
The ASP. NET server Control also has a feature, that is, all server controls must be placed inside a <form runat = "server"> </form>.
The following is my summary of ASP. NET Server controls:
I hope this article will help my fellow ASP. NET beginners.