How to make conditional logic in an ASP.NET DataRepeater control?

I am binding my DataRepeater control to a table with many columns. I would like to display only a subset of these elements, depending on what is filled.

How / where should I do my trial tests in dataRepeater? This is the code inside 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

+3
source share
1 answer

You should be fine:

<% 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. , , - , .

+5

Source: https://habr.com/ru/post/1759256/


All Articles