using CSS style and class in server control

Discussion in 'ASP .Net Building Controls' started by Steve Richter, May 23, 2005.

  1. 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
     
    Steve Richter, May 23, 2005
    #1
    1. Advertising

  2. Steve Richter

    Guest

    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


    Steve Richter wrote:
    > 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
     
    , May 23, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dlo_olb
    Replies:
    2
    Views:
    2,340
    Fredrik Lundh
    Jun 25, 2003
  2. Aaron Beall
    Replies:
    2
    Views:
    1,850
    Aaron Beall
    Sep 14, 2007
  3. Ken Varn
    Replies:
    0
    Views:
    496
    Ken Varn
    Apr 26, 2004
  4. Nik Coughlin
    Replies:
    2
    Views:
    186
    Nik Coughlin
    Jan 23, 2008
  5. Jayden
    Replies:
    9
    Views:
    304
    alex23
    Sep 27, 2012
Loading...

Share This Page