Alex Dn
Alex Dn

Reputation: 5553

ASP.NET Custom web control styles Render style rules

I have a custom web control that inherits from ASP.NET button control. In Render method override, I rendering my own HTML output (div). The question is how I can render style attributes for that control in most correct way.

I can simply iterate over the Style collection in for / foreach loop and write it to HTMLTextWriter, but maybe .NET has a build in method that writes the style definition to htmlwriter ?

Thanks

Upvotes: 0

Views: 1001

Answers (2)

Alex Dn
Alex Dn

Reputation: 5553

The solution is to use the Value property of CssStyleCollection object in ASP.NET WebControl. http://msdn.microsoft.com/en-us/library/system.web.ui.cssstylecollection.value.aspx

Upvotes: 0

James Johnson
James Johnson

Reputation: 46077

It sounds like what you want is the ApplyStyle() method:

var style = new Style();
style.ForeColor = System.Drawing.Color.Red;
style.BackColor = System.Drawing.Color.Yellow;

var ctrl = new WebControl();
ctrl.ApplyStyle(style);

Upvotes: 0

Related Questions