【问题标题】:How to remove the current DataItem from being bound to a ASP.NET Repeater?如何从绑定到 ASP.NET 中继器中删除当前 DataItem?
【发布时间】:2012-11-13 04:29:50
【问题描述】:

我想评估当前绑定到转发器的 DataItem 并在满足我的条件时将其从添加中删除。我原以为将 e.Item.DataItem 设置为 Nothing 会起作用,但事实并非如此。任何想法如何在满足特定条件时不将 DataItem 添加到转发器?

Protected Sub rpt_OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        If true Then
            'don't add the current item
            e.Item.DataItem = Nothing
        Else
            'add the current item
        End If
    End If
End Sub

【问题讨论】:

    标签: .net asp.net vb.net repeater


    【解决方案1】:

    您是否尝试将项目的可见性设置为 False?

    If true Then
      //don't add the current item
      e.Item.Visible = False
    Else
      //add the current item
    End If
    

    【讨论】:

      【解决方案2】:

      你走错路了。在代码中的这一点上,已经添加了一个项目,您只是绑定(填充)它。您要做的是在将结果集设置为DataSource 之前过滤结果集,然后再调用repeater.DataBind

      【讨论】:

      • 不幸的是,这行不通,而这正是我真正想做的事情。我想很难解释这种情况。但是我想知道在 ItemDataBound 中做这种事情为时已晚就足够了。
      • 在那种不幸的情况下,如果我有机会,我会将整个 ItemTemplate 包装在一个面板中,当满足感兴趣的条件时,我会将面板的可见性设置为假的。
      • 当然,如果您使用AlternatingItemTemplate,例如不同的背景颜色,这种方法会搞砸。要解决这个问题,你必须有一个到目前为止实际可见的项目数量的外部计数器,并根据visibleItems%2==0 设置背景,但现在它真的开始变脏了。我希望这不是你的情况
      猜你喜欢
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      相关资源
      最近更新 更多