Monday, August 18, 2008

Register Custom Control in Web Page

When we want to use a User Control (which is defined in *.ascx) in a web page, we can register the control like this:
<%@ Register Src="~/MyObjectDisplayer.ascx" TagName="MOD" TagPrefix="CustomControl" %>

If we use a Custom Control (which is defined in *.cs), however, the register tag should be a little different:
<%@ Register Namespace="MyControls" TagPrefix="CustomControl" %>

Whenever we want to place this control in the webpage, we write:
<CustomControl:PromptButton id="pb1" runat="server"/>

It assumes that our control classes is PromptButton and it is in MyControls namespace. And note that it has been tested in ASP.NET Web Site project, but not in ASP.NET Web Application project.

1 comment:

Xiaoguang said...

For Web Application Projects, we must specify Assembly attribute for the Register tag. Otherwise we'll get the "Unknown server tag" error. The assembly name is the project name that the custom control resides in.