locked
Only render HTML if database value is x RRS feed

  • Question

  • User-1826049516 posted

    Hey,

    I use a repeater. I want to render part of the html only if the current value is not x:

    Repeater
    ...ItemTemplate
    ......<div>1</div> <--- show for all records
    ......If db value is not x
    .........<div>2</div> <--- only show if not x
    ......End If
    ...End ItemTemplate
    End Repeater

    Off the top of my head, I can change the 2nd <div> to a <Panel>with runat=server, and have Visible=true. In code behind, check the current value and if x set the Panel to Visible=false

    Is that the best way?

    Thanks

    Tuesday, February 13, 2018 3:22 PM

Answers

  • User-166373564 posted

    Hi ldoodle, 

    It is actually good way to use ItemDataBound to set the visibility. 

    <table>
               <tr>
                   <th>
                       Id
                   </th>
                   <th>
                       Name
                   </th>
                   <th>
                       Marks1
                   </th>
                   <th>
                       Marks2
                   </th>
                   <th id="marks" runat="server">
                       Matks3
                   </th>
               </tr>
               <asp:Repeater ID="rptDemo" runat="server" OnItemDataBound="ItemDataBound">
                   <ItemTemplate>
                       <tr>
                           <td>
                               <asp:Label ID="lblId" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID")%>'></asp:Label>
                           </td>
                           <td>
                               <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name")%>'></asp:Label>
                           </td>
                           <td>
                               <asp:Label ID="lblMarks1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Marks1")%>'></asp:Label>
                           </td>
                           <td>
                               <asp:Label ID="lblMarks2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Marks2")%>'></asp:Label>
                           </td>
                           <td>
                               <asp:Label ID="lblMarks3" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Marks3")%>'></asp:Label>
                           </td>
                       </tr>
                   </ItemTemplate>
               </asp:Repeater>
           </table>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[5]{new DataColumn("ID",typeof(int)),
                                    new DataColumn("Name",typeof(string)),
                                    new DataColumn("Marks1",typeof(int)),
                                    new DataColumn("Marks2",typeof(int)),
                                    new DataColumn("Marks3",typeof(int))});
                    dt.Rows.Add(1, "John", 85, 75, 0);
                    dt.Rows.Add(2, "Jack", 87, 74, 0);
                    dt.Rows.Add(3, "Micheal", 88, 71, 20);
                    rptDemo.DataSource = dt;
                    rptDemo.DataBind();
                }
            }
    
    protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Label activeLabel = (Label)e.Item.FindControl("lblMarks3");
                    string s = activeLabel.Text;
                    if (s == "0")
                    {
                        activeLabel.Visible = false;
                        marks.Visible = false;
                    }
                    else
                    {
                        activeLabel.Visible = true;
                        marks.Visible = true;
                    }
                }
            }
    

    Regards,

    Angie 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 21, 2018 7:30 AM

All replies

  • User2049726087 posted
    You can use repeater control's itemdatabound event to achieve this. Please refer following link for more info.
    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.110).aspx
    Tuesday, February 13, 2018 4:45 PM
  • User-1826049516 posted

    Yes, that's what I was referring to.

    Just wondered if this is the best way.

    Tuesday, February 13, 2018 5:03 PM
  • User-166373564 posted

    Hi ldoodle, 

    It is actually good way to use ItemDataBound to set the visibility. 

    <table>
               <tr>
                   <th>
                       Id
                   </th>
                   <th>
                       Name
                   </th>
                   <th>
                       Marks1
                   </th>
                   <th>
                       Marks2
                   </th>
                   <th id="marks" runat="server">
                       Matks3
                   </th>
               </tr>
               <asp:Repeater ID="rptDemo" runat="server" OnItemDataBound="ItemDataBound">
                   <ItemTemplate>
                       <tr>
                           <td>
                               <asp:Label ID="lblId" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID")%>'></asp:Label>
                           </td>
                           <td>
                               <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name")%>'></asp:Label>
                           </td>
                           <td>
                               <asp:Label ID="lblMarks1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Marks1")%>'></asp:Label>
                           </td>
                           <td>
                               <asp:Label ID="lblMarks2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Marks2")%>'></asp:Label>
                           </td>
                           <td>
                               <asp:Label ID="lblMarks3" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Marks3")%>'></asp:Label>
                           </td>
                       </tr>
                   </ItemTemplate>
               </asp:Repeater>
           </table>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[5]{new DataColumn("ID",typeof(int)),
                                    new DataColumn("Name",typeof(string)),
                                    new DataColumn("Marks1",typeof(int)),
                                    new DataColumn("Marks2",typeof(int)),
                                    new DataColumn("Marks3",typeof(int))});
                    dt.Rows.Add(1, "John", 85, 75, 0);
                    dt.Rows.Add(2, "Jack", 87, 74, 0);
                    dt.Rows.Add(3, "Micheal", 88, 71, 20);
                    rptDemo.DataSource = dt;
                    rptDemo.DataBind();
                }
            }
    
    protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Label activeLabel = (Label)e.Item.FindControl("lblMarks3");
                    string s = activeLabel.Text;
                    if (s == "0")
                    {
                        activeLabel.Visible = false;
                        marks.Visible = false;
                    }
                    else
                    {
                        activeLabel.Visible = true;
                        marks.Visible = true;
                    }
                }
            }
    

    Regards,

    Angie 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 21, 2018 7:30 AM