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

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




    Please help!!

    Thursday, November 16, 2017 7:08 AM

All replies

  • User-1716253493 posted

    Use inline ?: operator in C#


    For example

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

    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:

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

    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:


    //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")
    repeater.DataSource = dt2;


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

    Best Regards,

    Friday, November 17, 2017 8:41 AM