2 PlaceHolder在Repeater中 - 隐藏/显示

时间:2013-12-12 22:43:39

标签: asp.net vb.net repeater

我试图隐藏PlaceHolder2并显示PlaceHolder1 - 点击linkBut​​ton。两者都是内部转发器。

.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

2 个答案:

答案 0 :(得分:1)

转发器有一组项目 - 您需要检查集合中的每个项目:

For Each c As Control in rptOnly.Items

    Dim p1 As Control = c.FindControl("PlaceHolder1")
    Dim p2 As Control = c.FindControl("PlaceHolder2")

    If p1 IsNot Nothing Then
        p1.Visible = True
    End If
    If p2 IsNot Nothing Then
        p2.Visible = False
    End If
Next

每个项目都是RepeaterItem:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeateritem_methods(v=vs.110).aspx

Myfunction_Click中,您应该能够找到占位符作为控件父级 - 就像这样(您可能需要强制转换父对象):

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

答案 1 :(得分:1)

使用RepeaterItem中的FindControl(因为Repeater中可能有多个项目),然后设置可见性。

转发器中第一项的EX =

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

对于转发器中的所有项目,在rptOnly.Items中为每个RPI执行RepeaterItem并执行相同的操作。

从评论中编辑:

您的特定问题是设置Button的CommandName属性,该属性也在ItemTemplate中,并在Repeater的ItemCommand事件中更改PlaceHolders的可见性。

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