using CSS style and class in server control

S

Steve Richter

Looking for some basic guidance on how or whether I should use class=
and <style> and whatever you call CSS to control the appearance of my
menu server control.

It is great that I can render a sequence of <a> elements to the page,
using the class= attribute to control the appearance, with the result
being a decent looking menu bar.

But how do I inject the CSS classes into the style block of the page?
And once those classes are placed in the page, how does the server
control avoid name clashes with other server controls doing the same
thing?

What is the alternative? Do I have to duplicate the style behavior in
each <a> element of my menu control?

thanks,

-Steve
 
L

lisa

Hi Steve,

It's a matter of taste, I think. You can include the stylesheet in
your control and mark it Embedded Resource. Then you can use
RegisterClientScriptBlock to add it to the page (it doesn't matter that
it's a stylesheet and not a script block). If you use
IsClientScriptBlockRegistered, you can ensure that you only get one
copy of the script added in. Here's an example of reading the resource
into the page. I call this in PreRender:

Protected Overridable Sub RegisterScript()
'this script is client script and should appear only once
If Not Page.IsClientScriptBlockRegistered("ListBoxPlus_js") Then
Dim reader As New StreamReader(Me.GetType().Assembly.
GetManifestResourceStream(Me.GetType(), "ListBoxPlus.js"))
Dim script As String = "<script language='javascript'
type='text/javascript' >" _
+ ControlChars.CrLf _
+ "<!--" _
+ ControlChars.CrLf _
+ reader.ReadToEnd() _
+ ControlChars.CrLf _
+ "//-->" _
+ ControlChars.CrLf _
+ "</script>"
Page.RegisterClientScriptBlock("ListBoxPlus_js", script)

reader = Nothing
script = Nothing
End If

In terms of ensuring that you don't walk on other styles that the user
might use, one solution is to use unfriendly names like
MyBigDamnControl_TableHeader. Things that aren't likely to be
duplicated. Or you can just put the styles into the Render, which is a
bit of a pain, but you only have to do it once (twice, if you use a
custom designer), and then it's done and more or less bulletproof.

Lisa
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top