By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,759 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,759 IT Pros & Developers. It's quick & easy.

User Web Control Load Problem

P: n/a
Hi,

How do I see to that an encapsulated control is surely created?

Im trying to create a WebUserControl (A), witch is supposed to be
able to contain other WebUserControls (ie B). Im 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 couldnt be cerated.
Try the next instead.
End Try
Next

End Sub

Testpage ASCX contains (A):
<MY:Details id="tblDetailsTable" runat="server" Titel="Test" />
Testpage Page_Load:
Dim lbl1 As myLabelControl = New myLabelControl
lbl1.Text = "Titel"
tblDetailsTable.AddSection(lbl1)

Nov 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.