Rendering child controls "inline"


N

Netveloper

Hi,

I'm currently creating a very basic UserControl. The control implements an
interface called IAdvanceView which looks like below


Public Interface IAdvancedView

Sub OnRenderBegin()

Sub OnRenderEnd()

End Interface


so the idea is to do some tigger some rendering from the control in various
stages of it's life cycle. The controll collection can contain
more of my control, so they can be nested. The control overrides the Render
method

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

Me.OnRenderBegin()

For Each SubControl As Control In Me.Controls
If (TypeOf (SubControl) Is IAdvancedView) Then
SubControl.RenderControl(writer)
End If
Next

Me.OnRenderEnd()

MyBase.ChildControlsCreated = True
MyBase.Render(writer)

End Sub



So the concept is

(1) Call OnRenderBegin on the control - having it do some action, like
databind a repeater
(2) Find the next control and have it render, using the same three steps
(3) Same as (1) but for OnRenderEnd method

The OnRenderBegin and OnRenderEnd methods are very simple


Public Sub OnRenderBegin() Implements IAdvancedView.OnRenderBegin

Dim BeginElements As New ArrayList

With BeginElements
.Add("Begin element 1")
.Add("Begin element 2")
.Add("Begin element 3")
End With

With Me.rptBeginElements
.DataSource = BeginElements
.DataBind()
End With

End Sub

Public Sub OnRenderEnd() Implements IAdvancedView.OnRenderEnd

Dim EndElements As New ArrayList

With EndElements
.Add("End element 1")
.Add("End element 2")
.Add("End element 3")
End With

With Me.rptEndElements
.DataSource = EndElements
.DataBind()
End With

End Sub

The HTML code for the contol is equaly simple

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="ContainerView.ascx.vb" Inherits="MyProject.ContainerView"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:repeater id="rptBeginElements" runat="server">
<headertemplate>
<ul>
</headertemplate>
<itemtemplate>
<li><%# Container.DataItem %></li>
</itemtemplate>
</asp:repeater>

<asp:repeater id="rptEndElements" runat="server">
<itemtemplate>
<li><%# Container.DataItem %></li>
</itemtemplate>
<footertemplate>
</ul>
</footertemplate>
</asp:repeater>

So taking the test code, the idea is to have it render list items from the
data bound to the control in OnRenderBegin and OnRenderEnd and inbetween the
two, the child controls s hould be created, ineffect creating sublists for
the child controls. For example if I hade two controls, one located in the
others Control collection I would like for it to render like

<ul>
<li>Begin Element 1</li>
<li>Begin Element 2</li>
<li>Begin Element 3</li>
<ul>
<li>Begin Element 1</li>
<li>Begin Element 2</li>
<li>Begin Element 3</li>
<li>End Element 1</li>
<li>End Element 2</li>
<li>End Element 3</li>
</ul>
<li>End Element 1</li>
<li>End Element 2</li>
<li>End Element 3</li>
</ul>

Now the lists is just a test.. I could ofcourse place other code in my
repeaters, but I think this illustrates what I want to happen. Now this is
not the result I'm getting, I keep getting the lists rendered after
eachother and the second one is rendered twice. The full code is a class



Public Class ContainerView
Inherits System.Web.UI.UserControl
Implements IAdvancedView

// Insert previous methods here



End Class



To test it, create two objects of the control, c1 and c2 .. .add c2 to the
controls collection of c1 and add c1 to the control collection of a webform.

Any ideas what I should change ? =)
 
Ad

Advertisements


Top