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?

Thanks!
jglazer63Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
      
      SmtpMail.Send(objMM)
0
jglazer63Author Commented:
Ahh but what if the styles are in the <style> section of the page itself?
0
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>")

bodyStr.Append(RenderedControlHTML)
bodyStr.Append("</HTML>")

Then you can set the body like:

objMM.Body = bodyStr.ToString()
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

ddayx10Commented:
Wow,

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.

dday
0
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?
0
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.  
0
jglazer63Author Commented:
I realize I could follow such a rule but I was hoping for a "generic" solution.  I'll keep playing.
0
jglazer63Author Commented:
Does anyone have any additional ideas here?
0
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.