Dynamic User Controls

A

anthonyroche

I have a web app (ASP.Net) using Templates with predominatly the body
content stored in the db and displayed to screen using a PageID
querystring. What I want to know is, if a content manager adds the
tags
<wuc:news id="News" runat="server"></wuc:news> into the body section
via a CMS together with other page data (HTML content), how can I
detect this tag (user control) and actually load it in the place
he/she wants.
Example HTML content loaded from db

<table><tr><td>some text here</td></tr>
<tr><td> more text maybe with images</td></tr>
<!-- NOW I WANT THE NEWS USER CONTROL -->
<tr><td><wuc:news id="News" runat="server"></wuc:news></td></tr>
</table>

I use a placeholder on the template page to pick up the string(above)
that is stored in the DB which works fine. When I view source I can
see the <wuc.. tag but ofcourse the news control is not rendered or
loaded.

Any ideas?
 
M

Martin Dechev

Hi,

You will have to parse the "html" (which happens to be a little bit more
than html) and add the controls by yourself. Let's suppose you have a method
that renders the "html" in question with the following signature:

public void Render(string content, HtmlTextWriter writer){}

You will have to parse the string in content. So, when you find pure html
(for direct output) do:

writer.Write(pureHtml);

When you find some control (i.e. WUCNews) you will have to first instantiate
it from the corresponding class:

WUCNews news1 = new WUCNews();

Then probably set some properties (you might need to parse some attributes):

news1.Page = Page;
news1.EnableViewState = false;
// etc...

And in the end render it:

news1.RenderControl(writer);

Hope this helps
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top