3

我在 ac# aspx 页面中有以下代码:

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

此代码导致以下错误。

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

为什么会这样,我怎样才能做出使用的条件语句Container.DataItemContainer.DataItem在 a 中使用时效果很好,<%# %>但是将if语句放入<%# %>会导致以下错误:

Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct
4

1 回答 1

4

你可以有这样的东西


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

或者


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

编辑 在解决方案中添加了等于

于 2010-04-29T17:21:23.150 回答