getting web page - control and class to talk to one another is hard...

Discussion in 'ASP .Net' started by Support, Mar 22, 2005.

  1. Support

    Support Guest

    Hello:

    I have a aspx web page - let's call it main.aspx
    It contains a placeholder: Myplaceholder.

    I have a web control, lets call it webcontrol which has a code behind class
    called webcontrolclass and a label called mycontrollabel
    In the webcontrolclass, I have a private sub called:
    Sub writetomycontrollabel(stuff as text)
    mycontrollabel.text=stuff
    End Sub

    The code behind main.aspx
    Dim MANAGEGENERAL As Control = LoadControl("webcontrol.ascx") ' needed for
    the placeholder
    Public MyMANAGEGENERAL As New webcontrolclass


    I have a sub which is called by a button located on main.aspx
    Sub mainaspx_click(usualstuff ) handles mainaspx.Click
    MyPlaceholder.Controls.Add(MANAGEGENERAL)
    MyMANAGEGENERAL.writetomycontrollabel("hello")
    End sub

    I get the error message:
    Object reference not set to an instance of an object.
    Exception Details: System.NullReferenceException: Object reference not set
    to an instance of an object.
    MyMANAGEGENERAL.writetomycontrollabel("hello")

    So, I am missing something obvious - but I dont get it.
    Help
    Thanks
    Terry
    Support, Mar 22, 2005
    #1
    1. Advertising

  2. Support

    ashish Guest

    Re: getting web page - control and class to talk to one another ishard...

    I think you need to load the control using
    LoadControl method and then add it to the controls collection. The
    problem is that the label control inside your custom control is not
    loaded yet..


    hth
    -ashish


    ote:
    > Hello:
    >
    > I have a aspx web page - let's call it main.aspx
    > It contains a placeholder: Myplaceholder.
    >
    > I have a web control, lets call it webcontrol which has a code behind class
    > called webcontrolclass and a label called mycontrollabel
    > In the webcontrolclass, I have a private sub called:
    > Sub writetomycontrollabel(stuff as text)
    > mycontrollabel.text=stuff
    > End Sub
    >
    > The code behind main.aspx
    > Dim MANAGEGENERAL As Control = LoadControl("webcontrol.ascx") ' needed for
    > the placeholder
    > Public MyMANAGEGENERAL As New webcontrolclass
    >
    >
    > I have a sub which is called by a button located on main.aspx
    > Sub mainaspx_click(usualstuff ) handles mainaspx.Click
    > MyPlaceholder.Controls.Add(MANAGEGENERAL)
    > MyMANAGEGENERAL.writetomycontrollabel("hello")
    > End sub
    >
    > I get the error message:
    > Object reference not set to an instance of an object.
    > Exception Details: System.NullReferenceException: Object reference not set
    > to an instance of an object.
    > MyMANAGEGENERAL.writetomycontrollabel("hello")
    >
    > So, I am missing something obvious - but I dont get it.
    > Help
    > Thanks
    > Terry
    >
    >
    >
    ashish, Mar 22, 2005
    #2
    1. Advertising

  3. Support

    Scott Allen Guest

    Where does

    MANAGEGENERAL As Control = LoadControl("webcontrol.ascx")

    exist? In Page_Load? Is it executed with every postback and before the
    Click event?

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Tue, 22 Mar 2005 16:15:15 -0500, "Support"
    <> wrote:

    >Hello:
    >
    >I have a aspx web page - let's call it main.aspx
    >It contains a placeholder: Myplaceholder.
    >
    >I have a web control, lets call it webcontrol which has a code behind class
    >called webcontrolclass and a label called mycontrollabel
    >In the webcontrolclass, I have a private sub called:
    >Sub writetomycontrollabel(stuff as text)
    > mycontrollabel.text=stuff
    >End Sub
    >
    >The code behind main.aspx
    >Dim MANAGEGENERAL As Control = LoadControl("webcontrol.ascx") ' needed for
    >the placeholder
    >Public MyMANAGEGENERAL As New webcontrolclass
    >
    >
    >I have a sub which is called by a button located on main.aspx
    >Sub mainaspx_click(usualstuff ) handles mainaspx.Click
    >MyPlaceholder.Controls.Add(MANAGEGENERAL)
    > MyMANAGEGENERAL.writetomycontrollabel("hello")
    >End sub
    >
    >I get the error message:
    >Object reference not set to an instance of an object.
    >Exception Details: System.NullReferenceException: Object reference not set
    >to an instance of an object.
    > MyMANAGEGENERAL.writetomycontrollabel("hello")
    >
    >So, I am missing something obvious - but I dont get it.
    >Help
    >Thanks
    >Terry
    >
    >
    Scott Allen, Mar 23, 2005
    #3
  4. Support

    Support Guest

    exist before and page Loads ...

    Public Class A
    Inherits System.Web.UI.Page
    Dim MANAGEGENERAL As Control = LoadControl("ManageGeneral.ascx")


    Private Sub PAge_Init() Handles MyBAse.Init
    End Sub


    Private Sub PAge_Load() Handles MyBase.Load
    MyPlaceholder.Controls.Add(MANAGEGENERAL)
    End Sub

    Terry






    "Scott Allen" <> wrote in message
    news:...
    > Where does
    >
    > MANAGEGENERAL As Control = LoadControl("webcontrol.ascx")
    >
    > exist? In Page_Load? Is it executed with every postback and before the
    > Click event?
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    > On Tue, 22 Mar 2005 16:15:15 -0500, "Support"
    > <> wrote:
    >
    >>Hello:
    >>
    >>I have a aspx web page - let's call it main.aspx
    >>It contains a placeholder: Myplaceholder.
    >>
    >>I have a web control, lets call it webcontrol which has a code behind
    >>class
    >>called webcontrolclass and a label called mycontrollabel
    >>In the webcontrolclass, I have a private sub called:
    >>Sub writetomycontrollabel(stuff as text)
    >> mycontrollabel.text=stuff
    >>End Sub
    >>
    >>The code behind main.aspx
    >>Dim MANAGEGENERAL As Control = LoadControl("webcontrol.ascx") ' needed for
    >>the placeholder
    >>Public MyMANAGEGENERAL As New webcontrolclass
    >>
    >>
    >>I have a sub which is called by a button located on main.aspx
    >>Sub mainaspx_click(usualstuff ) handles mainaspx.Click
    >>MyPlaceholder.Controls.Add(MANAGEGENERAL)
    >> MyMANAGEGENERAL.writetomycontrollabel("hello")
    >>End sub
    >>
    >>I get the error message:
    >>Object reference not set to an instance of an object.
    >>Exception Details: System.NullReferenceException: Object reference not set
    >>to an instance of an object.
    >> MyMANAGEGENERAL.writetomycontrollabel("hello")
    >>
    >>So, I am missing something obvious - but I dont get it.
    >>Help
    >>Thanks
    >>Terry
    >>
    >>

    >
    Support, Mar 24, 2005
    #4
  5. Support

    Scott Allen Guest

    You might want to turn on two options under Project -> Properties ->
    Build: Option Strict and Option Explicit. That might help track down
    the source of the error, looking at the code it doesn't seem these
    options are on.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Thu, 24 Mar 2005 07:40:45 -0500, "Support"
    <> wrote:

    >exist before and page Loads ...
    >
    >Public Class A
    >Inherits System.Web.UI.Page
    >Dim MANAGEGENERAL As Control = LoadControl("ManageGeneral.ascx")
    >
    >
    >Private Sub PAge_Init() Handles MyBAse.Init
    >End Sub
    >
    >
    >Private Sub PAge_Load() Handles MyBase.Load
    > MyPlaceholder.Controls.Add(MANAGEGENERAL)
    >End Sub
    >
    >Terry
    >
    >
    >
    >
    >
    >
    >"Scott Allen" <> wrote in message
    >news:...
    >> Where does
    >>
    >> MANAGEGENERAL As Control = LoadControl("webcontrol.ascx")
    >>
    >> exist? In Page_Load? Is it executed with every postback and before the
    >> Click event?
    >>
    >> --
    >> Scott
    >> http://www.OdeToCode.com/blogs/scott/
    >>
    >> On Tue, 22 Mar 2005 16:15:15 -0500, "Support"
    >> <> wrote:
    >>
    >>>Hello:
    >>>
    >>>I have a aspx web page - let's call it main.aspx
    >>>It contains a placeholder: Myplaceholder.
    >>>
    >>>I have a web control, lets call it webcontrol which has a code behind
    >>>class
    >>>called webcontrolclass and a label called mycontrollabel
    >>>In the webcontrolclass, I have a private sub called:
    >>>Sub writetomycontrollabel(stuff as text)
    >>> mycontrollabel.text=stuff
    >>>End Sub
    >>>
    >>>The code behind main.aspx
    >>>Dim MANAGEGENERAL As Control = LoadControl("webcontrol.ascx") ' needed for
    >>>the placeholder
    >>>Public MyMANAGEGENERAL As New webcontrolclass
    >>>
    >>>
    >>>I have a sub which is called by a button located on main.aspx
    >>>Sub mainaspx_click(usualstuff ) handles mainaspx.Click
    >>>MyPlaceholder.Controls.Add(MANAGEGENERAL)
    >>> MyMANAGEGENERAL.writetomycontrollabel("hello")
    >>>End sub
    >>>
    >>>I get the error message:
    >>>Object reference not set to an instance of an object.
    >>>Exception Details: System.NullReferenceException: Object reference not set
    >>>to an instance of an object.
    >>> MyMANAGEGENERAL.writetomycontrollabel("hello")
    >>>
    >>>So, I am missing something obvious - but I dont get it.
    >>>Help
    >>>Thanks
    >>>Terry
    >>>
    >>>

    >>

    >
    Scott Allen, Mar 24, 2005
    #5
    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. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    543
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  2. Steve Richter
    Replies:
    6
    Views:
    691
    Steve Richter
    May 3, 2007
  3. Support
    Replies:
    0
    Views:
    85
    Support
    Mar 22, 2005
  4. Robert Cohen
    Replies:
    3
    Views:
    252
    Andrew Durstewitz
    Jul 15, 2003
  5. Phrogz
    Replies:
    4
    Views:
    220
    Austin Ziegler
    Sep 6, 2006
Loading...

Share This Page