473,396 Members | 1,938 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,396 software developers and data experts.

Server-Side CSS on Page Load

24
Very quick question.

I have a master page (M1) and three content pages (C1, C2, C3). In M1, there's a list of three anchors (A1, A2, A3) which point towards the three content pages.

The anchors have some CSS to make them look pretty. I'd like to be able to dynamically set the CSS for A1 when I navigate to C1 so it looks slightly different to the other two (e.g. to indicate this is the currently selected item).

Below is what I have so far. However, unfortunately, this isn't working. I can confirm that the "class" attribute IS being set correctly, and I can even see it if I view the resultant page source, but the actual style doesn't change.

I have found another way to do this by manually setting the Style properties of the anchor in the code behind. but I would prefer to just assign it to a CSS class instead, to keep all my CSS in one place.

My question is - can anyone tell me why, even though the class attribute appears to be being set, the style does not change? My guess it's got something to do with load order on the page, but I'm confused that I can see the class attribute has been set in the browser's "view source" option...

So far, I have:

In M1 (markup):
Expand|Select|Wrap|Line Numbers
  1.         <ul class="mainMenuBar">
  2.             <li><a runat="server" id="homeLink" href="Main.aspx">Home</a></li>
  3.             ...
  4.         </ul>

My CSS:
Expand|Select|Wrap|Line Numbers
  1. .mainMenuBar
  2. {
  3.     list-style-type:none;
  4.     /*Some stuff to make the list look good*/
  5. }
  6.  
  7. .mainMenuBar li
  8. {
  9.     display:inline;
  10. }
  11.  
  12. .mainMenuBar li a
  13. {
  14.   /*Some stuff to make the anchors look pretty*/
  15. }
  16.  
  17. .mainMenuBar li a:hover
  18. {
  19.   /*Some stuff to change the look of the menu item on hover*/
  20. }
  21.  
  22. .currentMenuItem
  23. {
  24.  /*Some stuff to make the menu item look "current". */
  25. }

In M1 (code behind):

I have a function to change the style of the selected anchor. Basically, this function can be called from C1 (or C2, C3, etc.) on Page_Load. The idea is that calling this will set the "class" attribute of the selected anchor:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub MakeCurrent(ByVal control As HtmlAnchor)
  2.         control.Attributes("class") = "currentMenuItem"
  3.     End Sub
  4.  
  5.     Public Sub MakeCurrent(ByVal lt As LinkType)
  6.         Select Case lt
  7.             Case LinkType.Home
  8.                 MakeCurrent(homeLink)
  9.                 ...
  10.         End Select
  11.     End Sub
  12.  
  13.     Public Enum LinkType
  14.         Home
  15.         ...
  16.     End Enum

And finally in the Page_Load of C1 (and the rest), I have the following to call the MakeCurrent method in my master page and change the class of my anchor:
Expand|Select|Wrap|Line Numbers
  1.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.         ' Set the css class of the home link on the master page to indicate this is the current page.
  3.         CType(Page.Master, Main).MakeCurrent(Main.LinkType.Home)
  4.     End Sub

Thanks in advance.

-Q
May 21 '09 #1
3 6646
Queez
24
My apologies, I just checked the w3 specification for the anchor (a) tag:
Expand|Select|Wrap|Line Numbers
  1. <!ELEMENT A - - (%inline;)* -(A)       -- anchor -->
  2. <!ATTLIST A
  3.   %attrs;                              -- %coreattrs, %i18n, %events --
  4.   charset     %Charset;      #IMPLIED  -- char encoding of linked resource --
  5.   type        %ContentType;  #IMPLIED  -- advisory content type --
  6.   name        CDATA          #IMPLIED  -- named link end --
  7.   href        %URI;          #IMPLIED  -- URI for linked resource --
  8.   hreflang    %LanguageCode; #IMPLIED  -- language code --
  9.   rel         %LinkTypes;    #IMPLIED  -- forward link types --
  10.   rev         %LinkTypes;    #IMPLIED  -- reverse link types --
  11.   accesskey   %Character;    #IMPLIED  -- accessibility key character --
  12.   shape       %Shape;        rect      -- for use with client-side image maps --
  13.   coords      %Coords;       #IMPLIED  -- for use with client-side image maps --
  14.   tabindex    NUMBER         #IMPLIED  -- position in tabbing order --
  15.   onfocus     %Script;       #IMPLIED  -- the element got the focus --
  16.   onblur      %Script;       #IMPLIED  -- the element lost the focus --
  17.   >
As you can see, there's no class attribute there, so this is just me being a moog.

Annoyingly, it looks like there's no easy way to do what I'm trying to do using the HTML anchor, so I'll have to use some other element (such as an ASP.NET Button, or something). No big deal.

Cheers anyway.

-Q
May 21 '09 #2
Frinavale
9,735 Expert Mod 8TB
I set the style of links all the time....


To make sure I tested this (using the w3cschool's Try It editor) and it works:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <style>
  4. .linkStyle{
  5.   color:red;
  6. }
  7. .linkStyle2{
  8.   color:green;
  9. }
  10. .linkStyle3{
  11.   color:blue;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16.  
  17. <a href="lastpage.htm" target="_blank" class='linkStyle'>Last Page</a> 
  18. <a href="lastpage.htm" target="_blank" class='linkStyle2'>Last Page</a> 
  19. <a href="lastpage.htm" target="_blank" class='linkStyle3'>Last Page</a> 
  20. <p>
  21. If you set the target attribute of a link to "_blank",
  22. the link will open in a new window.
  23. </p>
  24.  
  25. </body>
  26. </html>

My guess would be that your browser cache needs clearing??
May 22 '09 #3
Hi ... Not sure if you are still trying to resolve this issue, but if not ...

Make your 'li' element a named server control (include an ID attribute and runat="server"). Then, apply you main and current styles through the 'li'.
Nov 5 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Phil | last post by:
I am using a Pascal like language (Wealth-Lab) on W2K and call this server: class HelloWorld: _reg_clsid_ = "{4E797C6A-5969-402F-8101-9C95453CF8F6}" _reg_desc_ = "Python Test COM Server"...
9
by: Grim Reaper | last post by:
My work let me put SQL Server 7.0 Enterprise Edition on my laptop. I have never setup a server from the beginning, so I am a little new at creating server groups. Alright, I am trying to create...
0
by: Chris Halcrow | last post by:
Hi I've spent ALL DAY trying to re-install SQL Server 2000 on Windows XP. I continually get the error 'cannot configure server' just at the end of the installation. I've tried the following: ...
0
by: Zorba.GR | last post by:
IBM DB2 Connect Enterprise Edition v8.2, other IBM DB2 (32 bit, 64 bit) (MULTiOS, Windows, Linux, Solaris), IBM iSoft Commerce Suite Server Enterprise v3.2.01, IBM Tivoli Storage Resource Manager...
4
by: coosa | last post by:
Hi, I was installing SQL Server on my machine and during installation my PC freezed. It happens frequently on my machine. So i tried after restarting to install it again and since then i always...
1
by: Peter | last post by:
I've purchased VS.NET 2005 Standard and have tried to install SQL Server 2005 Express, but get the following error in the error log. Please could someone help me.... Microsoft SQL Server 2005...
14
by: Marcus | last post by:
I have a function that simply returns TRUE if it can connect to a particular Sql Server 2005 express, or FALSE if it cannot. I am getting some strange error codes returned when the computer that...
10
by: sara | last post by:
Hi All, I was able to connect to MS SQL Server 2005 on my computer but after a while I can not. When I want to connect to it using MS SQL Server Management Studio I got this error: An error...
1
by: manish deshpande | last post by:
Hi, When i'm installing MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm by the following command: rpm -i MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm the following error is being shown: ...
14
by: Developer | last post by:
Hello All, i have recently installed VS2005 and was trying to install SQL sever 2000. I have Win XP' SP2. But when I tried installing, it only installed client tools and not the database. Can...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.