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
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