Add labels to custom control

Discussion in 'ASP .Net Building Controls' started by Antonio Maciel, Oct 7, 2003.

  1. 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
    Antonio Maciel, Oct 7, 2003
    #1
    1. Advertising

  2. Antonio Maciel

    Teemu Keiski Guest

    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.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist


    "Antonio Maciel" <> wrote in message
    news:...
    > 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
    >
    >
    Teemu Keiski, Oct 9, 2003
    #2
    1. Advertising

  3. 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


    "Teemu Keiski" <> wrote in message
    news:%...
    > 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.
    >
    > --
    > Teemu Keiski
    > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > ASP.NET Forum Moderator, AspAlliance Columnist
    >
    >
    > "Antonio Maciel" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
    Antonio Maciel, Oct 9, 2003
    #3
  4. Antonio Maciel

    Teemu Keiski Guest

    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.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist

    "Antonio Maciel" <> wrote in message
    news:...
    > 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
    >
    >
    > "Teemu Keiski" <> wrote in message
    > news:%...
    > > 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.
    > >
    > > --
    > > Teemu Keiski
    > > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > > ASP.NET Forum Moderator, AspAlliance Columnist
    > >
    > >
    > > "Antonio Maciel" <> wrote in message
    > > news:...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
    Teemu Keiski, Oct 11, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Russell

    Dynamic add labels to a form.

    Stephen Russell, Aug 11, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    465
    S. Justin Gengo
    Aug 11, 2003
  2. =?Utf-8?B?V2FubmFiZV9HZWVr?=

    Custom Control Add Control Dynamically.

    =?Utf-8?B?V2FubmFiZV9HZWVr?=, Jun 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    406
    John Saunders
    Jun 30, 2004
  3. Ronny Mandal

    PyX custom x-labels

    Ronny Mandal, May 8, 2006, in forum: Python
    Replies:
    0
    Views:
    294
    Ronny Mandal
    May 8, 2006
  4. Chad Scharf
    Replies:
    3
    Views:
    866
    Chad Scharf
    Oct 18, 2007
  5. Matt Sokol
    Replies:
    2
    Views:
    375
    Theo Tillotson
    Aug 7, 2003
Loading...

Share This Page