Composite Control and User Control

Discussion in 'ASP .Net Web Controls' started by Madison, Apr 18, 2007.

  1. Madison

    Madison Guest

    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.
     
    Madison, Apr 18, 2007
    #1
    1. Advertising

  2. Madison

    Shimon Sim Guest

    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.
    "Madison" <> wrote in message
    news:...
    > 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.
     
    Shimon Sim, Apr 18, 2007
    #2
    1. Advertising

  3. 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 (, 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.
     
    Walter Wang [MSFT], Apr 19, 2007
    #3
  4. Hi Madison,

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

    Regards,
    Walter Wang (, 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.
     
    Walter Wang [MSFT], Apr 23, 2007
    #4
  5. Madison

    Madison Guest

    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?


    "Walter Wang [MSFT]" wrote:

    > Hi Madison,
    >
    > Have you tried above suggestion? Please feel free to let me know if there's
    > anything I can help.
    >
    > Regards,
    > Walter Wang (, 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.
    >
    >
     
    Madison, Apr 24, 2007
    #5
  6. 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 (, 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.
     
    Walter Wang [MSFT], Apr 25, 2007
    #6
  7. Madison

    Madison Guest

    Hi Walter,

    Thank you for your help. I can refer my composite control from asp page now.
     
    Madison, Apr 25, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sleigh
    Replies:
    1
    Views:
    2,694
    sleigh
    Feb 12, 2004
  2. shapper
    Replies:
    6
    Views:
    959
    shapper
    Dec 26, 2007
  3. Chad
    Replies:
    0
    Views:
    227
  4. Itaichuk

    Deriving a Composite Control from a User Control

    Itaichuk, Oct 13, 2007, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    736
    Itaichuk
    Oct 13, 2007
  5. Mike

    Composite vs non composite Controls

    Mike, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    266
    Sundararajan
    Mar 11, 2005
Loading...

Share This Page