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

Discussion in 'ASP .Net' 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, Sep 5, 2007
    #1
    1. Advertisements

  2. What can I do about this to allow me to set the properties when
    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. Advertisements

  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)
     
    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);
     
    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, 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, 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, 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, Sep 5, 2007
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.