Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error while Adding Controls to a form dynamically

Status
Not open for further replies.

jino

Programmer
Apr 1, 2004
41
0
0
CA
Hi,

I need to add labels to a form dynamically. The number of labels to be added is driven by a variable stored in session. The labels should have text like "Label 1", Label 2", "Label 3" and so forth. I am trying to create these controls in the page_load event. But it throws an unhandled exception.

Code:
dim i as integer

If numberInSession > 0 then

For i=1 to numberInSession

dim myLabel as System.Web.UI.WebControls.Label
Me.FindControl("Form1").Controls.Add(myLabel)
myLabel.Text = "Label" & i

Next
End If

Help?

Jino
 
myLabel = New System.Web.UI.WebControls.Label()
 
What is the error?
Also, dynamic controls should be created in the page_init event.

Jim
 
I'm assuming the error will be an object reference not set to an instance of an object error as they haven't created a new instance of the Label (as Veep points out).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top