473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to dynamically change the style sheet based on browser type

In a VS2005 ASP.NET project, I'm trying to find a way to change which css
file is linked in based on the browser type. I'd like to use one css file
for IE browsers, and a different one for all other browsers. I'm using a
MasterPage. I added a Literal control to the Head section of the
MasterPage, and then in the Page_Load event I set its Text property to set
up the desired link, like this:

StyleSheetLiteral.Text = "<link rel=\"stylesheet\" type=\"text/css\"
href=\"/inc/content_ie.css\">";

This works, but the VS2005 IDE reports it as an error, with message
"Unrecognized tag prefix or device filter 'asp'." Plus I can't help
thinking that there must be a better way.

I also tried setting the Page.StyleSheetTheme property in the Page_Load
event, but this gives a System.InvalidOperation exception when the page
loads. The documentation for Page.StyleSheetTheme says that it must be set
before the page's Init event completes, but I can't figure out how to access
that event in the MasterPage.

Any ideas on the best way to accomplish setting the style sheet dynamically?

Thanks in advance,
--Rob Roberts

Jan 21 '06 #1
2 5140
The <link>tag in ASP.NET 2.0 can runat=server, e.g. you can place on the
master page:
<link runat="server" id="StyleLink1" type="text/css"/>

You can access it directly on the master page like this:
StyleLink1.Href ="MyStyleSheet";

Or you can access it in the content page like this:

//assuming that you have
//<%@ masterType VirtualPath="~/MasterPage.master" %> in your page markup
HtmlLink lnk = (HtmlLink)Master.FindControl("StyleLink1");
if (lnk != null) lnk.Href="MyStyleSheet";

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Rob Roberts" wrote:
In a VS2005 ASP.NET project, I'm trying to find a way to change which css
file is linked in based on the browser type. I'd like to use one css file
for IE browsers, and a different one for all other browsers. I'm using a
MasterPage. I added a Literal control to the Head section of the
MasterPage, and then in the Page_Load event I set its Text property to set
up the desired link, like this:

StyleSheetLiteral.Text = "<link rel=\"stylesheet\" type=\"text/css\"
href=\"/inc/content_ie.css\">";

This works, but the VS2005 IDE reports it as an error, with message
"Unrecognized tag prefix or device filter 'asp'." Plus I can't help
thinking that there must be a better way.

I also tried setting the Page.StyleSheetTheme property in the Page_Load
event, but this gives a System.InvalidOperation exception when the page
loads. The documentation for Page.StyleSheetTheme says that it must be set
before the page's Init event completes, but I can't figure out how to access
that event in the MasterPage.

Any ideas on the best way to accomplish setting the style sheet dynamically?

Thanks in advance,
--Rob Roberts


Jan 21 '06 #2
Thank you very much! That worked perfectly.

--Rob Roberts

"Phillip Williams" <Ph**************@webswapp.com> wrote in message
news:5F**********************************@microsof t.com...
The <link>tag in ASP.NET 2.0 can runat=server, e.g. you can place on the
master page:
<link runat="server" id="StyleLink1" type="text/css"/>

You can access it directly on the master page like this:
StyleLink1.Href ="MyStyleSheet";

Or you can access it in the content page like this:

//assuming that you have
//<%@ masterType VirtualPath="~/MasterPage.master" %> in your page markup
HtmlLink lnk = (HtmlLink)Master.FindControl("StyleLink1");
if (lnk != null) lnk.Href="MyStyleSheet";

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Rob Roberts" wrote:
In a VS2005 ASP.NET project, I'm trying to find a way to change which css
file is linked in based on the browser type. I'd like to use one css
file
for IE browsers, and a different one for all other browsers. I'm using a
MasterPage. I added a Literal control to the Head section of the
MasterPage, and then in the Page_Load event I set its Text property to
set
up the desired link, like this:

StyleSheetLiteral.Text = "<link rel=\"stylesheet\" type=\"text/css\"
href=\"/inc/content_ie.css\">";

This works, but the VS2005 IDE reports it as an error, with message
"Unrecognized tag prefix or device filter 'asp'." Plus I can't help
thinking that there must be a better way.

I also tried setting the Page.StyleSheetTheme property in the Page_Load
event, but this gives a System.InvalidOperation exception when the page
loads. The documentation for Page.StyleSheetTheme says that it must be
set
before the page's Init event completes, but I can't figure out how to
access
that event in the MasterPage.

Any ideas on the best way to accomplish setting the style sheet
dynamically?

Thanks in advance,
--Rob Roberts


Jan 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: alex | last post by:
I'm new to xslt, and I am attempting to use it to produce a comma-separated-value file from a large, dynamically-generated data file formatted in xml (examples of the xml file and my xslt style...
7
by: Michael Hill | last post by:
I have a perl script that i use to render a html page. In that page i call out a stylesheet. If the user has a 800x600 display the fonts are really too big. Since screen resolution is a function...
12
by: techie | last post by:
Hi All, I have a portal working on an intranet for a company. The portal was designed keeping in mind the resolution of 1024*768. I am including a ..css file in all the files. Now i have to make...
1
by: Hank Flowers | last post by:
In trying to load a style sheet via JavaScript I ran into a problem...the style sheet doesn't load. This simple task has turned into a vexing problem. Here is the script and html: <html> <head>...
3
by: VSK | last post by:
Hi, Iam looking for a way to dynamically add the stylesheet. I have all the .aspx pages extend from a CommonPage.cs which extend from System.Web.UI.Page. Now i want a way to detect the...
2
by: Steve Franks | last post by:
What is the proper/best way to dynamically change the HTML written out to the browser based on the value from a variable in a code behind page? For instance, I have test.aspx.vb as a code behind...
7
by: Tim Rogers | last post by:
Hi folks, this is a resolution-detect script that I used on a site. As you can see it is designed to detect when the screen resolution falls below a certain level then load an alternative style...
6
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.