aspx ページに含まれるすべてのコントロール ID をリストに入力しようとしています。
その目的で ArrayList を使用すると、そのリストは正常に生成されます。関数の例: AddControls1。
しかし、一般的な List を使用すると、NullReferenceException エラーが発生しました。関数の例: AddControls2。
一般的なリストを作成するときに何が間違っていますか?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
''success !
Dim controlList1 As New ArrayList()
controlList1 = AddControls1(Page.Controls, controlList1)
For Each str As String In controlList1
Response.Write(str & "<br/>")
Next
''FAIL
Dim controlList2 As New List(Of String)
controlList2 = Nothing
controlList2 = AddControls2(Page.Controls, controlList2)
For Each ctl As String In controlList2
Response.Write(ctl & "<br/>")
Next
End Sub
Private Function AddControls1(ByVal page As ControlCollection, ByVal controlList As ArrayList) As ArrayList
For Each c As Control In page
If c.ID IsNot Nothing Then
controlList.Add(c.ID)
End If
If c.HasControls() Then
AddControls1(c.Controls, controlList)
End If
Next
Return controlList
End Function
Private Function AddControls2(ByVal page As ControlCollection, ByVal controlList As List(Of String)) As List(Of String)
For Each c As Control In page
If c.ID IsNot Nothing Then
controlList.Add(c.ID) <-- here I got NullReferenceException error
End If
If c.HasControls() Then
AddControls2(c.Controls, controlList)
End If
Next
Return controlList
End Function