Page.ParseControls combined with custom webcontrols

I recently ran into a problem that had me stumped for a while.  In one of my legacy ASP.NET 1.1 applications, I use a few different custom webcontrols where I’ve over-ridden the default render() method in order to produce different output.  For example, I wanted to use a CheckBoxList web control, but I was unhappy with the way it rendered by default, for both the flow and table methods.

This worked fine for normal pages, and is usually not really a big deal.  However, when it came time to using them in a project of mine that used Page.ParseConrols() to dynamically generate ASP.NET controls, I was always getting the error “Unknown server tag: blah”.

Page.ParseConrols() is not a very common method to use for generating ASP.NET pages, but in my case it was necessary.  This particular application generated webpages by transforming XML using an XSL stylesheet, and then sought to render the results of that transformation to the page.  However, I also wanted to be able to use the results of that transformation in codebehind, if the user wanted to perform a postback on the rendered page.  So instead of using XSL to simply output common HTML tags, I output tags like asp:TextBox and asp:Button.  Normally all this would do is print literal text of asp.net tags to the screen, because the webcontrols aren’t being processed.  But Page.ParseConrols() allows you to pass a string to it, and it outputs a control that can be added to a PlaceHolder.

That description is a little muddled, but hopefully you sort of get the idea.  Anyways, the problem came about when I tried to also use a custom webcontrol in this manner, alongside the regular asp.net webcontrols.  It would always give me the unknown server tag error, even though I had clearly included my @Register TagPrefix directive on the page with the placeholder.

The trick was, you have to include the @Register directive along with the rest of the content that’s passed to Page.ParseConrols().  Once you include that directive as a prefix to the rest of the html, it works as expected.

See the following links for resources that helped me:

http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.webcontrols/browse_frm/thread/1bb2ab396fd26b0b?hl=en&lr=&ie=UTF-8&oe=UTF-8&rnum=1&prev=/groups%3Fq%3DPage.ParseControl%2Btag%2Bprefix%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D%2523lZ%2524jzRdCHA.1540%2540tkmsftngp10%26rnum%3D1

http://forums.asp.net/p/376584/1514100.aspx#1514100

Advertisements

0 Responses to “Page.ParseControls combined with custom webcontrols”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: