Hi,

I have a WebUserControl, let's call it "BaseControl.ascx" that acts as a base control for a custom control (a class that inherits BaseControl). Inside BaseControl I have a couple of regular controls (for example a DropDownList called 'dropdown'), which I try to populate in the inheriting control:
csharp Code:
  1. namespace ProjectName.Controls
  2. {
  3.     public class SpecializedControl : BaseControl
  4.     {
  5.         public void SetupList()
  6.         {
  7.             dropdown.DataSource = "ABCDEFG".ToCharArray();
  8.             dropdown.DataBind();
  9.         }
  10.     }
  11. }

As far as I know, the only way to register a custom control (eg, not a .ascx control but a class inheriting a control) is to use this syntax on the page:
Code:
<%@ Register Assembly="ProjectName" Namespace="ProjectName.Controls" TagPrefix="custom" %>
This seems to work, I can place a SpecializedControl on the page with this:
Code:
<custom:SpecializedControl runat="server" ID="test" />
However, when I now call the 'SetupList' method on this control, from the Page_Load event, it seems that 'dropdown' (the DropDownList control in BaseControl.ascx) is null, it seems it is never instantiated.


To test what is happening I just created a simple TestControl.ascx (note: an ascx file again, so a WebUserControl and not a custom control), also with a dropdown and the same SetupList method.

When I register this control using the following syntax, everything works fine:
Code:
<%@ Register Src="~/Controls/TestControl.ascx" TagName="TestControl" TagPrefix="custom" %>
I can call SetupList and the list is populated just fine.

When I register the exact same control however with the other syntax, it does not work, and 'dropdown' is null:
Code:
<%@ Register Assembly="ProjectName" Namespace="ProjectName.Controls" TagPrefix="custom" %>
In this test case I can choose which syntax to use, but in my actual case I can't. The control I want to put on my page is not an ascx file, it's a class that inherits another control (a custom control), so as far as I know I am stuck with this second syntax. So, I cannot get this to work...

I also tried creating the control dynamically using LoadControl, but that gives the same problem.


Long story short: it seems like I have to use the first syntax to register a control (with a 'Src' tag pointing to the control ascx file), but I can't since there is no ascx file...

How can I do this properly?