@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.