Search code examples
asp.netvb.netrepeater

2 PlaceHolder inside Repeater - Hide/Show


I am trying to hide PlaceHolder2 and show PlaceHolder1 - on click of linkButton. Both are insider repeater.

Aspx

<form id="form1" runat="server">
   <asp:Repeater ID="rptOnly" runat="server">
         <ItemTemplate>
              <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="False">
                      //Display some images
              </asp:PlaceHolder>
              <asp:PlaceHolder ID="PlaceHolder2" runat="server">
                   <asp:linkbutton runat="server" id="lnkbtn" text="Click Here" OnClick="Myfunction_Click" />
              </asp:PlaceHolder>
         </ItemTemplate>
   </asp:Repeater>
</form>

VB.net

Protected Sub Myfunction_Click(sender As Object, e As EventArgs)
    PlaceHolder1.Visible = True
    PlaceHolder2.Visible = False
End Sub

Solution

  • Use FindControl from the RepeaterItem (as there could be multiple items in the Repeater) and then set the visibility.

    EX for First Item in Repeater =

    CType(rptOnly.Items(0).FindControl("PlaceHolder1"), PlaceHolder).Visible = True
    CType(rptOnly.Items(0).FindControl("PlaceHolder2"), PlaceHolder).Visible = False
    

    For all items in the repeater, do a For Each RPI as RepeaterItem in rptOnly.Items and do the same thing.

    Edit from Comments:

    Your particular issue would have you set the CommandName Property of your Button, which is also within the ItemTemplate, and change the visibility of the PlaceHolders in the ItemCommand Event of the Repeater.

    CType(e.Item.FindControl("PlaceHolder1"), PlaceHolder).Visible = True