Search code examples
asp.netvb.netwebformsweb-controls

ASP.NET inline expressions not working in CssClass attribute?


I have the following code:

<asp:LinkButton runat="server" 
                CommandName="SwitchStep"
                CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                CssClass="<%# some conditional code here %> activestep">
    Step <%# Container.ItemIndex + 1 %>: <%# DataBinder.Eval(Container.DataItem, "ID")%>
</asp:LinkButton>

Inline statements work in the CommandArgument attribute, and I know they work in the text attribute. For some reason, though, in the CssClass attribute, the inline statement ends up (unparsed) in the HTML output! What the hell?

In Chrome:

<a class="&lt;%= 'steptab' %&gt;" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("StepControl:_ctl1:_ctl0", "", true, "", "", false, true))'>
            Step 1: section_name</a>

Has anyone run into this before? I'm not sure why this shouldn't work. It doesn't seem logical, and I'm a little frustrated.

Some notes:

  • If I put a character in here that doesn't belong, such as "?" (this is VB), the compiler complains.
  • Any server tag (<%, <%#, etc) shows up in the HTML.
  • This is inside a repeater control. Wait, that's in the code.
  • If I remove the "activestep" class after the inline statement, the inline statement still appears, although at one time I didn't have a class attribute appearing in HTML at all.

Any ideas? Thanks for your help!


Solution

  • I am not sure if you can write an Expression in CssClass property. Try this:

    <asp:LinkButton runat="server" 
                    CommandName="SwitchStep"
                    CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                    CssClass="<%$ Iif(condition, "activestep", "") %>">
        Step <%# Container.ItemIndex + 1 %>: <%# DataBinder.Eval(Container.DataItem, "ID")%>
    </asp:LinkButton>
    

    On the other hand, you could use ItemDataBound event handler to wrapper your condition and set the CssClass property on the control inside your Repeater. Take a look

    In the WEbForm, add a reference to the ItemDataBound

    <asp:Repeater id="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server">
    ...
    </asp:Repeater>
    

    VB.NET

    ' This event is raised for the header, the footer, separators, and items.
    Public Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) 
    
        ' Execute the following logic for Items and Alternating Items.
        if e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
    
            Dim p as Product = CType(e.Item.DataItem, Product) ' cast to your entity just a sample
    
            If p.Active Then ' check some condition     
    
                CType(e.Item.FindControl("Your_LinkButtonId"), LinkButton).CssClass = "activestep"
    
            End If
        End if
    
    End Sub
    

    C#

    // This event is raised for the header, the footer, separators, and items.
    public void Repeater1_ItemDataBound(Object sender, RepeaterItemEventArgs e) 
    {
        // Execute the following logic for Items and Alternating Items.
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
        {
            Product p = (Product)e.Item.DataItem; // cast to your entity just a sample
    
            if (p.Active) // check some condition
            {
                ((LinkButton)e.Item.FindControl("Your_LinkButtonId")).CssClass = "activestep";
            }
        }  
    }