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:
namespace ProjectName.Controls { public class SpecializedControl : BaseControl { public void SetupList() { dropdown.DataSource = "ABCDEFG".ToCharArray(); dropdown.DataBind(); } } }
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:
This seems to work, I can place a SpecializedControl on the page with this:Code:<%@ Register Assembly="ProjectName" Namespace="ProjectName.Controls" TagPrefix="custom" %>
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.Code:<custom:SpecializedControl runat="server" ID="test" />
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:
I can call SetupList and the list is populated just fine.Code:<%@ Register Src="~/Controls/TestControl.ascx" TagName="TestControl" TagPrefix="custom" %>
When I register the exact same control however with the other syntax, it does not work, and 'dropdown' is null:
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...Code:<%@ Register Assembly="ProjectName" Namespace="ProjectName.Controls" TagPrefix="custom" %>
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?




Reply With Quote