• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 620
  • Last Modified:

Access CSS stylesheet from codebehind?

I need to access an reproduce the stylesheet of a page in codebehind because I am emailing forms that use these styles.  How can I access a page's styles in codebehind?

  • 5
  • 3
1 Solution
Steve KrileCommented:
If you are sending the email and you know your recipients can receive HTML emails, you don't need to.  Simply add a reference to your style sheet to the body of your message.

Say you had an email you were building from code like this:

      'Create an instance of the MailMessage class
      Dim objMM as New MailMessage()

      'Set the properties
      objMM.To = txtEmailAddy.Text
      objMM.From = "YourEmailAddress"

      'Send the email in HTML format
      objMM.BodyFormat = MailFormat.Html

      'Set the subject
      objMM.Subject = "DataGrid Emailing"

      'Set the body - use VbCrLf to insert a carriage return
      objMM.Body = "<style src='http://www.mysite.com/css/default.css'></style><font face=""Verdana"">This is an example of emailing with an embedded style sheet reference." & RenderedControlHTML
jglazer63Author Commented:
Ahh but what if the styles are in the <style> section of the page itself?
Steve KrileCommented:
Please forgive me...I typed too fast and from (bad) memory.  The Body of your object needs to look like this:

dim bodyStr as StringBuilder = New StringBuilder()

bodyStr.Append("<HTML><HEAD><<ink rel="stylesheet" href=""http://www.mysite.com/css/mystyle.css"" type=""text/css""/></HEAD>")


Then you can set the body like:

objMM.Body = bodyStr.ToString()
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


That is a super interesting question... I hope someone comes up with a better answer than I've been able to figure out.

I found lots of interesting ways to interface with the styles in the head section, but none that truly meet your request.

The IStyleSheet allows you to add/register styles to the head section:
IStyleSheet styles = this.Header.StyleSheet;

The StyleSheetTheme will give you info about linked stylesheet filenames and then you could read those manually with a stringreader:
string ss = this.StyleSheetTheme;

You could get inline styles of a particular element using the folowing (but only works on inline style darnit):
ICollection k = body1.Style.Keys;

It looks like an uphill battle. I would have thought there would be a class already built to enumerate the CSS in a <style> block, but I cant find one. I am intrigued to see if anyone else does.

The closest I could get you will require some work and that is to simply add an Id and runat="server" to you existing <style> block thusly:
<style type="text/css" runat="server" id="ss1">

Then you can read the text manually and it would look something like this:
{InnerText = "\r\n\t\t.any{text-align:center;}\r\n    \r\n    "}
//This will probably get goofed up by the text editor I'm posting in right now :(

It's quite possible from there to parse this all out with regex or any way you like, but will be some work.

Steve KrileCommented:
ddayx:  I wonder why you want the style sheet's contents in the email.  Most users will be retrieving email from a web-enabled device.  No?  Am I missing an obvious thing here?
Steve KrileCommented:
OK, just noticed "Ahh but what if the styles are in the <style> section of the page itself?"  

I guess I have a very strict rule I apply to my own designs - I NEVER put style information on my designer pages.  
jglazer63Author Commented:
I realize I could follow such a rule but I was hoping for a "generic" solution.  I'll keep playing.
jglazer63Author Commented:
Does anyone have any additional ideas here?
Steve KrileCommented:
Well, I've recently been building emails to be sent out when various things happen (sign up to a web site, change password, that sort of thing).  I've been looking at the nicely formatted emails I get from professional companies and noticed something very peculiar.  Most of the well-formatted emails I get DO NOT HAVE STYLE SECTIONS at the top of the email.  Instead, just about all that I've seen include the exact formatting in the individual tags of the email.  I believe this may have something to do with email programs stripping out http:// references in <head> sections of email - but I'm not sure about that.

Anyway, I have gone to this approach.  I'm building email templates that are much more plain than my website design but that contain all the formatting rules in each tag.  A bit tedious, but the results as rendered in my users emails is consistent and well controlled.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now