What are User controls?
User controls are custom, reusable controls and they use the same techniques that are employed by HTML and web server controls. They offer an easy way to partition and reuse common user interfaces across ASP.NET Web applications. They use the same Web Forms programming model on which a Web Forms page works.Creating User Controls
User Controls are added to the site like any other content type: through the Add New Item dialog box. Similar to pages, you get the option to choose the programming language and whether you want to place the code in a separate Code Behind file.Now Add following code to file .ascx
<strong><span style="font-size: xx-large;">Web User Control</span></strong>
Register the user control on the Web Forms page.
<%@ Register src=”WebUserControl.ascx” tagname=”WebUserControl” tagprefix=”uc1” %>
To instantiate or use the user control on the Web Forms page, add the following line of code in the <form> tags.
<form id=”Form1” method=”post” runat=”server”>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</form>
How a user control is processed :
When a page with a user control is requested, the following occurs:- User controls begins with a <%@Control%> directive instead of a <%@Page%> directive.
- The page parser parses the .ascx file specified in the Src attribute in the @ Register directive and generates a class that derives from the System.Web.UI.UserControl class.
- The parser then dynamically compiles the class into an assembly.
- If you are using Visual Studio then at design time only, visual studio creates a code behind file for the user control, and the file is precompiled by the designer itself.
- Finally the class for the user control, which is generated through the process of dynamic code generation and compilation, includes the code for the code behind file (.ascx.cs) as well as the code written inside the .ascx file.
0 comments:
Post a Comment