2013-12-11 56 views
1

我有一個asp.net頁面,其中包含使用頁面上控件的方法。我需要實例化這個頁面,自動化一些「用戶交互」,選擇一個單選按鈕,點擊一個按鈕等。以編程方式實例化asp.net頁面及其控件

大多數情況下,這個工作,但我必須物理實例化我想要使用的每個控件這是編程式的。是否有一種實例化所有控件的方法/技巧,就像在頁面「加載」之前所做的那樣,它會被髮送到瀏覽器?

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

我想,能進一個實例化所有的控制,但是這是一個臨時的自動化,我需要做的,我希望不要有實例的一切。

如果您願意,我可以調用「構建」頁面嗎?我想我正在尋找或相反的處置?調用init並沒有幫助,因爲這不會在心理上創建控件。

這可能會做的工作..但有沒有更好的辦法?

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 

回答

0

在原始問題中使用我的代碼塊結束。

相關問題