user677526
user677526

Reputation:

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:

Any ideas? Thanks for your help!

Upvotes: 1

Views: 2776

Answers (2)

Tim B James
Tim B James

Reputation: 20364

You can build up the CssClass using String.Format()

CssClass='<%# String.Format("{0} activestep", If(condition, "truestring", "falsestring"))%>'>

Upvotes: 1

Felipe Oriani
Felipe Oriani

Reputation: 38618

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

Upvotes: 0

Related Questions