Change class of element in code

C

CJM

I have a ASP.NET site with a simple, standard menu:

<div id="menu">
<ul>
<li><a class="selected" id="home" href="index.aspx">Home</a></li>
<li><a id="about" href="/about/">About Us</a></li>
<li><a id="services" href="/services/">Services</a></li>
<li><a id="resources" href="/resources/">Resources</a></li>
<li><a id="contact" href="/contact/">Contact Us</a></li>
</ul>
</div>

As you can see, the Home menu item has the class 'selected', such that it
will render differently to the others to indictate the current page.

There is some code both before and after this section that is standard
across all pages, so I want pull all this html out and place it in an
INCLUDEd file. However, setting the class for the appropriate menu isn't
immediately possible in this example.

I could manually set the class of the element in javascript after the menu
is INCLUDEd, but I thought I'd set it in the codebehind page instead...
except I haven't got a clue how to achieve this (or even if it is possible).

How would I set the class of a particular element in ASP.NET (VB)?

I know the javascript option is easy, but a) there is a small possibility
that javascript will be disable on a client browser, and b) I'm using this
as a learning vehicle for ASP.NET.

Thanks in advance.

CJM
 
C

Cowboy \(Gregory A. Beamer\)

You will have to use JavaScript to do this, if you want to do without a
postback.

If you want to do this based on postback, you have a couple of choices.

One is a menu control. If you want CSS friendly menus, there are CSS
Friendly adapters, although there is no "menu" control. There is a way to
use the treeview to make a simple list, like you have, however. The menu
control allows you to easily have styles for selected, etc.

Another way is to set up each of your list items so there is somethign that
has runat=server that you can control by ID in code behind. You can then set
it. THis is more work than a menu control, so I do not recommend this way,
if you can use the menu, that is.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
 
P

PJ6

If you are only doing this for the sake of learning than it is time for you
to leave behind the mindset of ASP Classic. Don't use "include files". In
fact, bar yourself from writing any HTML whatsoever. Create a new class that
inherits from Panel and build it out using the web control classes in
System.Web.UI. If I remember correctly, you will find an HTMLLink class, but
none for Unordered List so that should be an good learning exercise for you.

HTH,
Paul
 
C

CJM

PJ6 said:
If you are only doing this for the sake of learning than it is time for
you to leave behind the mindset of ASP Classic. Don't use "include files".
In fact, bar yourself from writing any HTML whatsoever. Create a new class
that inherits from Panel and build it out using the web control classes in
System.Web.UI. If I remember correctly, you will find an HTMLLink class,
but none for Unordered List so that should be an good learning exercise
for you.

Never mind ASP Classic - what about the rest of the web? The .NET paradigm
only works for .NET, so I need to consider whether it is worth learning the
'official' Microsoft way, or picking and choosing the tools and techniques
as I see fit.

For example, should I shun HTML as you suggest and use .NET to produce
cloned IE-centric tag soup? Yes, I know the .NET framework has come on a
long way from Frontpage/Word in terms of HTML, but it's still far from
perfect. It's still IE-centric and IE 7 still isn't standards compliant...

I've looked into Master pages and it seems provide some of the benefits of
using SSIs to structure a site/application. But at best it's complicated and
inelegant, and there are significant caveats - no support for @import,
problems rebasing URLs, problems with FindControl, etc. In addition, Master
Pages appear to miss out on the most important benefit of SSIs - caching.
OK, is only a KB or two, here and there but it's the principle.

[Note: that said, I've actually re-jigged my site to use master pages -
another learning exercise! Let's see if I change my opinion over time.]

As for using a Panel (containg .NET controls), doesn't this rather defeat
the object... I'm templating my system for superior performance and ease of
development and maintenance. Sure, I could try to assemble building blocks
using a Panel for a wrapper, but I gain nothing and don't achieve my
original aims. Or am I missing something?

I'm not dismissing your suggestions outright, but I remain to be convinced.
And frankly, to suggest that we need to move even further away from HTML is
an absurd suggestion. The web is awash with 'developers' with no real skill
or knowledge but a copy of Dreamweaver or VS.NET - we need more people who
can code in HTML before they do anything else.
 
C

CJM

Cowboy (Gregory A. Beamer) said:
You will have to use JavaScript to do this, if you want to do without a
postback.

If you want to do this based on postback, you have a couple of choices.

One is a menu control. If you want CSS friendly menus, there are CSS
Friendly adapters, although there is no "menu" control. There is a way to
use the treeview to make a simple list, like you have, however. The menu
control allows you to easily have styles for selected, etc.

Another way is to set up each of your list items so there is somethign
that has runat=server that you can control by ID in code behind. You can
then set it. THis is more work than a menu control, so I do not recommend
this way, if you can use the menu, that is.

--

Thanks for the quick response, Greg.

I'm actually trying out Master Pages now, so I've exposed the menu anchors
to the server, and I'm setting the class of a appropriate anchor in the
content page's page_load().

If this doesn't work out, I'll probably go back to the javascript route. A
menu control certainly doesn't appeal, but I can see one of the many CSS
friendly adapters might be useful elsewhere, but are too complicated and too
much effort for this scenario. I'm afraid you can't beat HTML + CSS in this
case.
 
P

PJ6

Never mind ASP Classic - what about the rest of the web? The .NET paradigm
only works for .NET, so I need to consider whether it is worth learning
the 'official' Microsoft way, or picking and choosing the tools and
techniques as I see fit.

Huh? I thought this was the Microsoft aspnet group. If you want to not use
ASP.NET for what it brings to the table - its web object model - than why
bother using it at all?
For example, should I shun HTML as you suggest and use .NET to produce
cloned IE-centric tag soup? Yes, I know the .NET framework has come on a
long way from Frontpage/Word in terms of HTML, but it's still far from
perfect. It's still IE-centric and IE 7 still isn't standards compliant...

The ASP.NET web object model does not create "tag soup", and it is not
IE-centric; you would know this if you had spent even a few minites with it.
What you see in the forms designer is entirely different from the HTML these
classes generate when rendering a page at runtime. I personally don't
recommend using the forms designer at all.
As for using a Panel (containg .NET controls), doesn't this rather defeat
the object... I'm templating my system for superior performance and ease
of development and maintenance. Sure, I could try to assemble building
blocks using a Panel for a wrapper, but I gain nothing and don't achieve
my original aims. Or am I missing something?

Generating HTML from objects is by far the highest-performing design. You
wouldn't think so, but it's true - it's faster than putting HTML directly on
a page. You also gain a smaller code base and reuasable components. I
definitely think you are missing something.
I'm not dismissing your suggestions outright, but I remain to be
convinced. And frankly, to suggest that we need to move even further away
from HTML is an absurd suggestion. The web is awash with 'developers' with
no real skill or knowledge but a copy of Dreamweaver or VS.NET - we need
more people who can code in HTML before they do anything else.

You're right - the web is awash with developers with no real skill; most do
not understand object-oriented design. If you were local to Boston I would
sit you down and take the time show you why in real applications -
especially reusable components such as controls - HTML should be rendered
with classes and not coded by hand. "coding in HTML" is not really coding at
all.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top