HowTo change Stylesheet with a normal <a href:"..">Link</a>

Last post 09-03-2008 9:28 AM by NC01. 9 replies.

Sort Posts:

  • HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-02-2008, 6:57 PM
    • Member
      point Member
    • deki85
    • Member since 09-02-2008, 12:12 PM
    • Posts 6

    Hi... just one question for you guys..

    Im new in the asp.net world but i LOVE IT =) anyway ... I need help =( ...-> how can i do this in ASP.Net with C# ?? -> Subject

    THX VERY MUCH!

     DEKI

     

     

     

  • Re: HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-02-2008, 7:19 PM
    Answer

    Deki,

    I'm not sure how to do this programmatically, since a link isn't a server side control.  It sounds like you are trying to rig up some theming for your site (I apologize in advance if I'm wrong about this).

    The best way to accomplish theming is to use ASP.NET Themes: http://msdn.microsoft.com/en-us/library/ykzx33wh.aspx

    You basically have different theme folders in your web application, each with a different css file.  You can set the default theme in your web.config file, or in the page directive.

    You can also set the theme for a page or your site programmatically, which in turn will get you what you are looking for, as different css files will get loaded depending on what theme you set for the page in code behind.

    James

    James Ashley, Magenic Technologies
    (james.ashley.magenic@gmail.com)
  • Re: HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-02-2008, 7:19 PM
    • Participant
      945 point Participant
    • photoshoper
    • Member since 04-07-2007, 7:32 AM
    • Posts 279

     What are you trying to do?

  • Re: HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-02-2008, 7:36 PM
    • Member
      351 point Member
    • codysechelski
    • Member since 11-05-2007, 6:03 PM
    • Plano, Texas
    • Posts 88
     You will need to add a runat="server" atribute and an id in order to access it from code

     

    Example:  (this example changes the stylesheet if the browser is IE6)

    ASP.net 

    1    <head>
    2       ...
    3          <link runat="server" id="maincss" href="css/main.css" rel="stylesheet" type="text/css" />
    4       ...
    5    </head>
    

     

    VB.net (sorry I don't know C# too well, but it should be very close)

    1        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2            Dim bv As String = Request.Browser.Browser.ToString() + Request.Browser.Version.ToString()
    3            If bv = "IE6.0" Then
    4                maincss.Href = "css/IE6main.css"
    5            End If
    6            ...
    7        End Sub
    
  • Re: HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-02-2008, 7:56 PM
    Answer
    • Member
      351 point Member
    • codysechelski
    • Member since 11-05-2007, 6:03 PM
    • Plano, Texas
    • Posts 88

    Sorry, I may have misunderstood...

    If you want to just use <a> tags to change the stylesheet you can do that with javascript - no C# required. You will need to be sure and give your <link> tag in the <head> and id

    1    <head runat="server">
    2        ...
    3        <link id="mystyle" href="Style/StyleSheet.css" rel="stylesheet" type="text/css" />
    4        
    5        <script type="text/javascript">
    6            function chageStyle(ss) {     //ss is the argument that you will use to pass the location of the stylesheet
    7                document.getElementById("mystyle").href = ss;
    8            }
    9        </script>
    10   </head>
    11   <body>
    12       <form id="form1" runat="server">
    13       ...
    14   
    15       <a href="#" onclick ="chageStyle('Style/StyleSheet.css')">This is one style</a>
    16       <a href="#" onclick ="chageStyle('Style/AnotherStyle.css')">This is another style</a>
    17   
    18       ...
    19       </form>
    20   </body>
    
     
  • Re: HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-02-2008, 8:23 PM
    Answer
    • Contributor
      5,079 point Contributor
    • NHOQUE
    • Member since 04-02-2008, 9:00 AM
    • Kumamoto, Japan
    • Posts 849

    deki85:

    Hi... just one question for you guys..

    Im new in the asp.net world but i LOVE IT =) anyway ... I need help =( ...-> how can i do this in ASP.Net with C# ?? -> Subject

    THX VERY MUCH!

     DEKI

    Here is it.

    Tested with IE7 and FF2

    <%--
    red.css File in Theme Folder
    
    body{
    color:Red;
    }
    
    blue.css File in Theme Folder
    
    body{
    color:Blue;
    }
    
    --%>
    
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="chngStyle.aspx.cs" Inherits="myTest_chngStyle" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>無題のページ</title>
        <link rel="Stylesheet" href="theme/red.css" id="lnk" />
        <script type="text/javascript">
        function stylechngr(obj){
        document.getElementById("lnk").href=obj;
        }
        
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <a href="javascript:stylechngr('theme/blue.css');">StyleSheet:Blue</a>
        <a href="javascript:stylechngr('theme/red.css');">StyleSheet:Red</a>
        <p>You can change my color by clicking on the above link. Thanks.</p>
        </div>
        </form>
    </body>
    </html>
    

     

    Thanks.

    HOQUE MD.NAZMUL
    [document.getReaders]
  • Re: HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-03-2008, 7:32 AM
    • Member
      point Member
    • deki85
    • Member since 09-02-2008, 12:12 PM
    • Posts 6

    THX 4 your help.. the last post with javascript is good... but i need to implement this whitout Javascript..

    Im developing a Webcalendar for users with visual impraiment.. so i have to follow the WCAG 1.0 guidelines.. and its forbiden to use javascript on the sites..

     maybe a nother solution?? whitout javascript??

     

    THX

    deki

  • Re: HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-03-2008, 7:54 AM
    Answer
    • All-Star
      75,655 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,090
    • TrustedFriends-MVPs

    deki85:

    THX 4 your help.. the last post with javascript is good... but i need to implement this whitout Javascript..

    Im developing a Webcalendar for users with visual impraiment.. so i have to follow the WCAG 1.0 guidelines.. and its forbiden to use javascript on the sites..

     maybe a nother solution?? whitout javascript??

     

    THX

    deki

    You've got 2 choices. Use JavaScript as NHOQUE posted or do a PostBack on each click and use the CodeBehind.

    NC...

  • Re: HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-03-2008, 8:49 AM
    • Member
      point Member
    • deki85
    • Member since 09-02-2008, 12:12 PM
    • Posts 6

    hmmm

    Im new in this asp.net ... so i dont have much experience... can u give me maybe a little solution how i can implement this? Imporant is that javascript is forrbiden.. & i dont know what NHOQUE is.. .=((

    MANY THANKS!!

     deki

  • Re: HowTo change Stylesheet with a normal <a href:"..">Link</a>

    09-03-2008, 9:28 AM
    Answer
    • All-Star
      75,655 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,090
    • TrustedFriends-MVPs

    Try this:

    aspx file:

    <form id="form1" runat="server">
     This is some content being displayed
     <br />
     <a id="blueLink" href="http://forums.asp.net/AddPost.aspx?ReplyToPostID=2597480&Quote=False#" runat="server" onclick="__doPostBack('ColorLinkPostBack', 'Blue');">Set to Blue</a>
     <a id="redLink" href="http://forums.asp.net/AddPost.aspx?ReplyToPostID=2597480&Quote=False#" runat="server" onclick="__doPostBack('ColorLinkPostBack', 'Red');">Set to Red</a>
    </form>

    aspx.cs file:

    protected void Page_Load(object sender, EventArgs e)
    {
     this.ClientScript.GetPostBackEventReference(this, string.Empty);

     if (this.IsPostBack)
     {
      string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
      string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];

      if ( eventTarget == "ColorLinkPostBack" )
      {
       System.Text.StringBuilder styleScript = new System.Text.StringBuilder();

       styleScript.Append("<style type=\"text/css\">\n");
       styleScript.Append("BODY\n");
       styleScript.Append("{\n");
       styleScript.AppendFormat(" color:{0};\n", eventArgument);
       styleScript.Append("}\n");
       styleScript.Append("</style>\n");

       this.ClientScript.RegisterClientScriptBlock(this.GetType(), "BodyStyleSheet", styleScript.ToString());
      }
     }
     else
     {
      string defaultColor = "Green";

      System.Text.StringBuilder styleScript = new System.Text.StringBuilder();

      styleScript.Append("<style type=\"text/css\">\n");
      styleScript.Append("BODY\n");
      styleScript.Append("{\n");
      styleScript.AppendFormat(" color:{0};\n", defaultColor);
      styleScript.Append("}\n");
      styleScript.Append("</style>\n");

      this.ClientScript.RegisterClientScriptBlock(this.GetType(), "BodyStyleSheet", styleScript.ToString());
     }
    }

    NC...

Page 1 of 1 (10 items)
Microsoft Communities