0
創建在運行時我添加一個文本框與|上一頁和控制在運行時
Dim MRNCell As New TableCell
MRNCell.ID = "MRNCell"
Dim txtMRN As New TextBox
txtMRN.ID = "NewMRN"
MRNCell.Controls.Add(txtMRN)
MRNRow.Cells.Add(MRNCell)
然後我試圖訪問使用|上一頁公共只讀屬性的Text屬性。以下是該物業的三個版本,其中沒有一個適用。
Public ReadOnly Property NewMRN() As String
Get
Dim NewMRNNum As TextBox = CType(Me.FindControl("NewMRN"), TextBox)
'NewMRNNum = Nothing
Return NewMRNNum.Text
End Get
End Property
Public ReadOnly Property NewMRN() As String
Get
Dim sNewMRN As String = String.Empty
For Each MyRow As TableRow In MyTable.Rows
For Each MyCell As TableCell In MyRow.Cells
If MyCell.ID = "MRNCell" Then
For Each MyControl As Control In MyCell.Controls
Dim MRNBox As New TextBox
MRNBox = TryCast(MyControl, TextBox)
If Not (MRNBox Is Nothing) Then
sNewMRN = MRNBox.Text
End If
Next
End If
Next
Next
'There is only one TextBox in the table and sNewMRN = ""
Return sNewMRN
End Get
End Property
Public ReadOnly Property NewMRN() As String
Get
'For this one the TextBox is declared Public in the class
'The Text property = ""
Return txtMRN.Text
End Get
End Property
我有兩個公共只讀屬性。第一個從設計器中創建的TextBox返回Text屬性,其他嘗試返回在運行時創建的Text屬性。一個工作,另一個或者拋出一個異常或者返回一個空字符串,這取決於我使用的三種方法中的哪一種。
If Not PreviousPage Is Nothing Then
'Works
Dim sMessageID As String = PreviousPage.MessageID
'Does not work
Dim sNewMRN As String = PreviousPage.NewMRN
Literal1.Text = "<p>" & sMessageID & "</p><p>" & sNewMRN & "</p>"
End If
所以,我怎麼能訪問在運行時創建一個文本框的Text屬性,有一個公共只讀屬性返回的值,這樣我就可以訪問|上一頁它?
格雷格