Using Container.DataItem with If statement inside <%%>

I have the following code in a C # aspx page:

<ItemTemplate>
    <a <% if(((Dictionary<string, string>)Container.DataItem)["type"]==Session["type"]){%> class="active"<%}%>

This code causes the following error.

Compiler Error Message: CS0117: 'System.ComponentModel.Container' does not contain a definition for 'DataItem'

Why is this and how can I make a conditional statement that uses Container.DataItem? Container.DataItemworks fine if used in <%# %>, however including the operator ifin <%# %>causes the following error:

Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct
+3
source share
1 answer

You might have something like this


<ItemTemplate>
<%# ((Dictionary<string, string>)Container.DataItem)["type"].Equals(Session["type"]) ? 
"<a class='active'>mylink</a>" : 
"<a>mylink</a>" %>

or


<ItemTemplate>
<a class='<%# ((Dictionary<string, string>)Container.DataItem)["type"].Equals(Session["type"]) ? 
"active" : string.Empty" %>'>my link </a>

EDIT Added equalities to solution

+3
source

Source: https://habr.com/ru/post/1743390/


All Articles