Composite Control and User Control

M

Madison

Hello,

I'm working in project that I had created composite control to verify
textbox with RequiredFieldVlidator or CompareValidator or
RegularExpressionValidator. The composite control working fine on the regular
web form but when I place composite control in User Control and place User
Control in web form then I got error message: System.NullReferenceException
was unhandled by user code
Message="Object reference not set to an instance of an object." when I try
to call Composite control in User Control from web form. I do not get any
error if I call some regular asp:TextBox in user control from web form.

Thank you for any advice.
 
S

Shimon Sim

Check if you calling anything from your form in your customer control that
require Load event to fire to initialize.
In general check the order of events they may work differently if you put
your control inside of User Control.
Good luck.
 
W

Walter Wang [MSFT]

Hi Madison,

You could configure your VS2005 debugger to break whenever the
NullReferenceException occurs to see which statement is causing the error:

1) Click VS2005 menu Debug/Exceptions
2) Click on button "Find" and input "NullReferenceException", then click OK
3) After it found the exception in the list, click the checkbox at the
right side of it.

In the meanwhile, you could also show some of your code so that we can tell
more clearly what went wrong.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Madison,

Have you tried above suggestion? Please feel free to let me know if there's
anything I can help.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Madison

Hi Walter,
Here is my codes. There is 3 parts
1. aspx page called user control
2. asxc user control use composite control and other asp controls
3. composite textbox control
------------------
Measure.aspx
------------------
Protected Sub ResetControls()
CustomAdmin.SectorCd_ag = Nothing ---> call user control
CustomAdmin.CalcRewardAmtInd_ag = Nothing ---> call user control
CustomAdmin.KWCalcRewardAmt_ag = Nothing ---> call user control
(problem)
CustomAdmin.KWHCalcRewardAmt_ag = Nothing ---> call user control
(problem)
CustomAdmin.ThermCalcRewardAmt_ag = Nothing ---> call user control
(problem)
End Sub

-----------------------
customAdmin.asxc (user control)
-----------------------
---------------------------------
asp:checkbox control property
---------------------------------
Property SectorCd_ag() As String
Get
If ckbSector_ag.Checked Then --->asp:checkbox
Return "A"
Else
Return ""
End If
End Get
Set(ByVal value As String)
If value = "A" Then
ckbSector_ag.Checked = True --->asp:checkbox
Else
ckbSector_ag.Checked = False --->asp:checkbox
End If
End Set
End Property
------------------------------------
asp:radioButton control property
------------------------------------
Property CalcRewardAmtInd_ag() As String
Get
Select Case rdoCalcRewardInd_ag.SelectedIndex
Case 0
Return "Y"
Case 1
Return "N"
Case Else
Return String.Empty
End Select
End Get
Set(ByVal value As String)
Select Case value
Case "Y"
rdoCalcRewardInd_ag.SelectedIndex = 0
Case "N"
rdoCalcRewardInd_ag.SelectedIndex = 1
Case Else
rdoCalcRewardInd_ag.SelectedIndex = -1
End Select
End Set
End Property
--------------------------------------
composite textbox control property
--------------------------------------
Property KWCalcRewardAmt_ag() As String
Get
Return ccKWCalcRewardAmt_ag.Text ---> composite textbox control
End Get
Set(ByVal value As String) --->called composite textbox control
(problem)
If (value Is Nothing) Then
ccKWCalcRewardAmt_ag.Text = Nothing ---> composite textbox
control
Else
ccKWCalcRewardAmt_ag.Text = value ---> composite textbox
control
End If
End Set
End Property

---------------
ccTextBox.vb (composite text box control - only part that has problem)
---------------
Public Property Text() As String
Get
Return _textbox.Text
End Get
Set(ByVal value As String) -- this is problem part
If (value Is Nothing) Then
_textbox.Text = Nothing
Else
_textbox.Text = value
End If

End Set
End Property

I got error System.NullReferenceException: Object reference not set to an
instance of an object. Source error point to Property KWCalcRewardAmt_ag in
user control and when I debug the last code is Public Property Text() in
composite control.

Any idea?
 
W

Walter Wang [MSFT]

Hi Madison,

Whenever you need to acccess a child control in one of your public
method/property in the composite control class, you need to call
EnsureChildControls() to make sure the child controls are created first.
You can create a private wrapper property to return a child control such as:

Public Class ccTextBox
Inherits CompositeControl

Private _textbox As TextBox

Private ReadOnly Property TheTextBox() As TextBox
Get
EnsureChildControls()
Return _textbox
End Get
End Property

Protected Overrides Sub CreateChildControls()
Controls.Clear()
_textbox = New TextBox()
Controls.Add(_textbox)
End Sub

Public Property Text() As String
Get
Return TheTextBox.Text
End Get
Set(ByVal value As String)
TheTextBox.Text = value
End Set
End Property
End Class


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Madison

Hi Walter,

Thank you for your help. I can refer my composite control from asp page now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top