Thursday, March 29, 2012

HTML from code behind

Hi,
A hopefully simple question for someone to answer. I wish to dynamically change the HTML code from VB code behind. For example I have a table setup in the HTML code and what I want to do is display formatted text within this table that has been placed dynamically by code behind, depending on a querystring. See example below

<td width="300" rowspan="13" valign="top"><p></p
(CODE TO PLACED HERE BY CODE BEHIND)
</td
Above a table is defined in HTML, but I want the below code to be placed into this table by using code behind (this will be dependent on a querystring so that different text will be displayed depending on what link was used)

<p align="right"><font face="Arial, Helvetica, sans-serif">This
is a test
</font></p>
<p align="justify"><font face="Arial, Helvetica, sans-serif">Want
this sample to be sent from the code behind. How can it be done?</font></p
Can anyone please help?
ThanksYou may use <asp:table/> control and add tablerow and tablecell at run time.
Thanks for that, I had a look in MSDN and that will work fine for creating the tables, rows and cells but what about the formatted text. I noticed that there is innerhtml, could you please explain how I could use this. Would I be able to create a static table, that is written in HTML but add "runat = server" in the table definition. Then add HTML from code behind using innerHTML. I have tried getting it to work but I keep getting syntax errors. Any thoughts would be great.

Thanks
James
The easiest way (I think) is to put a placeholder where you want your dynamic text to go:

<table>
<tr>
<td>
<asp:Placeholder id="TableCellPlaceholder" runat='server" />
</td>
</tr>
</table>

In your codebehind, you of course define this placeholder:

Protected TableCellPlaceholder As System.Web.UI.WebControls.Placeholder

Then, when you get to adding the dynamic text, you just add it as a LiteralControl:

Dim HtmlTtoInject As String
If Request.Params( "s" ) = "Google" Then
HtmlToInject = "< b>Thanks for choosing Google</b>"
Else
HtmlToInject = "< i>It's time you tried Google</i>"
End If
TableCellPlaceholder.Controls.Add( New LiteralControl( HtmlToInject ) )

This is admittedly a quick and dirty approach ... however, it's also perhaps the easiest.

Hope this helps.
Great, that works just how I wanted. Thank you :)

You did say:
"This is admittedly a quick and dirty approach ... however, it's also perhaps the easiest."

What other way may you do this? Just for future reference.

Thanks again
Well, it's more "object-oriented" to do what JimmyM said, and create TableRow objects, which contain TableCell objects, which in turn contain Label objects or Literal objects ... and then attach the TableRow objects to your Table object.

But, you can do that later when you need a more structured approach to dynamically building a table.

For the simple purpose you said, using a Placeholder is nice and easy.
Just put in an <asp:label> control and use this code:

label1.InnerHtml="Here is some HTML code <bthis is in bold</b> this is a link

Hope this helps

Colin Cameron

No comments:

Post a Comment