I love .NET, but it is not perfect. If you’ll remember from past mini-rants, SelectSingleNode() selected the first node in the document… not the node in the context of the XmlNode you called the method from (this may still be the case; I haven't checked it out yet). And no Directory.Copy() method exists (but one for File?). In my venture into .NET 2.0, I’ve found another "feature" that I'm scratching my head at. It involves creating content controls (<asp:Content/>) dynamically.
As a quick background, Content controls are used in the new master pages feature in ASP.NET 2.0. A master page is essentially a template that content pages reference in order to add dynamic content while keeping the same look and feel site-wide. In a master page, you create content placeholder controls (<asp:ContetPlaceHolder/>). These controls, as their name suggests, are merely placeholders. A content page contains content controls, which contain data (text, markup, server controls) to render to the corresponding ContentPlaceHolder in the master page.
In the content page, you can assign a master page programmatically by using the MasterPageFile property:
Page.MasterPageFile = "~/MyMasterPage.master";
Of course, common sense suggests you can do the same with content controls by using the System.Web.UI.WebControls.Content class:
Content myContent = new Content();
If you run the above code, you will receive no error. However, if you try to assign the ContentPlaceHolderId property to correspond to a placeholder in the master page, you'll get a runtime error stating that it isn’t possible to assign this property.
This behavior is strange. First, the IntelliSense says you can (“Gets or sets the ID of the ContentPlaceHolder control that is associated with the current content"). Second, the online documentation says that this property isn’t read-only (get; set;). Third, what the hell? This should be possible... simply out of common sense. If you can assign a master page programmatically, shouldn’t you be able to do the same for content controls?
Thankfully, there is a work around. You simply retrieve a reference to the placeholder control in the master page:
ContentPlaceHolder myContent = (ContentPlaceHolder) Master.FindControl(“myContent”);
myContent.Controls.Add(Page.ParseControl("Hello, World"));
This technique is quicker than creating, populating, and inserting content controls into the page, but I still find the latter more logical.
Once again, I find something of the .NET framework that doesn't make much sense. Surely someone thought of this… at least I hope they would.