Is it possible to load a User Control that was registered in the web.config file, rather than in the aspx file?
Here is an example of what I would do. In my aspx:
index.aspx
<%@ Register TagPrefix="module" TagName="ModExample" Src="~/controls/example.ascx" %>
index.cs
protected void Page_Load(object sender, EventArgs e)
{
controls_example exmp = LoadContent("controls/example.ascx") as controls_example;
myContentPanel.Controls.Add(exmp);
}
I had to register my user control in the page, or else ASP would not know what "controls_example" was. However, I know you can change your web.config to look like this:
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="module" tagName="ModExample" src="~/controls/contentModule.ascx"/>
<add assembly="Subtext.Web.Controls" namespace="Subtext.Web.Controls" tagPrefix="module"/>
</controls>
</pages>
</system.web>
</configuration>
So, here is the problem. How do I create a variable of type "controls_example" when it has been registered in the web.config file?
Do I need to:
- add a "using namespace"?
- change/add something in my web.config?
- not define what the datatype of the variable is? (I would love to avoid this)
- something else?
Best Answer
Your
web.config
looks fine. In the page you need to add ausing
with the namespace of your control. Then you can use the page'sLoadControl
method to get an instance of your control that you can then add as a child control to some container (I usually choose aasp:Placeholder
to ensure its position is where I want it)