How can I change an element's class in the code behind?

M

moondaddy

I have some table cells with the runat attribute set to server. In the
codebehind I can see these cells but I don't see any properties to change
their class. How can this be done from the code behind?
 
C

Cliff Harris

It looks like when you use it as a server-side control, it wants you to use
the .Style member to adjust style settings. However, if you want to "cheat"
to get it to work, just use the "Attributes" property of the control:

tablecell1.Attributes.Add("class", "MyCssClass");

That's the only way I could find to do it.

HTH,
-Cliff
 
M

moondaddy

I found a way. Please let me know if there's a better way than this.

Thanks.

'First remove the class attribute from the cell
Me.cellStatusBar1.Attributes.CssStyle.Remove("class")
'Now add it back in specifying which class to use
Me.cellStatusBar1.Attributes.Add("class", "clsChkOutBar_NormalText")
 
C

Cliff Harris

The first of your two statement has an error...
Me.cellStatusBar1.Attributes.CssStyle.Remove("class")
will remove a style attribute.. for example.. if my tag in HTML looks like
this

....
<td id="cell1" style="color:red" runat="server">
....

and I have this in my codebehind
cell1.Attributes.CssStyle.Remove("color");

the following tag would be produced on the client:
....
<td id="cell1" style="" runat="server">
....

The reason it still works for you is because, as I tested, if you add an
attributes (in your case the "class" attribute) and it already exists, it is
overridden. If you want to still remove it, you would just modify it like
this:

'First remove the class attribute from the cell
Me.cellStatusBar1.Attributes.Remove("class")
'Now add it back in specifying which class to use
Me.cellStatusBar1.Attributes.Add("class", "clsChkOutBar_NormalText")

-Cliff

PS Sorry about the mix of C# and VB... I'm sure you still get it...


moondaddy said:
I found a way. Please let me know if there's a better way than this.

Thanks.

'First remove the class attribute from the cell
Me.cellStatusBar1.Attributes.CssStyle.Remove("class")
'Now add it back in specifying which class to use
Me.cellStatusBar1.Attributes.Add("class", "clsChkOutBar_NormalText")
 
M

moondaddy

Thanks that helps to clear it up. In short what I wanted to do was to
change from one style class to another. I thought that if I didn't remove
it first I would get a conflict or have it in there 2 times, but since it
gets overridden, then removing it is unnecessary.

--
(e-mail address removed)
Cliff Harris said:
The first of your two statement has an error...
Me.cellStatusBar1.Attributes.CssStyle.Remove("class")
will remove a style attribute.. for example.. if my tag in HTML looks like
this

...
<td id="cell1" style="color:red" runat="server">
...

and I have this in my codebehind
cell1.Attributes.CssStyle.Remove("color");

the following tag would be produced on the client:
...
<td id="cell1" style="" runat="server">
...

The reason it still works for you is because, as I tested, if you add an
attributes (in your case the "class" attribute) and it already exists, it is
overridden. If you want to still remove it, you would just modify it like
this:

'First remove the class attribute from the cell
Me.cellStatusBar1.Attributes.Remove("class")
'Now add it back in specifying which class to use
Me.cellStatusBar1.Attributes.Add("class", "clsChkOutBar_NormalText")

-Cliff

PS Sorry about the mix of C# and VB... I'm sure you still get it...
 

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