根据Repeater中的if隐藏ASP.net中的元素 [英] Hide an element in ASP.net based on an if inside a Repeater

查看:55
本文介绍了根据Repeater中的if隐藏ASP.net中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用包裹在<%#标记中的简单If语句来隐藏某些内容,但是当我需要访问Container.DataItem时,我不知道如何在中继器中执行此操作,因为我需要当前被重复"的数据项

I know how to use a simple If statement wrapped in the <%# tags to hide something, but I don't know how to do it in a repeater when I need to access Container.DataItem, as in I need the dataItem currently being 'repeated'

例如

if (CurrentValidationMessage.Link != "")
{
  show a hyperlink
}

标记:

<asp:Repeater ID="repValidationResults" runat="server">
        <HeaderTemplate>
        </HeaderTemplate>
        <ItemTemplate>
            <a href='<%# ((MttImportValidationMessage)Container.DataItem).EditLink  %>'> Link to erroneous Milestone </a>
            <%# ((MttImportValidationMessage)Container.DataItem).Message %>
            <br />
        </ItemTemplate>
    </asp:Repeater>

推荐答案

如果仅使用id和runat ='server'标记转发器中的控件,并使用e引用ItemDataBound事件中的DataItem,则可能更易于维护..Item.DataItem.然后使用e.Item.FindControl引用ItemTemplate中的控件并执行逻辑.

It might be more maintainable if you just tagged the controls in the repeater with id's and runat='server' and reference the DataItem in the ItemDataBound event by using e.Item.DataItem. Then use e.Item.FindControl to reference your controls in the ItemTemplate and perform your logic.

protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        Domain.Employee employee = (Domain.Employee)e.Item.DataItem;
        Control myControl = (Control)e.Item.FindControl("controlID");
        //Perform logic
    }
}

这篇关于根据Repeater中的if隐藏ASP.net中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆