Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it impossible to declaratively data bind the CssClass property on ItemStyle inside GridView?

I'm trying to programmatically switch styles on a page based on the CSS class of elements inside a GridView. I've done this in several normal (non-templated) elements' class attributes successfully, but it doesn't seem to work inside template controls for some reason. Visual Studio treats the data binding code as opaque text (and in fact, even colors it accordingly as an opaque string). Is databinding not supported in this way for and ?

I can't find anything in the class documentation that says this is the case, but on the other hand it doesn't work and Visual Studio seems to be hinting that this is as-designed.

edit: an example. Here, a Gridview is declared, and in the column definition, the ItemStyle-CssClass attribute is set to two values; one which is hardcoded and the other is taken from the current value of an enum variable. Assume that in this case, StyleHelper is a static class, the Festivity property is of type StyleSelectorEnum, and the current value is StyleSelectorEnum.PartyMode.

<asp:GridView runat="server">
    <Columns> 
        <asp:TemplateField ItemStyle-CssClass="style1 <%= Lib.Web.StyleHelper.Festivity.ToString() %>" />
            <ItemTemplate>
                <div />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

In this example, the table that's generated will contain rows with td entries that appear as such:

<td class="style1 <%= Lib.Web.StyleHelper.Festivity.ToString() %>" >

when in practice, I would have hoped it would look like this:

<td class="style1 PartyMode" >

like image 704
bwerks Avatar asked Dec 20 '25 13:12

bwerks


1 Answers

I found what I was looking for. It comes almost entirely from this article that discusses ASP.NET Expression Builders using the <%$ %> construct. In the context of my example, the solution is as follows.

The markup:

<asp:GridView runat="server">
    <Columns> 
        <asp:TemplateField>
            <ItemStyle CssClass='<%$ Code: "style1 " + Lib.Web.StyleHelper.Festivity.ToString() %>' />
            <ItemTemplate>
                <div />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

Things to note:

  • The ItemStyle-CssClass property on ItemTemplate may not be used in this manner. Literal content errors result.
  • The <%$ %> construct must fill the whole value, which mean in my case doing a string construction inside of it, rather than "style1 <%$ Code: ... %>"
  • Single quotes must be used for the value in this case because of the double quotes used in string construction.

The section to declare the expression builder in web.config:

<compilation debug="true">
    <expressionBuilders>
        <add expressionPrefix="Code" type="CodeExpressionBuilder"/>
    </expressionBuilders>
</compilation> 

The class definition itself, which is placed in app_code:

[ExpressionPrefix("Code")]
public class CodeExpressionBuilder : ExpressionBuilder
{
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
    {
        return new CodeSnippetExpression(entry.Expression);
    }
}

Now, the td elements in the resulting table are given the class "style1 PartyMode" as intended.

Full credit for this solution goes to InfinitiesLoop from his blog; I did very little work to adapt this for my purposes.

like image 160
bwerks Avatar answered Dec 22 '25 03:12

bwerks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!