Add labels to custom control

A

Antonio Maciel

Hi.

I am trying to add a label in my custom control based on a condition but
could not find how to do it. I have the following code at my Render event:

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

'Get the values to display from a datareader
If drOptions.HasRows Then
Do While drOptions.Read
Dim lblTitle As New Label
If drOptions.GetInt32(1) = 0 Then
lblTitle.Text = drOptions.GetString(2)
Me.Controls.Add(lblTitle)
Else
output.WriteLine("<input type=radio name='rbItems'>"
& drOptions.GetString(2) & "<br>")
End If
Loop
End If

End Sub

That code does not work. How should I add the label before the options as I
need?

Thanks,

Antonio Maciel
 
T

Teemu Keiski

Adding Label to Controls collection is bit too late in this phase. Instead
you could try writing the Label out as HTML using the HtmlTextWriter or by
calling:

lblTitle.RenderControl(output)

in the loop.
 
A

Antonio Maciel

Hi Teemu.

Thanks, it worked.

But I have another question if you don't mind. When I tried to add a label
as you said, the label was created ok. This lable does not have an id. When
I tried to add a RadioButton control, I was forced to add an id. Without
define an id for the radiobutton I receive an object reference error. Why
can the lable be added without an id but the radiobutton forces me to define
an id?

Thanks,

Antonio Maciel
 
T

Teemu Keiski

Probably because radioButton generates its client-side name attribute (which
used when posting data to the server on postback) based on the ID, though it
should generate it automatically if ID isn't provided by the Page developer.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top