Controls still Nothing when UserControl's New() method is called

Discussion in 'ASP .Net Building Controls' started by Nathan Sokalski, Sep 5, 2007.

  1. I have a UserControl (*.ascx) that has the following design:

    <div id="divFileDir" runat="server">
    <asp:Image ID="imgIcon" runat="server" CssClass="icon" Height="16px"
    Width="16px"/>
    <asp:LinkButton ID="lnkName" runat="server" CssClass="filedir"
    CausesValidation="false"/>
    <asp:Label ID="lblSize" runat="server" CssClass="size"/>
    <asp:Label ID="lblDate" runat="server" CssClass="date"/>
    </div>

    The Set method of some of the properties I have declared in the codebehind
    set certain attributes (such as Text). In the overloaded New() method, I
    assign certain values to these properties, therefore calling their Set
    method. This causes the following error:

    Object reference not set to an instance of an object.

    The Objects that are not set to an instance of an object are the Controls in
    the *.ascx file. What can I do about this to allow me to set the properties
    when the UserControl is created? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Sep 5, 2007
    #1
    1. Advertising

  2. > What can I do about this to allow me to set the properties when
    > the UserControl is created?


    Constructor is too early as it is called before any child control is
    created. Child controls are safe to access in Init override like
    following.

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    // here
    }
    Muhammad Naveed Yaseen, Sep 5, 2007
    #2
    1. Advertising

  3. Nathan Sokalski

    bruce barker Guest

    use OnInit. the controls defined in the aspx file are not created in the
    constructor, but during an initialization event.

    -- bruce (sqlwork.com)

    Nathan Sokalski wrote:
    > I have a UserControl (*.ascx) that has the following design:
    >
    > <div id="divFileDir" runat="server">
    > <asp:Image ID="imgIcon" runat="server" CssClass="icon" Height="16px"
    > Width="16px"/>
    > <asp:LinkButton ID="lnkName" runat="server" CssClass="filedir"
    > CausesValidation="false"/>
    > <asp:Label ID="lblSize" runat="server" CssClass="size"/>
    > <asp:Label ID="lblDate" runat="server" CssClass="date"/>
    > </div>
    >
    > The Set method of some of the properties I have declared in the codebehind
    > set certain attributes (such as Text). In the overloaded New() method, I
    > assign certain values to these properties, therefore calling their Set
    > method. This causes the following error:
    >
    > Object reference not set to an instance of an object.
    >
    > The Objects that are not set to an instance of an object are the Controls in
    > the *.ascx file. What can I do about this to allow me to set the properties
    > when the UserControl is created? Thanks.
    bruce barker, Sep 5, 2007
    #3
  4. Are you creating the control as you would any ordinary object suchas

    ControlClass myControl = new ControlClass();?

    If so, you're going to get a null every time you hit one of the asp.net
    controls that's in the design surface. The reason is, using the new
    assignment all you are doing is creating a new instance of the class, not
    instantiating the full control. The difference, using new creates the class
    without the design surface of the .ascx file. To correctly add a control to
    a page programatically you need to use the LoadControl method of the page
    such as:

    ControlClass myControl = (ControlClass)Page.LoadControl(pathtomyascxfile);


    --
    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage

    "Nathan Sokalski" <> wrote in message
    news:...
    >I have a UserControl (*.ascx) that has the following design:
    >
    > <div id="divFileDir" runat="server">
    > <asp:Image ID="imgIcon" runat="server" CssClass="icon" Height="16px"
    > Width="16px"/>
    > <asp:LinkButton ID="lnkName" runat="server" CssClass="filedir"
    > CausesValidation="false"/>
    > <asp:Label ID="lblSize" runat="server" CssClass="size"/>
    > <asp:Label ID="lblDate" runat="server" CssClass="date"/>
    > </div>
    >
    > The Set method of some of the properties I have declared in the codebehind
    > set certain attributes (such as Text). In the overloaded New() method, I
    > assign certain values to these properties, therefore calling their Set
    > method. This causes the following error:
    >
    > Object reference not set to an instance of an object.
    >
    > The Objects that are not set to an instance of an object are the Controls
    > in the *.ascx file. What can I do about this to allow me to set the
    > properties when the UserControl is created? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Mark Fitzpatrick, Sep 5, 2007
    #4
  5. If I were to use the Page.LoadControl() method, how can I pass parameters
    like I do with New()?
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Mark Fitzpatrick" <> wrote in message
    news:%23n$...
    > Are you creating the control as you would any ordinary object suchas
    >
    > ControlClass myControl = new ControlClass();?
    >
    > If so, you're going to get a null every time you hit one of the asp.net
    > controls that's in the design surface. The reason is, using the new
    > assignment all you are doing is creating a new instance of the class, not
    > instantiating the full control. The difference, using new creates the
    > class without the design surface of the .ascx file. To correctly add a
    > control to a page programatically you need to use the LoadControl method
    > of the page such as:
    >
    > ControlClass myControl = (ControlClass)Page.LoadControl(pathtomyascxfile);
    >
    >
    > --
    > Hope this helps,
    > Mark Fitzpatrick
    > Microsoft MVP - FrontPage
    >
    > "Nathan Sokalski" <> wrote in message
    > news:...
    >>I have a UserControl (*.ascx) that has the following design:
    >>
    >> <div id="divFileDir" runat="server">
    >> <asp:Image ID="imgIcon" runat="server" CssClass="icon" Height="16px"
    >> Width="16px"/>
    >> <asp:LinkButton ID="lnkName" runat="server" CssClass="filedir"
    >> CausesValidation="false"/>
    >> <asp:Label ID="lblSize" runat="server" CssClass="size"/>
    >> <asp:Label ID="lblDate" runat="server" CssClass="date"/>
    >> </div>
    >>
    >> The Set method of some of the properties I have declared in the
    >> codebehind set certain attributes (such as Text). In the overloaded New()
    >> method, I assign certain values to these properties, therefore calling
    >> their Set method. This causes the following error:
    >>
    >> Object reference not set to an instance of an object.
    >>
    >> The Objects that are not set to an instance of an object are the Controls
    >> in the *.ascx file. What can I do about this to allow me to set the
    >> properties when the UserControl is created? Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    Nathan Sokalski, Sep 5, 2007
    #5
  6. I have tried using the Init event (I also tried the Load event) of the
    *.ascx.vb file, and I still recieve the error.

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Init
    Me.lnkName.Text = Me.iteminfovalue.Name()
    Me.lblDate.Text = Me.iteminfovalue.LastWriteTime.ToShortDateString()
    End Sub

    What am I doing wrong here? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Muhammad Naveed Yaseen" <> wrote in message
    news:...
    >> What can I do about this to allow me to set the properties when
    >> the UserControl is created?

    >
    > Constructor is too early as it is called before any child control is
    > created. Child controls are safe to access in Init override like
    > following.
    >
    > protected override void OnInit(EventArgs e)
    > {
    > base.OnInit(e);
    > // here
    > }
    >
    Nathan Sokalski, Sep 5, 2007
    #6
  7. First, this is the codebehind of an *.ascx file, not *.aspx. Therefore, the
    code that sets the values for the properties should be in the *.ascx.vb
    file. I have tried using the Init (and the Load) events of the *.ascx.vb,
    but I still recieve the same error. What am I doing wrong? Thanks.

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Init
    Me.lnkName.Text = Me.iteminfovalue.Name()
    Me.lblDate.Text = Me.iteminfovalue.LastWriteTime.ToShortDateString()
    End Sub
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "bruce barker" <> wrote in message
    news:...
    > use OnInit. the controls defined in the aspx file are not created in the
    > constructor, but during an initialization event.
    >
    > -- bruce (sqlwork.com)
    >
    > Nathan Sokalski wrote:
    >> I have a UserControl (*.ascx) that has the following design:
    >>
    >> <div id="divFileDir" runat="server">
    >> <asp:Image ID="imgIcon" runat="server" CssClass="icon" Height="16px"
    >> Width="16px"/>
    >> <asp:LinkButton ID="lnkName" runat="server" CssClass="filedir"
    >> CausesValidation="false"/>
    >> <asp:Label ID="lblSize" runat="server" CssClass="size"/>
    >> <asp:Label ID="lblDate" runat="server" CssClass="date"/>
    >> </div>
    >>
    >> The Set method of some of the properties I have declared in the
    >> codebehind set certain attributes (such as Text). In the overloaded New()
    >> method, I assign certain values to these properties, therefore calling
    >> their Set method. This causes the following error:
    >>
    >> Object reference not set to an instance of an object.
    >>
    >> The Objects that are not set to an instance of an object are the Controls
    >> in the *.ascx file. What can I do about this to allow me to set the
    >> properties when the UserControl is created? Thanks.
    Nathan Sokalski, Sep 5, 2007
    #7
  8. Nathan Sokalski

    Teemu Keiski Guest

    Define the needed things as properties on the control, which you set when
    you have the instance at hand.

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net

    "Nathan Sokalski" <> wrote in message
    news:%23$...
    > If I were to use the Page.LoadControl() method, how can I pass parameters
    > like I do with New()?
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    > "Mark Fitzpatrick" <> wrote in message
    > news:%23n$...
    >> Are you creating the control as you would any ordinary object suchas
    >>
    >> ControlClass myControl = new ControlClass();?
    >>
    >> If so, you're going to get a null every time you hit one of the asp.net
    >> controls that's in the design surface. The reason is, using the new
    >> assignment all you are doing is creating a new instance of the class, not
    >> instantiating the full control. The difference, using new creates the
    >> class without the design surface of the .ascx file. To correctly add a
    >> control to a page programatically you need to use the LoadControl method
    >> of the page such as:
    >>
    >> ControlClass myControl =
    >> (ControlClass)Page.LoadControl(pathtomyascxfile);
    >>
    >>
    >> --
    >> Hope this helps,
    >> Mark Fitzpatrick
    >> Microsoft MVP - FrontPage
    >>
    >> "Nathan Sokalski" <> wrote in message
    >> news:...
    >>>I have a UserControl (*.ascx) that has the following design:
    >>>
    >>> <div id="divFileDir" runat="server">
    >>> <asp:Image ID="imgIcon" runat="server" CssClass="icon" Height="16px"
    >>> Width="16px"/>
    >>> <asp:LinkButton ID="lnkName" runat="server" CssClass="filedir"
    >>> CausesValidation="false"/>
    >>> <asp:Label ID="lblSize" runat="server" CssClass="size"/>
    >>> <asp:Label ID="lblDate" runat="server" CssClass="date"/>
    >>> </div>
    >>>
    >>> The Set method of some of the properties I have declared in the
    >>> codebehind set certain attributes (such as Text). In the overloaded
    >>> New() method, I assign certain values to these properties, therefore
    >>> calling their Set method. This causes the following error:
    >>>
    >>> Object reference not set to an instance of an object.
    >>>
    >>> The Objects that are not set to an instance of an object are the
    >>> Controls in the *.ascx file. What can I do about this to allow me to set
    >>> the properties when the UserControl is created? Thanks.
    >>> --
    >>> Nathan Sokalski
    >>>
    >>> http://www.nathansokalski.com/
    >>>

    >>
    >>

    >
    >
    Teemu Keiski, Sep 5, 2007
    #8
    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. Nathan Sokalski
    Replies:
    7
    Views:
    935
    Teemu Keiski
    Sep 5, 2007
  2. Replies:
    1
    Views:
    181
  3. Devin Jeanpierre
    Replies:
    2
    Views:
    455
    Devin Jeanpierre
    Feb 14, 2012
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    133
    Mohsen Pahlevanzadeh
    Sep 3, 2013
  5. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    117
    Mohsen Pahlevanzadeh
    Sep 3, 2013
Loading...

Share This Page