U
utvecklare.net
Hi,
How do I see to that an encapsulated control is surely created?
I´m trying to create a WebUserControl (A), witch is supposed to be
able to contain other WebUserControls (ie B). I´m creating the control
(A) dynamically. The problem occurs in the inner control (B).
I get the error: "The object reference is not set to an instance of an
object."
And I can see that the lblMyLabel is "Nothing" when I'm trying to set
the property Text just below here. Why is it nothing? How do I see to
that the lblMyLabel is surely created?
My inner control (B) myLabelControl:
ASCX contains this: <asp:Label id="lblMyLabel" runat="server">My
FemDLabelControl</asp:Label>
Code behind:
Public Property Text() As String
Get
EnsureChildControls()
Return lblMyLabel.Text
End Get
Set(ByVal Value As String)
EnsureChildControls()
lblMyLabel.Text = Value
End Set
End Property
My control (A) ASCX contains:
<TABLE BORDER="0" CELLSPACING="1" CELLPADDING="1" width="100%"
id="tblDetails" runat="server">
Code behind:
Private m_iAntalSektioner As Integer = 0
Private m_Sektion As Object() = {New Object}
Public Sub AddSection(ByVal Sektion As Object)
m_iAntalSektioner = m_iAntalSektioner + 1
m_Sektion(m_iAntalSektioner - 1) = Sektion
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim row As HtmlTableRow
Dim cell As HtmlTableCell
For i = 1 To m_iAntalSektioner
Try
cell = New HtmlTableCell
cell.Attributes.Item("CLASS") = "TDBackgroundDetails"
cell.Controls.Add(m_Sektion(i - 1))
row = New HtmlTableRow
row.Cells.Add(cell)
tblDetails.Rows.Add(row)
Catch ex As System.NullReferenceException
'No section was cerated. The row couldn´t be cerated.
Try the next instead.
End Try
Next
End Sub
Testpage ASCX contains (A):
<MYetails id="tblDetailsTable" runat="server" Titel="Test" />
Testpage Page_Load:
Dim lbl1 As myLabelControl = New myLabelControl
lbl1.Text = "Titel"
tblDetailsTable.AddSection(lbl1)
How do I see to that an encapsulated control is surely created?
I´m trying to create a WebUserControl (A), witch is supposed to be
able to contain other WebUserControls (ie B). I´m creating the control
(A) dynamically. The problem occurs in the inner control (B).
I get the error: "The object reference is not set to an instance of an
object."
And I can see that the lblMyLabel is "Nothing" when I'm trying to set
the property Text just below here. Why is it nothing? How do I see to
that the lblMyLabel is surely created?
My inner control (B) myLabelControl:
ASCX contains this: <asp:Label id="lblMyLabel" runat="server">My
FemDLabelControl</asp:Label>
Code behind:
Public Property Text() As String
Get
EnsureChildControls()
Return lblMyLabel.Text
End Get
Set(ByVal Value As String)
EnsureChildControls()
lblMyLabel.Text = Value
End Set
End Property
My control (A) ASCX contains:
<TABLE BORDER="0" CELLSPACING="1" CELLPADDING="1" width="100%"
id="tblDetails" runat="server">
Code behind:
Private m_iAntalSektioner As Integer = 0
Private m_Sektion As Object() = {New Object}
Public Sub AddSection(ByVal Sektion As Object)
m_iAntalSektioner = m_iAntalSektioner + 1
m_Sektion(m_iAntalSektioner - 1) = Sektion
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim row As HtmlTableRow
Dim cell As HtmlTableCell
For i = 1 To m_iAntalSektioner
Try
cell = New HtmlTableCell
cell.Attributes.Item("CLASS") = "TDBackgroundDetails"
cell.Controls.Add(m_Sektion(i - 1))
row = New HtmlTableRow
row.Cells.Add(cell)
tblDetails.Rows.Add(row)
Catch ex As System.NullReferenceException
'No section was cerated. The row couldn´t be cerated.
Try the next instead.
End Try
Next
End Sub
Testpage ASCX contains (A):
<MYetails id="tblDetailsTable" runat="server" Titel="Test" />
Testpage Page_Load:
Dim lbl1 As myLabelControl = New myLabelControl
lbl1.Text = "Titel"
tblDetailsTable.AddSection(lbl1)