<script runat="server" language="VB">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Response.Write("<h1>Repeater Weirdness Demo</h1>This demo shows the Repeater weirdness. " & _
"Notice that by clicking the Button the Repeater's ItemCommand does not fire, " & _
"but the LinkButton does cause the ItemCommand to fire.")
Response.Write("<p /><hr /><big>Starting Page_Load event handler...</big><br />")
'If Not Page.IsPostBack Then
BindData()
'End If
End Sub
Private Sub BindData()
Response.Write("<hr /><big>Starting BindData() Method...</big><br />")
Dim a As New ArrayList
a.Add(1) : a.Add(2) : a.Add(3)
Repeater1.DataSource = a
Repeater1.DataBind()
Response.Write("Repeater has been databound...<br />")
'Print out control tree
Response.Write("<br />Control Tree Right After DataBind()...<br />")
PrintCTree(Repeater1, 0)
Response.Write("<p>")
'Reference a button
Dim b As Button = CType(Repeater1.Items(1).FindControl("buttonTest"), Button)
Response.Write("Button info in BindData(): ")
PrintCAncestors(b)
Response.Write("<p>")
End Sub
Private Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)
Response.Write("<hr /><big>Starting Repeater ItemCommand Event Handler...</big><br />")
Response.Write(String.Concat("<b>ItemCommand</b>: The CommandName = ", e.CommandName, "<br />"))
End Sub
Private Sub PrintCTree(ByVal c As Control, ByVal indent As Integer)
For i As Integer = 0 To indent
Response.Write(" ")
Next
Response.Write(c.UniqueID)
If c.Parent Is Nothing Then
Response.Write(" <b>NO PARENT</b><br />")
Else
Response.Write(String.Concat(" (Parent = ", c.Parent.UniqueID, ")<br />"))
End If
For Each child As Control In c.Controls
PrintCTree(child, indent + 1)
Next
End Sub
Protected Sub bCommand(ByVal sender As Object, ByVal e As CommandEventArgs)
Response.Write("<hr /><big>Starting Button's Command Event Handler...</big><br />")
Response.Write("Button Command event fired!<br />")
Dim b As Button = CType(sender, Button)
Response.Write("Control Ascestors: ")
PrintCAncestors(b)
Response.Write("<p>")
Response.Write("Examining Repeater's children:<br />")
For Each c As Control In Repeater1.Controls
Response.Write("<li>" & c.UniqueID & String.Concat(" (", c.GetType().ToString(), ")") & ", Parent = " & c.Parent.UniqueID)
If c.Equals(b.Parent) Then
Response.Write(" - this is the Button's parent!")
End If
Response.Write("</li>")
Next
Response.Write("<p>")
'Print out control tree
Response.Write("Control Tree in Button Command Event<br />")
PrintCTree(Me, 0)
Response.Write("<p>")
If b.Parent Is Nothing Then
Response.Write("Button Parent is <b>NOTHING!!</b><br />")
Else
Response.Write(String.Concat("Button Parent is ", b.Parent.UniqueID, " - ") & String.Concat(b.Parent.GetType().ToString(), "<br />"))
If b.Parent.NamingContainer Is Nothing Then
Response.Write("Button Parent's NamingContainer is <b>NOTHING!!</b><br />")
Else
Response.Write(String.Concat("Button Parent NamingContainer is ", b.Parent.NamingContainer.UniqueID, "<br />"))
End If
End If
End Sub
Protected Sub lCommand(ByVal sender As Object, ByVal e As CommandEventArgs)
Response.Write("<hr /><big>Starting LinkButton's Command Event Handler...</big><br />")
Response.Write("LinkButton Command event fired!<br />")
Dim lb As LinkButton = CType(sender, LinkButton)
Response.Write("Control Ascestors: ")
PrintCAncestors(lb)
Response.Write("<p>")
Response.Write("Examining Repeater's children:<br />")
For Each c As Control In Repeater1.Controls
Response.Write("<li>" & c.UniqueID & String.Concat(" (", c.GetType().ToString(), ")") & ", Parent = " & c.Parent.UniqueID)
If c.Equals(lb.Parent) Then
Response.Write(" - this is the LinkButton's parent!")
End If
Response.Write("</li>")
Next
Response.Write("<p>")
If lb.Parent Is Nothing Then
Response.Write("LinkButton Parent is <b>NOTHING!!</b><br />")
Else
Response.Write(String.Concat("Button Parent is ", lb.Parent.UniqueID, " - ") & String.Concat(lb.Parent.GetType().ToString(), "<br />"))
If lb.Parent.NamingContainer Is Nothing Then
Response.Write("Button Parent's NamingContainer is <b>NOTHING!!</b><br />")
Else
Response.Write(String.Concat("Button Parent NamingContainer is ", lb.Parent.NamingContainer.UniqueID, "<br />"))
End If
End If
End Sub
Private Sub PrintCAncestors(ByVal c As Control)
Response.Write(c.UniqueID & String.Concat(" (", c.GetType().ToString(), ")"))
If Not c.Parent Is Nothing Then
Response.Write(" --> ")
PrintCAncestors(c.Parent)
End If
End Sub
</script>
<HTML>
<body>
<form runat="server">
<hr />
<asp:Repeater id="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="linkButtonTest" Runat="server" OnCommand="lCommand" CommandName="bar" Text="LinkButton"></asp:LinkButton>
<asp:Button OnCommand="bCommand" ID="buttonTest" Runat="server" CommandName="foo" Text="Button"></asp:Button>
<br />
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</HTML>
|