Simple User Control

Mark123

New member
Joined
Jan 23, 2009
Messages
4
Programming Experience
3-5
I've got a user control, its a div with background image and a label on top. I've added the control to default.aspx and now I am trying to set the label.text of the control to "Text".

I keep getting a null reference exception.

Here's the property on the user control
VB.NET:
Public Property LabelText2() As String
        Get
            Return Label2.Text
        End Get
        Set(ByVal value As String)
            Label2.Text = value
        End Set
    End Property

When I hit set it says null exception Label2.text

Here's the call from aspx page
I am passing in a list of folder names and I want those names to show up on each one of the controls
VB.NET:
Private Sub CreateTable(ByVal Rows As Integer, ByVal FldrNames() As String, ByVal Fldrcollection As Generic.IList(Of ASP.ui_controls_folder_ascx))

        Dim rowCnt As Integer
        Dim cellCnt As Integer
        Dim index As Integer = 0

        For rowCnt = 1 To Rows + 1
            Dim tRow As New TableRow
            For cellCnt = 1 To 3
                Dim tCell As New TableCell
                Dim fldrcontrol As New ASP.ui_controls_folder_ascx
                If Not FldrNames(index) Is Nothing Then
                    tCell.Controls.Add(fldrcontrol)
                    fldrcontrol.ID = "Folder" + index.ToString 'works fine
                    fldrcontrol.LabelText2 = FldrNames(index) 'bombs out
                    tRow.Cells.Add(tCell)

                End If
                index = index + 1
            Next
            Me.Table1.Rows.Add(tRow)
        Next

Thanks for the help.

Mark
 
Last edited by a moderator:
Looks like this works fine

VB.NET:
Dim fldrcontrl As New ASP.ui_controls_folder_ascx
            
Me.RadAjaxPanel1.Controls.Add(fldrcontrl)
fldrcontrl.ID = "Folder5"
fldrcontrl.LabelText2 = "Testing"
 
Looks like I figured it out on my own. Adding the control to the Cell didnt initialize the label. Once I added the entire table to the ASP page then I could access all of the controls properties.

VB.NET:
' Create the Table
        Dim rowCnt As Integer
        Dim cellCnt As Integer

        Dim index As Integer = 0

        For rowCnt = 1 To Rows + 1
            Dim tRow As New TableRow
            For cellCnt = 1 To 3
                Dim tCell As New TableCell
                Dim fldrcontrol As New ASP.ui_controls_folder_ascx
                If Not FldrNames(index) Is Nothing Then

                    tCell.Controls.Add(fldrcontrol)
                    fldrcontrol.ID = "Folder" + index.ToString
                    fldrcollection.Add(fldrcontrol)
                    tRow.Cells.Add(tCell)

                End If
                index = index + 1
            Next
            Me.Table1.Rows.Add(tRow)
        Next

        'Change the label on each Item

        For index = 0 To fldrcollection.Count
            fldrcollection(index).LabelText = FldrNames(index)
        Next
 
Back
Top