Click here to Skip to main content
15,917,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all , i have created smalll web application using c#, and i have 2 css files,
i want to dynamically change the cssfiles, when certain condition is true,how to do this..

your answer will be great helpful to me
Posted

try:
ASP.NET
<head>
<link id="MyStyleSheet" rel="stylesheet" type="text/css"  runat="server" />
</head>


in your Page_Load, simply add a "href" attribute as below:
VB
Page_Load(Sender As Object, E As EventArgs)
If (!IsPostBack)
 MyStyleSheet.Attributes.Add("href","/css/flostyle.css")
 
Share this answer
 
v2
Comments
sameertm 10-Oct-11 3:55am    
its showing error that , MyStyleSheet is does not exists in the current context
member60 10-Oct-11 4:04am    
add the MyStyleSheet in head section this work definitely
Try this :
<head>
<script type="text/javascript">
if (Condition)
{
           document.write("<link href='Stylesheet.css' rel='stylesheet' type='text/css' media='screen' />")
}
</script>
</head>


or

stringbuilder str = new stringbuilder;
str.append("Above Javascript");
label1.text = str.toString;

or

MyStyleSheet.Attributes.Add("href","Stylesheet.css")
 
Share this answer
 
v2
declare 2 labels
label1 for javascript code & label2 for postback reference

from codebehind you can use
if (condition)
{
label2.text = "'Stylesheet.css'" //css path
}


stringbuilder str = new stringbuilder;
str.append("<script type="text/javascript"> +
"if (Condition)" + 
""{document.write('<link href="
<big>label2.text</big> +
" rel='stylesheet' type='text/css' media='screen' />')}" +
"</script>" +
");</script>") 
label1.text = str.toString;
 
Share this answer
 
v4
Hi,

you can use this type of code for that

HTML
<%if (Session["lbl"].ToString() != "") {
  %>
<link href="Styles/basic.css" rel="stylesheet" type="text/css" />
  <%
  } else {
  %>
<link href="Styles/basic_ie.css" rel="stylesheet" type="text/css" />
  <%
  }
       %>


you can change whatever you want by following this way

All the Best
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900