In the past (AKA 1.1) if I wanted to programatically load a control I would do something like:
MyNameSpace.MyControl myControl = LoadControl("~/MyNameSpace/MyControl.ascx") as MyNameSpace.MyControl;
myControl.myAttribute = 5;
this.Controls.Add(myControl);
Made sense, you could also cast the control. Anyhoo, this doesn't
work at all in
ASP.NET 2.0. So what to do? I read some
other junk on the web but you had to add a Reference or Register
declaration to the top of the page. Puke. Absolute
puke. I don't wanna do it this way!
So I came up with what I'm guessing was the way they wanted you to do
it (and strangely works out better). Lets go over the steps:
First, open up your web.config. Add your control to the controls section like so:
<configuration>....
<system.web>...
<pages>
<controls>
<add
tagPrefix="ui" tagName="MyControl"
src="~/MyNameSpace/MyControl.ascx"/>
</controls>
</pages>
</system.web>
</configuration>
Now, simply add the control on your page where you would want it to appear.
<ui:MyControl runat="server" ID="myJunk" />
Now, something magical happens. It's automagically casted to your
user control. You simply go into your code and do stuff like we
did before, except no need to cast, or add the control to the page:
myJunk.MyAttribute = 5;
And that's it. Can't say it's a little wierd, but I think I like it!