Dynamically creating CSS

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

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.
 
P

Peter Rilling

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

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top