Inherit from System.Web.UI.HtmlControls ??

J

Jill Graham

Hi folks,

The pages of my website are built dynamically and are based on templates.

A template can look like this :

<table>
<tr><td>This is the page header</td></tr>
<tr><td>This is the page content</td></tr>
<tr><td>This is the page footer</td></tr>
</table>

People can modify the templates to suit their needs.

My problem is as follows :
Some people, when modifying the template, want to add some style definitions
using <STYLE>...</STYLE> and these style definitions should be put in the
heading of the page (<head>...</head> section).

To accomplish this, I've written a custom control (named "style") which will
copy the style section from the template to the page header.
The template now looks like this :

<emc:style runat="server">
<style>
.content {font-family:Arial,Verdana,Sans-serif; color:#ff0000;
font-size:10px;}
</style>
</emc:style>
<table>
<tr><td>This is the page header</td></tr>
<tr><td class="content">This is the page content</td></tr>
<tr><td>This is the page footer</td></tr>
</table>

Once the custom "style" control is executed, its "innerhtml" property (the
<style> itself) is read and copied to the pages header.
Here is the VB code for the custom "style" control :

Public Class Style
Inherits System.Web.UI.htmlcontrols.HtmlContainerControl

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
Me.EnableViewState = False
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim headerControl As System.Web.UI.WebControls.PlaceHolder =
Page.FindControl("_head")
if not isNothing(headerControl) then
Dim newStyle As New
System.Web.UI.HtmlControls.HtmlGenericControl
newStyle.TagName = "STYLE"
newStyle.InnerText = Me.InnerHtml
headerControl.Controls.Add(newStyle)
newStyle = nothing
end if
headerControl = nothing

Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
End Sub
End Class


This works fine ! However, I just read in a book from MS Press that we
should NEVER write a custom control that inherits from
System.Web.UI.htmlcontrols because they break the HTML Control Model. But I
have to inherit from this class because of the use of tge "innerHTML"
property in my code.

Can anybody help me out here ? Is there another solution for this ?

Thanks

Jill
 
T

Teemu Keiski

Hi,

having the innerhtml feature is same as adding a LiteralControl with the
HTML to the child controls (of your control).
 
T

Teemu Keiski

I mean that InnerHtml proeprty works with HTML controls that way. In the
property set accessor the given HTML string is instantiated into
LiteralControl which is added to the Controls collection of the control.
Therefore it means that you can get similar feature, but you can derive from
System.Web.UI.Control or System.Web.UI.WenControls.WebControl.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top