Error adding control in Page PreInit event

Hi. The following code gives me an error at the line marked "Error Here"

    Private Sub Dynamic_Data_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
        Dim oDropDownList As New DropDownList
        oDropDownList.ID = "d1"
        Me.Panel_Controls.Controls.Add(oDropDownList) 'Error here
  End Sub

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
Fernando SotoConnect With a Mentor RetiredCommented:
I am not a web developer but from the exception the control that has issues is the Panel_Controls. From Microsoft documentation during Page.PreInit the controls have not yet been initialized and therefore the error. Try using the Page.Init event, this is where the controls have already been initialized.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Great answer. Thanks very much
Fernando SotoRetiredCommented:
Not a problem murbro, glad I was able to help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.