Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Container.DataItem with an If statement within <% %>

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 is causing the following error.

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

Why is that and how can I make a conditional statement that uses the Container.DataItem? Container.DataItem works perfectly when used within a <%# %> however putting the if statement within the <%# %> causes the following error:

Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct
like image 929
William Calleja Avatar asked Apr 07 '26 10:04

William Calleja


1 Answers

You could 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 the Equals to the solution

like image 54
Claudio Redi Avatar answered Apr 08 '26 22:04

Claudio Redi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!