在运行时创建的PreviousPage和控件

时间:2015-09-01 21:10:36

标签: asp.net visual-studio

在运行时,我添加一个带

的文本框
         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

那么,如何访问在运行时创建的Textbox的Text属性,在public readonly属性中返回该值,以便我可以使用PreviousPage访问它?

格雷格

1 个答案:

答案 0 :(得分:0)

Once again, writing the question made me think about it enough that I came up with a solution. I add the textbox at design-time and set the visible property to false. Then at run-time I make it visible and add it to the table row cell.

Greg