Search code examples
asp.netrepeatervisible

How do I switch visibility of a DIV in a repeater?


I have a DIV inside my repeater as follows:

    <asp:Repeater ID="topicView" runat="server" OnItemCommand="Delete_ItemCommand">
    <ItemTemplate>
    <table width="945px" cellpadding="0" cellspacing="0" border="0" class="post-table">
        <tr>
            <td colspan="2">
                <div class="post-info">
                    <div class="post-info-left"><%#DataBinder.Eval(Container.DataItem, "PostDate")%></div>
                    <div class="post-info-right">#<%#DataBinder.Eval(Container.DataItem, "PostID")%></div>
                    <div class="clear"></div>
                </div>
            </td>
        </tr>
        <tr>
            <td class="post-left">
                <p><strong><%#DataBinder.Eval(Container.DataItem, "Username")%></strong></p>
            </td>
            <td class="post-right">
                <p><%#DataBinder.Eval(Container.DataItem, "PostBody")%></p>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <div class="post-edit">
                    <p id="postEditAdmin" runat="server" visible="false"><a class="fancybox.iframe fancybox" href='editpost.aspx?postID=<%#DataBinder.Eval(Container.DataItem, "PostID")%>'>Edit</a> | <asp:LinkButton ID="deleteBtn" runat="server" Text="Delete" CommandName="Delete" OnClientClick="javascript:if(!confirm('Delete this information? this will delete permanently'))return false;" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "PostID")%>' /></p>
                    <p id="postEdit" runat="server" visible="true"><i>You must be an admin to be able to edit or delete a post</i></p>
                </div>
            </td>
        </tr>
    </table>
    </ItemTemplate>
</asp:Repeater>

Can anyone let me know how to set the postEditAdmin.Visible to true and postEdit.Visible to false? It's not being picked up in the page load.

Also please note it's going to be changed depending on an if statement. This is what I have so far:

        if (Session["role"].ToString() == "2")
        {
            postEditAdmin.Visible = true;
            postEdit.Visible = false;
        }

Solution

  • You have to make div visible or hidden during binding process. Add event to repeater.

    In aspx page, html side add

     <asp:Repeater ID="topicView" runat="server" OnItemCommand="Delete_ItemCommand" OnItemDataBound="topicView_ItemDataBound">
    

    On server side code behind file add

        void topicView_ItemDataBound(object sender, RepeaterItemEventArgs e) 
        {
             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
             {
               // Show or hid div here
               System.Web.UI.HtmlControls.HtmlContainerControl postEditAdmin = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("postEditAdmin");
    
               System.Web.UI.HtmlControls.HtmlContainerControl postEdit= (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("postEdit");
    
              if (Session["role"].ToString() == "2")
              {
                  postEditAdmin.Visible = true;
                  postEdit.Visible = false;
              }    
            }
        }