2

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?
4

2 回答 2

5

Your web.config looks fine. In the page you need to add a using with the namespace of your control. Then you can use the page's LoadControl method to get an instance of your control that you can then add as a child control to some container (I usually choose a asp:Placeholder to ensure its position is where I want it)

于 2012-04-07T19:41:34.680 回答
1

@Adrian, here is my response to your question. I have to say it here, because I will run far over my char limit.

Ok, the first problem I had was that I was using a ASP.NET Website, not a ASP.NET Application. I chaged this so I was using the latter. This made it so that all pages created from then on had a namespace by default (being the name of the folder they were in). I do not know if you can register controls using a namespace in a website, maybe because websites don't have namespaces by default.

So let's say I made a Web User Control called myControl.ascx, and saved it as ~/controls/myControl.ascx. Looking at the ascx file, the top line should read: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="myControl.ascx.cs" Inherits="myApplication.controls.myControl" %>. myApplication is the name of your website, and you can seed it added controls to the namespace because that is the folder it is in. Also, if you look at the code behind, the namespace will match.

Now, from here you have 2 options on how to register and use the control. The slow way is to register each control you want to use on each page you have, which is good if you only have a few of each. To do this, write the following line in the page you want to call the control from (like the index page, for example):

<%@ Register TagPrefix="myC" TagName="myCoolControl" Src="~/control/myControl.ascx" %>

That is the slow way, but it works (unless I typed something wrong).

Here is what I was trying to do: make it so I don't have to type this a thousand times, and only do it once in the web.config file.

Configure your web.config file to look like this:

<configuration>
    <system.web>
        <pages>
            <controls>
                <add tagPrefix="myCont" namespace="myApplication.controls" assembly="myApplication" />
            </controls>
        </pages>
    </system.web>
</configuration>

This will automaticly register any control made in the "myApplication.controls" namespace, which, if you set things up correctly, is any control made in the "controls" folder. Just remember, you don't have to call the folder "controls" for this to work, you may have any namespace listed here and it should work just fine.

Now open up a page you want to load a control from programmatic (like index). You are NOT going to write anything in the aspx file, including anything about registering the page. The control is already registered, so all we have to do is load it in the code-behind:

myControl myCont = LoadControl("~/controls/myControl.ascx") as myControl;
//feel free to use the variable, or add it to the page with a panel or something

I really hope this answered your question. It did take some digging around on my end to get it to work, so I'm glad to help others out with this problem. Thanks again to CyberDude, who got me going in the right direction.

于 2012-05-11T20:14:21.803 回答