Programmatically adding a css style to a web content form

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, May 5, 2008.

  1. I have css that would normally be placed in style tags in the header of the
    Master page that I want to add programmatically for a specific Web Content
    Form (the *.aspx page). How do I do this for a Web Content Form? I cannot
    use style tags in a Web Content Form, and I am having trouble figuring out
    how to add all the desired css properties to a
    System.Web.UI.WebControls.Style object. Can anyone help me? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, May 5, 2008
    #1
    1. Advertising

  2. Hello Nathan

    >I have css that would normally be placed in style tags in the header of the
    >Master page that I want to add programmatically for a specific Web Content
    >Form (the *.aspx page). How do I do this for a Web Content Form? I cannot
    >use style tags in a Web Content Form, and I am having trouble figuring out
    >how to add all the desired css properties to a
    >System.Web.UI.WebControls.Style object. Can anyone help me? Thanks.

    You can Access within your contentpage to the masterpage and throught the
    property ".Master"
    also to the .Header.

    Add there a HtmlGenericControl("style"), or make an Include with an "link"
    Tag there.

    Look further:

    -
    http://translate.google.ch/translat...-direktive.aspx&langpair=de|en&hl=de&ie=UTF-8
    (English Translation)

    -
    http://www.aspnetzone.de/blogs/pete...asterpage-zugreifen-mastertype-direktive.aspx
    (Original)

    --
    Gruss, Peter Bucher
    Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
    Peter Bucher [MVP], May 5, 2008
    #2
    1. Advertising

  3. Nathan Sokalski

    Stan Guest

    > ... and I am having trouble figuring out
    > how to add all the desired css properties to a
    > System.Web.UI.WebControls.Style object. Can anyone help me? Thanks.



    The "Style" property of a web server control is actually a collection
    not a single object (really ought to be named "Styles"). So you use
    the Add method. CssStyleCollections are actually a list of key/value
    pairs where the key is the attribute and the value the value. For
    example:

    Suppose you have a label control where you want to set the styles
    programmatically:

    Label1.Style.Add("Color", "Blue");
    Label1.Style.Add("font-size", "2em");

    will make the text quite large and blue.

    HTH
    Stan, May 8, 2008
    #3
  4. I am not talking about the Style property of a web server control, I am
    talking about a Web.UI.WebControls.Style object, which does not have an Add
    method. You seem to have the Style object confused with the
    CssStyleCollection object. These two object can be easy to confuse because
    the Style property is not an instance of the Style class. Any other
    suggestions?
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Stan" <> wrote in message
    news:...
    >> ... and I am having trouble figuring out
    >> how to add all the desired css properties to a
    >> System.Web.UI.WebControls.Style object. Can anyone help me? Thanks.

    >
    >
    > The "Style" property of a web server control is actually a collection
    > not a single object (really ought to be named "Styles"). So you use
    > the Add method. CssStyleCollections are actually a list of key/value
    > pairs where the key is the attribute and the value the value. For
    > example:
    >
    > Suppose you have a label control where you want to set the styles
    > programmatically:
    >
    > Label1.Style.Add("Color", "Blue");
    > Label1.Style.Add("font-size", "2em");
    >
    > will make the text quite large and blue.
    >
    > HTH
    >
    Nathan Sokalski, May 9, 2008
    #4
  5. Nathan Sokalski

    Stan Guest

    On May 9, 1:06 am, "Nathan Sokalski" <> wrote:
    > I am not talking about the Style property of a web server control, I am
    > talking about a Web.UI.WebControls.Style object, which does not have an Add
    > method. You seem to have the Style object confused with the
    > CssStyleCollection object. These two object can be easy to confuse because
    > the Style property is not an instance of the Style class. Any other
    > suggestions?
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/
    >
    > "Stan" <> wrote in message
    >
    > news:...
    >
    >
    >
    > >> ...  and I am having trouble figuring out
    > >> how to add all the desired css properties to a
    > >> System.Web.UI.WebControls.Style object. Can anyone help me? Thanks.

    >
    > > The "Style" property of a web server control is actually a collection
    > > not a single object (really ought to be named "Styles"). So you use
    > > the Add method. CssStyleCollections are actually a list of key/value
    > > pairs where the key is the attribute and the value the value. For
    > > example:

    >
    > > Suppose you have a label control where you want to set the styles
    > > programmatically:

    >
    > >        Label1.Style.Add("Color", "Blue");
    > >        Label1.Style.Add("font-size", "2em");

    >
    > > will make the text quite large and blue.

    >
    > > HTH- Hide quoted text -

    >
    > - Show quoted text -


    Ok, I misread your post slightly. Can I take it then that you are
    trying encapsulate a set of css styles in an instance of
    System.Web.UI.WebControl.Style class so it can be applied to any
    control without having to set the properties for each control
    individually? i.e. you are trying to emulate the way css "class"
    attributes work without having to access the <style> tags.

    I have tested the following which works OK
    (note that System.Web.UI.WebControl namespace is within scope)

    Style s = new Style();
    s.Font.Size = FontUnit.Parse("2em");
    s.ForeColor = System.Drawing.Color.Blue;
    Label1.ControlStyle.CopyFrom(s);

    Is that anything like what you are trying to do?

    Note also that the Style object does not encapsulate all possible css
    styles, e.g. it doesn't handle things like "margin" or "padding"
    However the Style object does have a CssClass property that can be
    used in the same way as those illustrated above except that the
    definition of the css class has to reside in markup in the usual way.

    Is there any reason why you can't use themes for all this, which allow
    the selected application of both Skin and CSS style sheets, and can be
    applied at page level using the @page directive (including content
    pages) and hence at control level with css classes and Skin IDs?
    Stan, May 10, 2008
    #5
  6. Thank you for that information. I think the basic scenario of my situation
    is that I am using Master/Content pages, and the CSS class I want will only
    be used in one content page. However, the CSS class will not always be the
    same, so it must be generated, so I cannot place it in the HTML or an
    external stylesheet like you normally would.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Stan" <> wrote in message
    news:...
    On May 9, 1:06 am, "Nathan Sokalski" <> wrote:
    > I am not talking about the Style property of a web server control, I am
    > talking about a Web.UI.WebControls.Style object, which does not have an
    > Add
    > method. You seem to have the Style object confused with the
    > CssStyleCollection object. These two object can be easy to confuse because
    > the Style property is not an instance of the Style class. Any other
    > suggestions?
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/
    >
    > "Stan" <> wrote in message
    >
    > news:...
    >
    >
    >
    > >> ... and I am having trouble figuring out
    > >> how to add all the desired css properties to a
    > >> System.Web.UI.WebControls.Style object. Can anyone help me? Thanks.

    >
    > > The "Style" property of a web server control is actually a collection
    > > not a single object (really ought to be named "Styles"). So you use
    > > the Add method. CssStyleCollections are actually a list of key/value
    > > pairs where the key is the attribute and the value the value. For
    > > example:

    >
    > > Suppose you have a label control where you want to set the styles
    > > programmatically:

    >
    > > Label1.Style.Add("Color", "Blue");
    > > Label1.Style.Add("font-size", "2em");

    >
    > > will make the text quite large and blue.

    >
    > > HTH- Hide quoted text -

    >
    > - Show quoted text -


    Ok, I misread your post slightly. Can I take it then that you are
    trying encapsulate a set of css styles in an instance of
    System.Web.UI.WebControl.Style class so it can be applied to any
    control without having to set the properties for each control
    individually? i.e. you are trying to emulate the way css "class"
    attributes work without having to access the <style> tags.

    I have tested the following which works OK
    (note that System.Web.UI.WebControl namespace is within scope)

    Style s = new Style();
    s.Font.Size = FontUnit.Parse("2em");
    s.ForeColor = System.Drawing.Color.Blue;
    Label1.ControlStyle.CopyFrom(s);

    Is that anything like what you are trying to do?

    Note also that the Style object does not encapsulate all possible css
    styles, e.g. it doesn't handle things like "margin" or "padding"
    However the Style object does have a CssClass property that can be
    used in the same way as those illustrated above except that the
    definition of the css class has to reside in markup in the usual way.

    Is there any reason why you can't use themes for all this, which allow
    the selected application of both Skin and CSS style sheets, and can be
    applied at page level using the @page directive (including content
    pages) and hence at control level with css classes and Skin IDs?
    Nathan Sokalski, May 10, 2008
    #6
    1. Advertising

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. andy
    Replies:
    1
    Views:
    521
    David Banister
    Jul 15, 2003
  2. Chris R. Timmons
    Replies:
    0
    Views:
    655
    Chris R. Timmons
    Aug 29, 2003
  3. Replies:
    1
    Views:
    3,354
    =?Utf-8?B?RFdT?=
    Mar 22, 2006
  4. hazz
    Replies:
    6
    Views:
    49,384
    SkyUCHC
    Jun 9, 2010
  5. Nathan Sokalski
    Replies:
    5
    Views:
    1,000
    Nathan Sokalski
    May 10, 2008
Loading...

Share This Page