By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,933 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,933 IT Pros & Developers. It's quick & easy.

Dynamically creating CSS

P: n/a
I have a User Control (.ascx & .ascx.vb) for which I need to dynamically
specify certain CSS stylesheet properties. Because the controls that I use
in the .ascx file do not list style as one of their properties (they list
CssClass, but but not a place where I can enter either a string or
property/value pairs). In the past when making pages I would type the Style=
attribute even though Visual Studio doesn't list it as an available
property. I have found the Style class and the
HtmlTextWriter.AddStyleAttribute method, but all the examples I have found
override Style.AddAttributesToRender and are in a non-ascx control (they are
not in controls that are built from existing controls). Could someone send
me a basic example of how to dynamically add style properties to an .ascx
user control? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Because ASCX controls do not necessary equate to a single HTML element,
there is no way for the browser to apply the style as it one can ask, "which
element should the style be applied to?"

Having said that, you can still create a style property for your control,
and then before the control renders, just apply the style as appropriate.

For instance,

public string Style{
get{...}
set{...}
}

<uc1:MyControl ... Style="..."/>

protected void OnPreRender(...){
// use the style as you see fit.
}

(by the way, this is c#, you can apply the same concept to your VB code.)
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:u3**************@TK2MSFTNGP14.phx.gbl...
I have a User Control (.ascx & .ascx.vb) for which I need to dynamically
specify certain CSS stylesheet properties. Because the controls that I use
in the .ascx file do not list style as one of their properties (they list
CssClass, but but not a place where I can enter either a string or
property/value pairs). In the past when making pages I would type the
Style= attribute even though Visual Studio doesn't list it as an available
property. I have found the Style class and the
HtmlTextWriter.AddStyleAttribute method, but all the examples I have found
override Style.AddAttributesToRender and are in a non-ascx control (they
are not in controls that are built from existing controls). Could someone
send me a basic example of how to dynamically add style properties to an
.ascx user control? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.