Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:21:54+00:00 2026-06-16T01:21:54+00:00

I’m trying to programmatically switch styles on a page based on the CSS class

  • 0

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" >

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:21:55+00:00Added an answer on June 16, 2026 at 1:21 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.