CreateChildControl or Render?

N

nobody

Should I create controls for a composite WebControl in the
CreateChildControl or the Render method? It seems like CreateChildControl
is not called at design time when the control is on a page being designed.

Here's my situation. I'm making a composite WebControl that has a label and
a RadioButtonList. I want the developer to be able to set properties on the
control to set the text for the text, and edit a collection of strings for
the RadioButtonList, and I want the changes the developer makes to the
properties in Property Window to reflect on the control on the page (at
design time).

Here's my code at the moment. I had to check the controls to see if they
were empty in the Render(...) method and create fake ones if they were so
something would render at design-time. The CreateChildControls(...) method
is never called at design time. So should I just intialize the controls and
add them in the Render(...) method, or do I even need to add them to the
WebControl if I'm just telling them to render themselves? Or should I even
have the Private variables for the controls and just create them in the
Render, tell them to render themselves, and destroy them... That sound
resource intensive in a Render(...) method... I have a feeling I'm going
about this all the wrong way. Anybody feel like throwing some pointers at
me? I'll duck if they're coming to hard and fast... ;)

Private m_QuestionLabel As New WebControls.Label
Private m_RadioButtonList As New WebControls.RadioButtonList
Private m_QuestionText As String = ""
Private m_Choices As New Collections.Specialized.StringCollection

Public Property QuestionText() As String
Get
Return m_QuestionText
End Get
Set(ByVal Value As String)
m_QuestionText = Value
End Set
End Property

Public Property Choices() As Collections.Specialized.StringCollection
Get
Return m_Choices
End Get
Set(ByVal Value As Collections.Specialized.StringCollection)
m_Choices = Value
End Set
End Property

Protected Overrides Sub CreateChildControls()
' Put the Label together and add it...
m_QuestionLabel.Font.Bold = True
m_QuestionLabel.Text = m_QuestionText
m_QuestionLabel.Width = Me.Width
Me.Controls.Add(m_QuestionLabel)
' Put the RadioButtonList together and add it...
m_RadioButtonList.Items.Clear()
For Index As Integer = 0 To m_Choices.Count - 1
m_RadioButtonList.Items.Add(m_Choices(Index))
Next
m_RadioButtonList.Width = Me.Width
Me.Controls.Add(m_RadioButtonList)
End Sub

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
' Render the Label...
Dim TempLabel As WebControls.Label
If m_QuestionLabel.Text.Trim() > String.Empty Then
' If there is text in our Label, then render it.
TempLabel = m_QuestionLabel
Else
' Otherwise, make up a fake one (for design-time).
TempLabel = New WebControls.Label
TempLabel.Text = "Question Text"
TempLabel.Font.Bold = True
TempLabel.Width = Me.Width
End If
TempLabel.RenderControl(writer)
TempLabel = Nothing
' Render the RadioButtonList...
Dim TempRBL As WebControls.RadioButtonList
If m_RadioButtonList.Items.Count > 0 Then
' If there are choices in our RadioButtonList, then
' render it.
TempRBL = m_RadioButtonList
Else
' Otherwise, make up a fake one (for design-time).
TempRBL = New WebControls.RadioButtonList
TempRBL.Items.Clear()
TempRBL.RepeatDirection = m_RadioButtonList.RepeatDirection
TempRBL.Items.Add("Choice 1")
TempRBL.Items.Add("Choice 2")
TempRBL.Items.Add("Choice 3")
TempRBL.Items.Add("Choice 4")
TempRBL.Width = Me.Width
TempRBL.Height = New WebControls.Unit(Me.Height.Value -
m_QuestionLabel.Height.Value)
End If
TempRBL.RenderControl(writer)
TempRBL = Nothing
End Sub
 
N

Natty Gur

Hi,

First try to check out RPS when you using render and CreateChildControl.
You'll find out that render result in higher RPS. I'm using render when
i write my own controls. If you need to check out if you in design or
runtime you can check this.Site.DesignMode.

HTH

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
N

nobody

What is RPS? R(something) Per Second? Thanks for the tip on
Site.DesignMode! I was just checking my RadioButtonList.Items to see if it
was empty. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top