Saturday, March 31, 2012

Setting ItemStyle CssClass programmatically

I would like to have something like this;
<asp:TemplateColumn ...>
<ItemStyle CssClass='<%# DataBinder.Eval(Container,"DataItem.Style")%>'>
</ItemStyle>
<ItemTemplate>
..
</ItemTemplate>
</asp:TemplateColumn>
Where I get the style for the TemplateColumn from the datasource (or compute
it based on the datasource)
It doesn't appear that I can set the CssClass attribute in this manner.
I'm not sure why it should be different from, for example, the Text attribut
e
of an asp:Label.
Thanks,
BradBrad,
It's a matter of how they are implemented, the *Style are simply properties
of the Grid, whereas the *Templates are actulaly templates - which is what
allows you to have binding information on then. In other words, the
ItemStyle applies to all rows of the grid, not just the current one being
binded.
If this is important, one thing you might look at is using a Repeater.
You'll have more control over what's being rendered, for instance, since the
TR isn't automatically generated, you need to put it in your ItemTemplate,
which should allow what you need to do:
<asp:repeater ...>
<headerTemplate>
<table>
</headerTemplate>
<itemTemplate>
<tr class='<%# DataBinder.Eval(Container,"DataItem.Style")%>'>
<td>blah</td>
</tr>
</itemTemplate>
<footerTemplate>
</table>
</footerTemplate>
</asp:repeater>
"Brad Quinn" <BradQuinn@.discussions.microsoft.com> wrote in message
news:358028B3-2484-4063-93F6-D2C3664F861F@.microsoft.com...
> I would like to have something like this;
> <asp:TemplateColumn ...>
> <ItemStyle CssClass='<%# DataBinder.Eval(Container,"DataItem.Style")%>'>
> </ItemStyle>
> <ItemTemplate>
> ...
> </ItemTemplate>
> </asp:TemplateColumn>
> Where I get the style for the TemplateColumn from the datasource (or
compute
> it based on the datasource)
> It doesn't appear that I can set the CssClass attribute in this manner.
> I'm not sure why it should be different from, for example, the Text
attribute
> of an asp:Label.
> Thanks,
> Brad

0 comments:

Post a Comment