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

Discussion in 'Microsoft ASP .NET' started by moondaddy, Mar 30, 2004.

  1. moondaddy

    moondaddy Guest

    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?

    --
     
    moondaddy, Mar 30, 2004
    #1
    1. Advertisements

  2. moondaddy

    Cliff Harris Guest

    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

    "moondaddy" <> wrote in message
    news:%23YM$...
    > 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?
    >
    > --
    >
    >
    >
     
    Cliff Harris, Mar 30, 2004
    #2
    1. Advertisements

  3. moondaddy

    moondaddy Guest

    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")


    --

    "moondaddy" <> wrote in message
    news:%23YM$...
    > 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?
    >
    > --
    >
    >
    >
     
    moondaddy, Mar 30, 2004
    #3
  4. moondaddy

    Cliff Harris Guest

    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" <> wrote in message
    news:...
    > 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")
    >
    >
    > --
    >
    > "moondaddy" <> wrote in message
    > news:%23YM$...
    > > 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?
    > >
    > > --
    > >
    > >
    > >

    >
    >
     
    Cliff Harris, Mar 30, 2004
    #4
  5. moondaddy

    moondaddy Guest

    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.

    --

    "Cliff Harris" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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")
    > >
    > >
    > > --
    > >
    > > "moondaddy" <> wrote in message
    > > news:%23YM$...
    > > > 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?
    > > >
    > > > --
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    moondaddy, Apr 2, 2004
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: Microsoft ASP .NET
    Replies:
    1
    Views:
    642
    Alphonse Giambrone
    Jun 28, 2003
  2. moondaddy
    Replies:
    4
    Views:
    227
    Steven Cheng[MSFT]
    Jul 27, 2004
  3. Andrew Fisher

    Can't get application obj to work in a code behind class

    Andrew Fisher, Jan 27, 2005, in forum: Microsoft ASP .NET
    Replies:
    4
    Views:
    193
    =?ISO-8859-1?Q?Anders_Nor=E5s?=
    Jan 31, 2005
  4. Daniel Manes

    Code-Behind Pain in the Behind!

    Daniel Manes, Jun 10, 2005, in forum: Microsoft ASP .NET
    Replies:
    11
    Views:
    416
  5. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: Microsoft ASP .NET
    Replies:
    2
    Views:
    312
    Guest
    Feb 19, 2007
Loading...

Share This Page