【问题标题】:How do I do conditional logic within an ASP.NET DataRepeater control?如何在 ASP.NET DataRepeater 控件中执行条件逻辑?
【发布时间】:2010-08-12 03:10:05
【问题描述】:

我正在将我的 DataRepeater 控件绑定到一个包含许多列的表。我只想显示其中的一个子集,具体取决于填充的内容。

我应该如何/在哪里在 dataRepeater 中进行连续测试?这是我的 itemtemplate 中的代码:

<% if (0= (DataBinder.Eval(Container.DataItem, "first").ToString().Length))
{
   i++;
}
    %>

我得到的错误是:CS0103:当前上下文中不存在名称“容器”

【问题讨论】:

    标签: c# asp.net data-binding asp.net-2.0 datarepeater


    【解决方案1】:

    你应该没问题:

    <% if (0 == (Eval("first").ToString().Length))
    {
       i++;
    }
    %>
    

    但根据您想要做什么,我可能会编写一个函数来处理数据的绑定,以保持显示和业务逻辑之间的分离。

    例如

    在你的 aspx 中:

    <asp:Repeater id="myRepeater" runat="server" onDataItemBound="FillInRepeater">
    <ItemTemplate>
    <div class="contactLarge">
        <div style="background-color:#C5CED8;clear:both"><asp:Label runat="server" ID="title"></asp:Label>
        .
        .
        .
    </div>
    </ItemTemplate>
    <AlternatingItemTemplate>
    </AlternatingItemTemplate>
    </asp:Repeater>
    

    在您的代码隐藏中:

    protected void FillInRepeater(object sender, RepeaterItemEventArgs e)
    {
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
        //in here you bind to your repeater labels and stuff then do all that sorta logic.
        //Grab Primary Data
        string titleText = DataBinder.Eval(e.Item.DataItem, "title").ToString();
        string somethingElseText = DataBinder.Eval(e.Item.DataItem, "somethingElse").ToString();
        string maybeSeeMaybeDontText = DataBinder.Eval(e.Item.DataItem, "maybeSeeMaybeDont").ToString();
    
        //Find the controls and populate them according the to row
        Label titleLabel = (Label)e.Item.FindControl("title");
        Label somethingElseLabel = (Label)e.Item.FindControl("somethingElse");
        Label maybeSeeMaybeDontLabel = (Label)e.Item.FindControl("maybeSeeMaybeDont");
    
        // display the fields you want to
        titleLabel.Text = titleText;
        somethingElseLabel.Text = somethingElseText;
    
        // here is where you could do some of your conditional logic
        if (titleText.Length != 0 && somethingElseText.Length != 0)
        {
            maybeSeeMaybeDontLabel.Text = maybeSeeMaybeDontText;
        }
      }
    }
    

    就个人而言,我更喜欢以这种方式做事,而不是在 ASP 中做任何逻辑。我知道这对某些人来说可能有点傻,但我喜欢尽可能将我的业务逻辑与我的显示逻辑分开。

    【讨论】:

    • @samandmoore 没有运气让 DatBinder.Eval 像你描述的那样工作。它在方法调用中需要两个对象。
    • fyi 我的转发器有一些我想有条件地显示的 HTML。 “i”跟踪每个项目显示的项目数。因此,例如,如果第 3 到第 5 行为空,那么我有空间显示第 6 到第 9 行。
    • @MakerOfThings7 我修复了第一部分。对我来说,只需Eval("first") 就可以正常工作。试试看。
    • @samandmoore 我现在得到异常:异常详细信息:System.InvalidOperationException:Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用.
    • 我这里有一个示例:perfmon.com/download/ContactsGrid_Null_DataRepeater.zip ...请确保删除用户控件中的“回调”控件;你可能没有安装那个对象
    猜你喜欢
    • 2013-03-15
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    相关资源
    最近更新 更多