This article describes how to create user controls in ASP.NET, dynamic modifications of control, control attributes, and control of the control event. ============================================================================================================================================================================================================= ================ Introduction ASP.NET server control makes web development work easier and more powerful. We introduce how to use the server control in the ASP.NET page. However, what should I do if the server does not have any requiring controls?
Of course, ASP.NET will not change from a need for a control. In fact, you can do your own control to replace the control provided by .NET. This control is the user control, which is also the topic discussed in this article.
Some people who write the first user control believe that knowing how to use the server control can not necessarily mean that writing user controls is easy.
In fact, writing a basic user control (sometimes known as Pagelets) and uses these controls as the ASP.NET page icon using the server control. Here is a simple example:
Basic.ascx
this is a user control ... really! p>
This is a user control! Seeing this, I think you will say that I should not get drunk, my mind is not clear. But this code is indeed a user control that is easy to use. Although this control has nothing, it is about what is a good description of user controls. Things are not as complex. Pay attention to suffix .ascx, which tells web pages This is a user control. It doesn't have any special meaning, just don't let IIS go directly to this code.
Now let's create a user control, look at the example below:
Basic.aspx <% @ page language = "VB"%> <% @ register tagprefix = "asp101samps" tagname = "sylext" src = "Basic.ascx"%>
body> html>
This code outputs a standard HTML page that displays the text in the user control instead of a mark. So how is it implemented? The key is in the registration (Register). To register your control, first define three properties:
TagPRefix Defines the namespace of the control location. With namespace constraints, you can use different functionality controls in the same page.
Tagname points to the name of the control used. The control name in the same namespace is unique. Control names generally indicate the functionality of the control.
SRC points to the resource file of the control. The resource file uses the virtual path ("Control.ASCX" or "/Path/control.ascx", cannot use the physical path ("C: /Path/control.ascx."). After the control is registered, you can use it as other server controls. By defining the Tagrefix and Tagname (Tagname), you can use it as used with the server's built-in control. It also determines the use of server operations (Runat = "Server") mode. Below is the basic way to call user controls on the web page:
Add the attribute to the user control and assign the value. I add two properties to the control, one is color, the other is Text.