views:

66

answers:

1

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

A: 

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
@samandmoore No luck getting DatBinder.Eval to work as you describe. It requires two objects in the method call.
MakerOfThings7
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
@MakerOfThings7 I fixed up the first part. it works fine with just `Eval("first")` for me. try that.
samandmoore
@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
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
alrighty, check out my answer now. that's how i would do it.
samandmoore
The only problem I have with that solution is that I can't control the rendering of "<div style="background-color:#C5CED8;clear:both">" 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