[英]PreviousPage and control created at run-time
在运行时,我添加了一个文本框
Dim MRNCell As New TableCell
MRNCell.ID = "MRNCell"
Dim txtMRN As New TextBox
txtMRN.ID = "NewMRN"
MRNCell.Controls.Add(txtMRN)
MRNRow.Cells.Add(MRNCell)
然后,我尝试使用PreviousPage访问公共只读属性中的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属性,并在公共readonly属性中返回该值,以便可以使用PreviousPage访问它?
格雷格
再一次,写问题使我对问题进行了充分的思考,以至于我提出了一个解决方案。 我在设计时添加了文本框,并将visible属性设置为false。 然后在运行时将其可见,并将其添加到表行单元格中。
格雷格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.