1

I have an asp.net page, with methods making use of controls on the page. I have a need to instantiate this page, automate some "user interaction", selecting a radio button, clicking a button etc.

For the most part this works, but I have to physically instantiate every control I want to use like this programmatically. Is there a method/technique for instantiating all the controls, as gets done when the page "loads" before it gets sent to the browser?

Dim myTempForm As New form1

mytempForm.label1 = "demo text" <--- label1 is Nothing --- object reference not set

mytempForm.label1 = new Label 

myTempForm.label1 = "demo text" <-- now works as expected, I can use it...

I suppose and can go ahead an instantiate all the controls, but this is a temporary automation that I need to do, I'd wish not to have to instantiate everything.

Is there a method I can call to "build" the page, if you will? I suppose I am looking or the opposite of dispose? Calling init does not help, as this does not psychically create the controls.

This will probably do the job.. but is there a better way?

For Each Control In Me.Controls

    If TypeOf (Control) Is Label Then
        Control = New Label
    ElseIf TypeOf (Control) Is Panel Then
        Control = New Panel
    ElseIf TypeOf (Control) Is TextBox Then
        Control = New TextBox
    ElseIf TypeOf (Control) Is RadioButton Then
        Control = New RadioButton
    ElseIf TypeOf (Control) Is Button Then
        Control = New Button
    ElseIf TypeOf (Control) Is UpdatePanel Then
        Control = New UpdatePanel
    End If

Next

1 Answer 1

0

Ended up using my code block in the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.