Applying Style Objects to Controls

J

Jesse Sweetland

I am writing a custom control that is essentially a scrollable data
grid with some other custom functionality. I would like to be able to
specify CSS styles for three row categories: Heading Row, Normal Row,
and Alternating Row. Subsequently, I have added three read-only
properties of type System.Web.UI.WebControls.Style named HeadingStyle,
RowStyle, and AlternatingRowStyle, respectively.

My preference would be to usee CssStyleCollection for the following
reasons:

1. The packaged WebControls use them, and I would like to be
consistent
2. CssStyleCollection keys can be iterated over and applied
generically

I have the option of using a dummy HtmlGenericControl and using it's
Style (CssStyleCollection) property as a means of storing the
CssStyleCollection behind each of my style properties (since I can't
instantiate a CssStyleCollection) but I chose not to because:

1. The IDE could not figure out what to do with properties of type
CssStyleCollection (there is obviously some undocumented attribute I'm
missing, since the WebControls use it just fine)
2. There is no way to specify these values at design-time
3. The IDE handles Style properties just fine

So, I have 3 Style objects. The CssStyleCollection has a Keys property
and an indexer that will allow you to iterate over all of the defined
styles. Style has nothing of the sort. It seems that in order to apply
the values in Style you have to implement something like the
following:

if( ! style.BackColor.Equals( Color.Empty ) )
{
myControl.Style["background"] = style.BackColor.Name;
}

// etc.

This seems really tedious. Is there a better way to do this? There is
a method (ApplyAttributesToRender) that takes an HtmlTextWriter object
as a parameter. How does this method work? Is this what I need? Am I
going about this completely wrong?

Thanks,

- Jesse
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top