locked
Optional Display of item template in a repeater based on flag RRS feed

  • Question

  • User483994611 posted

    I want to make repeater item template visible optional based on a flag but its throwing error like ..

    <ItemTemplate>
    <%# if (<%# Eval("flag")%>)
    { %>

    <div>

    </div

    </ItemTemplate>

    Please help!!

    Thursday, November 16, 2017 7:08 AM

All replies

  • User-1716253493 posted

    Use inline ?: operator in C#

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator

    For example

    <div style='<%# Eval("flag")=="a" ? "display:none;" : "" %>'>
    abcdsefg
    </div>

    Thursday, November 16, 2017 8:06 AM
  • User1400794712 posted

    Hi suvo,

    According to your description, I have two ideas about it, you can take them as a reference.

    1.Set visible value according to flag's value

    For example:

    <ItemTemplate>
        <input id="ID" runat="server" class="txtFromDate" visible='<%# Eval("flag") =="true"%>' value='<%# Eval("ID") %>' type="text" />
    </ItemTemplate>

    As you can see in the picture, the second and fourth row disappear.

    2. Deal the data in code behind, add the eligible data(for example: flag=true) to a new datatable and bind this datatable to repeater. Then the repeater will only show the data, the falg of which is true.

    My demo:

    C#

    //Raw data
    DataTable dt1 = new DataTable();
    dt1.Columns.AddRange(new DataColumn[2] { new DataColumn("ID"), new DataColumn("flag") });
    dt1.Rows.Add("1", "true");
    dt1.Rows.Add("2", "false");
    dt1.Rows.Add("3", "true");
    dt1.Rows.Add("4", "false");
    dt1.Rows.Add("5", "true");
    //Add a new datatable to store the eligible data.
    DataTable dt2 = new DataTable();
    dt2.Columns.AddRange(new DataColumn[1] { new DataColumn("ID") });
    //Get the eligible data and add it to dt2
    foreach (DataRow data in dt1.Rows)
    {
        if (data["flag"].ToString() == "true")
        {
            dt2.Rows.Add(data["ID"]);
        }
    }
    repeater.DataSource = dt2;
    repeater.DataBind();

    ASPX:

    <asp:Repeater ID="repeater" runat="server">
        <HeaderTemplate>
            <asp:Label ID="txtID" runat="server">ID</asp:Label>
        </HeaderTemplate>
        <ItemTemplate>
            <input id="ID" runat="server" class="txtFromDate" value='<%# Eval("ID") %>' type="text" />
        </ItemTemplate>
    </asp:Repeater>

    Best Regards,
    Daisy

    Friday, November 17, 2017 8:41 AM