views:

235

answers:

2

I am writing an application that requires that my customer can layout their input forms in which ever way they choose.

I would like to use an HTML template with placeholders, and then replace these with secified .NET controls at runtime:

<table style="width: 100%;">
<tr>
    <td colspan="2">
        <fieldset title="Customers Template" >
            <table style="width:100%;">
                <tr>
                    <td width="140">
                        Policy Number:</td>
                    <td width="150">
                        $$Policy Number$$</td>
                    <td colspan="2" rowspan="3" valign="top">
                        Type 1:<br />
                        $$Type 1$$</td>
                    <td rowspan="3" valign="top">
                        Info:<br />
                        $$Info$$</td>
                    <td rowspan="3" valign="top">
                        Problems:<br />
                        $$Problems$$</td>
                </tr>...

So I want to find and replace my $$xxx$$ text with various .NET controls, depending on the datatype of the field being added, at runtime.

Any suggestions on good ways to approach this?

Thanks, Mark

+1  A: 

Replace your $$xxx$$ blocks with <asp:PlaceHolder> controls.

At runtime you can parse the template

--- In the most simplest case, you can require the template be valid xhtml, then you can simply use the .NET XML APIs to iterate through all the ChildNodes of the XmlDocument. From each element build the appropriate ASP.NET Server control.

For a more complex (and complete HTML parser) see http://www.developer.com/net/csharp/article.php/2230091

As you parse, wherever you see <asp:PlaceHolder> - replace that with your logic that defines your template.

Tutorial: http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=752

Adam
The HTML template is created and supplied, created and controlled by my customer and dynamically loaded at runtime. I think asp place holders won't get registered properly in the controls collection when loading the file dynamically :-(
Mark Cooper
Depending on how you implement the 'loading' the file dynamically - you could parse the template and build the ASP.NET controls programmatically.
Adam
Aha, this is what I am after :-) I already know how to build the ASP.NET controls programmatically - but how would I actually parse the template?
Mark Cooper
updated my answer to reflect your latest question
Adam
A: 

If you're going to replace these placeholders with only strings take a look at NVelocity template engine. In this answer, I simply show how you can create an email by a template.

But for asp.net server controls, you can split your html template into chunks, and add them to your page as LiteralControl like that :

Page.Controls.Add(new LiteralControl(@"<td width='140'>
                        Policy Number:</td>
                    <td width='150'>"));
Page.Controls.Add(textBox1);
Page.Controls.Add(new LiteralControl(@"</td><td colspan='2' rowspan='3' valign='top'>
                        Type 1:<br />"));
Canavar