I have several UserControls that are .cs files and extend WebControl. I add them to my .aspx pages via the following:
<%@ Register TagPrefix="uc" Namespace="AssemblyName.My.Namespace" Assembly="AssemblyName" %>
However, I find that I’m having to do a lot of rendering via the HtmlTextWriter. So I wanted to move toward using .ascx files for my user controls. However, they will only show up on the page if I use the following syntax:
<%@ Register src="../../../controls/MyCustomControl.ascx" tagname="MyCustomControl" tagprefix="uc" %>
These .ascx controls extend UserControl, and the associated codebehind is in the same namespace as my .cs web controls.
“I have several UserControls that are .cs files”
Those are “custom controls” or “web controls”, not
UserControls. UserControls have a markup associated with them and inheritUserControl.“However, they will only show up on the page if I use the following syntax:”
That’s because
UserControlsare split (partial) classes that include an uncompiled part (the markup). You could add them with just the assembly reference, and the project would compile. However, the markup component would not be loaded, and you’d get object reference errors among other things at run time.