Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
******** ASP.NET 4.0, VB.NET 2010 ************
Hi all,

I have a page that creates labels programmatically. Each label initializes to a value based on the getcustomer method - see my code below.
Here is my problem, how can I reference the created label to use it?

For example, lets say I want to show it's value in a msgbox.
Example:
VB
msgbox(label1.text) 'Im get an error when I use label1


 Dim i As Integer
 Dim numlabels As Integer

 numlabels = 6
 For i = 1 To numlabels
     Dim myLabel As Label = New Label()
     myLabel.Text = "Value " & getcustomer()
     myLabel.ID = "Label" & i
     Panel1.Controls.Add(myLabel)

     Dim spacer As LiteralControl = New LiteralControl("<br />")
    Panel1.Controls.Add(spacer)
 Next
Posted
Updated 25-May-11 6:09am
v3
Comments
Kschuler 25-May-11 12:10pm    
I edited your question and took out the "as you can see the code below will have an output". In the future you should edit your questions instead of posting solutions for additional comments.

FindControl function[^]

because you added the control dynamically you can't directly access it. The example below shows how to access such a control by the ID that you have given it. (this was taken directly from the link above)

VB
' This occurs for Repeater1 and originates from LinkButton onClick.
  Protected Sub OnMyCommand1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
    Dim b As LinkButton = sender
    If Not (b Is Nothing) Then
      Dim c As Label = CType(b.Parent.FindControl("foo"), Label)
      If Not (c Is Nothing) Then
        c.Text = "text changed in handler"
        c.ForeColor = System.Drawing.Color.Green
      End If
    End If

  End Sub
 
Share this answer
 
Comments
Abhinav S 25-May-11 12:33pm    
Good answer. My 5.
thanks for your answer...its brilliant.. I have a follow up question..

I am creating an online exam page with
30 radiobuttons that created dynamically(during run time).
Here is my problem, how will I get the click event of each radiobutton and tag it in my method that will check if the next question is needed to be jump or escaped.

sample :
If im in question 10 and if I answer = "Yes" it will redirect me to Question 15 else it go to the next question or nothing happen
 
Share this answer
 
Comments
Kschuler 26-May-11 9:25am    
You should post this as a new question. You're not going to get much response from it as a solution to a different question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900