DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Integrate an UserControl .ascx into a .aspx Page

Posted by PANVEGA on July 4, 2008


User controls provide a means by which custom graphical interfaces can be created and reused. A user control is essentially a component with a visual representation. As such, it might consist of one or more Windows Forms controls, components, or blocks of code that can extend functionality by validating user input, modifying display properties, or performing other tasks required by the author.

Web user controls are derived from System.Web.UI.UserControl namespace. These controls once created, can be added to the aspx page either at design time or programmatically during run time. But they lack the design time support of setting the properties created along with the control. They cannot run on their own and they need to stand on another platform like an aspx page. Even if we try to load it on a web browser, IIS will not serve the files of the type .ascx.

  • create your your User Control here Component.ascx in Visual Studio


you can use the script tag in the Component.ascx control <script runat=”server”> ….. </script> . Then you do not need a seperate Code Behind C# or VB class. You can compare this approach with a JavaScript solution however here we have server site code

  • create a simple web application aspx page
  • You have to register your created User Control file on top of your ASPX page

<%@ Page Language=”C#” %>

<%@ Register Src=”Component.ascx” TagName=”rssComponent” TagPrefix=”Master” %>…

  • if you want to add more funtionality to your UserControl you should add some public properties to your control

<%@ Control Language=”C#” ClassName=”Component”%>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Xml” %>

<script runat=”server”>

private int limitItems;
public int LimitItems
get { return limitItems; }
set { limitItems = value; }

private XmlDocument docXml;
public XmlDocument DocXml
get { return docXml; }
set { docXml = value; }

private string rss_Library;
public string RssLibrary
get { return rss_Library; }
set { rss_Library = value; }

private string rss_Transform;
public string RssTransform
get { return rss_Transform; }
set { rss_Transform = value; }

Method and Logic ….


  • here I added some Links to XSLT and XML File. In Adition some properties how to present the Content on the Page.
  • <%=Init_Page()%> at the end of the ascx control loads a method  e.g. 

public string Init_Page() {… do something }

  • Add your TagPrefix Tag to your ASPX page in order to load the public properties and content in your page

<Master:Component xmlns:Master=”null” runat=”server” LimitItems=”10″ RssTransform=”Pages/Component.xslt”
RssLibrary=”Templates/VorlageRSS.xml” />

  • You can handle the public Properties within the ASPX page in the User Control as you normaly do
  • Now build your application

Advantages of a Web User Control:

The biggest advantage of the Web User controls is that they can be created as a site template and used through out the site. For example they can be made to contain the Menu/Link structure of a site and can be used at all the other aspx pages.

This means the following :

1. If the website introduces a new site-wide link within the current layout/structure, it is enough if we put it on the user control once. All pages will be updated once if the web user control is used in them.
2. If there is any link to be corrected, it can be done once at the server side.
3. The .ascx files can either be used as a simple alternative to the plain HTML or they can also be used to respond to events: This means even custom code can be created against them and put in the code behind files.

Drawbacks / Disadvantages:

Though the User controls offer a flexibility of having a site wide modifications, if the whole structure of the site changes, the HTML/aspx code of all the pages should be modified. But as long as the site maintains a same layout, then Web User controls are the number one choice for maintaining the generic layout of the site.

Another disadvantage is It can not be just be simply referenced for using in a different project. If we want to use this User Control in a different project, we have to copy the file and modify the namespace to the host namespace name.

User Controls vs. Other Controls

One source of confusion when working with ASP.NET is knowing the difference between the many different types of controls out there and what they should be called. Microsoft has attempted to distinguish this variety, but myriad names still exist for elements that have only subtle differences. For instance, an ASP.NET control (sometimes called a server control) is a server-side component that is shipped with .NET Framework. A server control is a compiled DLL file and cannot be edited. It can, however, be manipulated through its public properties at design-time or runtime. It is possible to build a custom server control (sometimes called a custom control or composite control). (We will build these in part 2 of this article.)

In contrast, a user control will consist of previously built server controls (called constituent controls when used within a user control). It has an interface that can be completely edited and changed. It can be manipulated at design-time and runtime via properties that you are responsible for creating. While there will be a multitude of controls for every possible function built by third-party vendors for ASP.NET, they will exist in the form of compiled server controls, as mentioned above. Custom server controls may be the .NET answer to ActiveX Web controls.

More Information:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: