3

I'm binding my DataRepeater control to a table that has many columns. I'd like to only display a subset of those, depending on what is populated.

How/where should I do my contitional tests within a dataRepeater? This is the code within my itemtemplate:

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

The error I get is: CS0103: The name 'Container' does not exist in the current context

makerofthings7
  • 60,103
  • 53
  • 215
  • 448

1 Answers1

5

You should be fine with this:

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

But depending on what you want to do, I would probably write a function to handle the binding of the data in order to retain separation between display and business logic.

e.g.

in your 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>

in your code-behind:

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;
    }
  }
}

personally, I prefer to do things this way rather than doing any logic inside the ASP. I know that it might seem a bit silly to some people, but I like to keep my business logic separate from my display logic whereever possible.

samandmoore
  • 1,221
  • 2
  • 15
  • 23
  • @samandmoore No luck getting DatBinder.Eval to work as you describe. It requires two objects in the method call. – makerofthings7 Aug 12 '10 at 05:09
  • fyi My repeater has some HTML that I'd like to conditionally display. "i" tracks the number of items that are displayed per item. So, for example if lines 3 through 5 are null, then I have room to display lines 6 though 9. – makerofthings7 Aug 12 '10 at 05:10
  • @MakerOfThings7 I fixed up the first part. it works fine with just `Eval("first")` for me. try that. – samandmoore Aug 12 '10 at 13:06
  • @samandmoore I now get the exception: Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. – makerofthings7 Aug 12 '10 at 13:38
  • I have a sample here: http://www.perfmon.com/download/ContactsGrid_Null_DataRepeater.zip ... just be sure remove the "callback" control within the usercontrol; you probably don't have that object installed – makerofthings7 Aug 12 '10 at 13:44
  • The only problem I have with that solution is that I can't control the rendering of "
    " if the label is blank. All the extra DIV's with null data will render and mess up my format. I'm shooting for a dynamic format. I *DO* like the separation of data you're doing :)
    – makerofthings7 Aug 12 '10 at 14:33
  • Due to the simplicity of my need, i went with a lit control, strinbuilder and string formatting. Going back to asp.net from MVC is tough :P Everything is so difficult, even simple things. The new databinding in asp.net 4.5 will solve a lot of that though. – Moulde Jun 06 '12 at 10:56