Upload
fenil-desai
View
213
Download
0
Embed Size (px)
Citation preview
8/9/2019 UserControls Vs CustomControls
http://slidepdf.com/reader/full/usercontrols-vs-customcontrols 1/2
Q: What's the difference between User Controls and Custom Controls? A: There are two main categories of creatable Web controls. User controls are more
simple to create, but custom controls are more simple to use. The extra effort that goes
into creating custom controls pays off for the developer using the control at design time.
User controls are the natural evolution of include files, which are well known by oldschool ASP developers. If you're still using server-side include files in ASP.NET, it'stime to leave them behind and dig into user controls, which are better in virtually everyway. User controls are little more than a snippet of a Web page that can be draggedand dropped anywhere to duplicate bits of user interface and the associatedfunctionality. (In fact, it's not difficult to convert an entire Web page into a user control.)They are object-oriented, so public properties can be added, as well as methods andevents, to allow them to easily communicate with the page and other controls on thepage.
User controls are saved with an ASCX file extension. Although they can be easilyreused on any of the pages within the project in which they were created, there is nogood way to reuse them in a different project. Another downside to user controls is thatthey only appear on the page at run time; at design time they appear on the page as anugly gray box, making it difficult to envision how they'll appear to your users.
Figure 1 shows the HTML source code for a basic sidebar menu user control. Astandard control like this is a navigational staple needed by virtually every page in aWeb site. It would be wasteful to manually re-create such a structure over and over again for each page. Instead, simply drag this control from the Visual Studio.NET
solution explorer window onto each page as needed. This example does not includeany code-behind logic, but any user control can have a code-behind file of its own tocontain any necessary server-side code. Figure 2 shows how the control looks on aWeb page that includes a CSS style sheet to enhance the appearance. <%@ Control Language="vb" AutoEventWireup="false" %>
<TABLE id="Table1" style="WIDTH: 136px; HEIGHT: 99px"
cellSpacing="1" cellPadding="1" width="136" border="1">
<TR>
<TD align="center">
<asp:HyperLink id="HyperLink1" runat="server"
NavigateUrl="Home.aspx">Home</asp:HyperLink>
</TD>
</TR><TR>
<TD align="center">
<asp:HyperLink id="HyperLink2" runat="server"
NavigateUrl="Products.aspx">Products</asp:HyperLink>
</TD>
</TR>
<TR>
<TD align="center">
<asp:HyperLink id="HyperLink3" runat="server"
8/9/2019 UserControls Vs CustomControls
http://slidepdf.com/reader/full/usercontrols-vs-customcontrols 2/2
NavigateUrl="Services.aspx">Services</asp:HyperLink>
</TD>
</TR>
<TR>
<TD align="center">
<asp:HyperLink id="HyperLink4" runat="server"
NavigateUrl="Contact.aspx">Contact Us</asp:HyperLink>
</TD>
</TR>
</TABLE>
Figure 1: (Above) A basic reusable side menu user control can be as simple as this. This control was created completely by dragging and dropping controls from the Visual Studio.NET toolbar onto an .ASCX user control
page.
Figure 2: (Above) User controls are great for defining the layout for standard sections of Web pages. When
combined with style sheets, they can morph to match the look and feel of any Web page on which they are hosted.
Custom controls can do everything that user controls can do - and much more. Thebiggest drawback is that they are more challenging and complex to create. Drag anddrop is not supported for the creation of these controls. Therefore, all the HTML theyoutput must be generated via code. Pretty much all the articles & controls on this website are examples of custom controls. In addition to run-time functionality without limits,custom controls support rich design-time functionality. They can appear on a page atdesign time the same way they'll appear at run time, or differently if preferred. They can
be adjusted to change how they'll appear in the toolbox, and how the properties willappear in the properties window. If the properties window doesn't support the desiredfunctionality, it can be extended with custom popup dialog boxes and other UI tricks.Custom controls are easily distributed and can be enhanced with help documentationand licensing functionality, allowing them to be sold to other developers.