Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use TemplateControl.ParseControl with custom server controls?

188 views
Skip to first unread message

Ting Zwei Kuei

unread,
Oct 16, 2002, 10:02:55 AM10/16/02
to
I am using ParseControl to dynamically generate web forms from XML and XSLT,
which works well with the standard ASP WebControls as defined by
System.Web.UI.WebControls. The problem comes when I want to add custom
WebControls such as Microsoft's IE WebControls.

Below is the test code I am using, simply save it as ParseControl.aspx, you
will also need to copy Microsoft.Web.UI.WebControls.dll into the /bin
directory in your application root folder.

When you call this script, you will get:

Parser Error Message: Unknown server tag 'MSWC:Toolbar'.

The error suggests that TemplateControl.ParseControl does not recognise the
tag prefix MSWC, so how does one go about specifying the tag prefixes
TemplateControl.ParseControl can accept?

<%@ Page Language="C#" Debug="true"%>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="Microsoft.Web.UI.WebControls" %>
<%@ Register TagPrefix="MSWC" Namespace="Microsoft.Web.UI.WebControls"
Assembly="Microsoft.Web.UI.WebControls" %>
<html>
<head>
<script language="C#" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
phContent.Controls.Add(Page.ParseControl("<ASP:Button runat=\"server\"
/>"));
phContent.Controls.Add(Page.ParseControl("<MSWC:Toolbar runat=\"server\"
/>"));
}
</script>
</head>
<body>
<form runat="server">
<ASP:PlaceHolder runat="server" id="phContent" />
</form>
</body>
</html>

--
Ting Zwei Kuei


Ting Zwei Kuei

unread,
Oct 17, 2002, 10:31:19 AM10/17/02
to
I have solved my own problem... the trick is to include the @Register
directive in the string you pass to the ParseControl method.

For example:

<%@ Page language="c#" debug="true"%>


<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="Microsoft.Web.UI.WebControls" %>

<html>
<head>
<script language="C#" runat="server">
void Page_Load(object sender, System.EventArgs e)
{

Control tbEdit = Page.ParseControl("<%@Register TagPrefix=\"MSWC\"
Namespace=\"Microsoft.Web.UI.WebControls\"
Assembly=\"Microsoft.Web.UI.WebControls\"%><MSWC:Toolbar runat=\"server\"
id=\"tbEdit\"><MSWC:ToolbarButton runat=\"server\" Text=\"Select All\"
/><MSWC:ToolbarButton runat=\"server\" Text=\"Clear Selection\"
/><MSWC:ToolbarButton runat=\"server\" Text=\"Delete Selected\"
/></MSWC:Toolbar>");
phContent.Controls.Add(tbEdit);

Ting Zwei Kuei

unread,
Oct 17, 2002, 11:05:53 AM10/17/02
to
It would be nice if Microsoft can make all ASP.NET @ directives accessible
via APIs. ;)


--
Ting Zwei Kuei


bruce barker

unread,
Oct 17, 2002, 1:30:40 PM10/17/02
to
as the directives are source code for the page compile, an api makes little
sense. for those that an api makes sense (say @OutputCache), have one.

-- bruce (sqlwork.com)

"Ting Zwei Kuei" <ting...@kuei.com> wrote in message
news:uh74P6edCHA.1540@tkmsftngp10...

0 new messages