Xaphas
Xaphas

Reputation: 519

aspx server tag routine doesn't work

I'm currently working on a little website where I am trying to dynamically set css tags. Looks something like this until now:

<li <% if (%>
<%#Eval("PAGE_LINK").ToString()%>
<%Equals("/Account/OverView")) {%> class="active"
<%} else { } %>>
<a href="<%# Eval("PAGE_LINK") %>"> <asp:Label runat="server" ID="Label1"> <%# Eval("OPTION_NAME") %></asp:Label></a></li>

The error looks like this: "CS1002: ; expected" It appears on line 3. I've spent like four hours with this statement. What am I doing wrong? Visual Studio isn't complaining about wrong syntax...

Upvotes: 0

Views: 54

Answers (2)

Xaphas
Xaphas

Reputation: 519

Finally got it to work. I still don't know why it crashed, but I decided to do it in the code-behind. The OnItemDataBound event did the trick. Looks as the following now:

    <!-- Pills Sub-Menu -->
<div class="container">
    <ul id="pilllisthtml" runat="server" class="nav nav-pills nav-justified">
        <asp:ListView runat="server" ID="PillList" OnItemDataBound="PillList_ItemDataBound">
            <ItemTemplate>
                <li id="myLI" runat="server">
                    <a href="<%# Eval("PAGE_LINK") %>">
                        <asp:Label runat="server" ID="Label1"> <%# Eval("OPTION_NAME") %></asp:Label></a></li>
            </ItemTemplate>
        </asp:ListView>
    </ul>
</div>

Code-Behind:

        protected void PillList_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        CustomerPortalDataBase.VwOptions it = (CustomerPortalDataBase.VwOptions)e.Item.DataItem;

        HtmlGenericControl bubu = (HtmlGenericControl)e.Item.FindControl("myLI");

        if (it.PAGE_LINK == "/Account/OverView")
        {
            bubu.Attributes["class"] = bubu.Attributes["class"] + " active";
        }



    }

Upvotes: 0

VDWWD
VDWWD

Reputation: 35554

If you want to add a class based on an IF statement, the easiest is a ternary operator inside your GridView or Repeater.

<li class="<%# Eval("PAGE_LINK").ToString() == "/Account/OverView" ? "active" : "" %>">
    <a href="<%# Eval("PAGE_LINK") %>"><%# Eval("OPTION_NAME") %></a>
</li>

And if you really need that Label, you better bind to the Text property directly.

<li class="<%# Eval("PAGE_LINK").ToString() == "/Account/OverView" ? "active" : "" %>">
     <a href="<%# Eval("PAGE_LINK") %>"><%# Eval("OPTION_NAME") %></a>
     <asp:Label ID="Label1" runat="server" Text='<%# Eval("OPTION_NAME")%>'></asp:Label>
</li>

You could even use a HyperLink control.

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("PAGE_LINK") %>' 
    Text='<%# Eval("OPTION_NAME") %>'></asp:HyperLink>

Upvotes: 1

Related Questions