Programmatically adding a user control and setting values

Discussion in 'ASP .Net' started by alun65@gmail.com, Jan 17, 2008.

  1. Guest

    I'm having trouble programmatically adding a user control and then
    setting some of it's server controls.

    I add the user control to the code behind and add it to a placeholder:

    protected void Page_Load(object sender, EventArgs e)
    {
    UserControls_WebUserControl myControl = new
    UserControls_WebUserControl();
    PlaceHolder1.Controls.Add(web);
    }

    And I make a reference to it on the webpage:

    <%@ Register Src="~/UserControls/WebUserControl.ascx"
    TagName="myUserControl" TagPrefix="CustomControl" %>

    All good so far?

    For the user contorl, to simplify it the the Page_Load of the user
    control is trying to set a hyperlink text (as later on a will
    hopefully pass a load of values to a constructor of the user control)
    like so:

    HyperLink1.Text = "Hi I'm a hyperlink";

    I'm expecting the obvious (well obvious to me anyways). For one
    hyperlink with the text "Hi I'm a hyperlink".

    instead when I run this I get the error:

    Exception Details: System.NullReferenceException: Object reference not
    set to an instance of an object.

    When debugging the hyperlink I've placed on the user control doesn't
    seem to exist! Could any kind programmer tell this noobie what he's
    doing wrong. Thanks in advance.

    Alun
    , Jan 17, 2008
    #1
    1. Advertising

  2. grava Guest

    <> wrote in message
    news:...
    > I'm having trouble programmatically adding a user control and then
    > setting some of it's server controls.
    >
    > I add the user control to the code behind and add it to a placeholder:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > UserControls_WebUserControl myControl = new
    > UserControls_WebUserControl();
    > PlaceHolder1.Controls.Add(web);
    > }
    >
    > And I make a reference to it on the webpage:
    >
    > <%@ Register Src="~/UserControls/WebUserControl.ascx"
    > TagName="myUserControl" TagPrefix="CustomControl" %>
    >
    > All good so far?
    >
    > For the user contorl, to simplify it the the Page_Load of the user
    > control is trying to set a hyperlink text (as later on a will
    > hopefully pass a load of values to a constructor of the user control)
    > like so:
    >
    > HyperLink1.Text = "Hi I'm a hyperlink";
    >
    > I'm expecting the obvious (well obvious to me anyways). For one
    > hyperlink with the text "Hi I'm a hyperlink".
    >
    > instead when I run this I get the error:
    >
    > Exception Details: System.NullReferenceException: Object reference not
    > set to an instance of an object.
    >
    > When debugging the hyperlink I've placed on the user control doesn't
    > seem to exist! Could any kind programmer tell this noobie what he's
    > doing wrong. Thanks in advance.
    >
    > Alun


    Where is HyperLink1 Control ??? In the page or in the UserControl ???

    Try with FindControl within the scope of the container of the Hyperlink
    (page or control).

    HTH


    --
    Gianluca Gravina
    http://blogs.ugidotnet.org/thinkingingrava
    grava, Jan 17, 2008
    #2
    1. Advertising

  3. Guest

    The server contorl HyperLink1 is on the user control .ascx page.

    Thanks for the suggestion Gianluca. In the user contorl codebehind I
    tried your suggestion out:

    protected void Page_Load(object sender, EventArgs e)
    {
    ((HyperLink)(FindControl("HyperLink1"))).Text = "I am the
    hyperlink";
    }

    But I still get a System.NullReferenceException for the hyperlink. Any
    other ideas?
    , Jan 17, 2008
    #3
  4. grava Guest

    <> wrote in message
    news:...
    > The server contorl HyperLink1 is on the user control .ascx page.
    >
    > Thanks for the suggestion Gianluca. In the user contorl codebehind I
    > tried your suggestion out:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > ((HyperLink)(FindControl("HyperLink1"))).Text = "I am the
    > hyperlink";
    > }
    >
    > But I still get a System.NullReferenceException for the hyperlink. Any
    > other ideas?
    >



    Here is a working sample.

    Code in the page Code Behind:

    private Control c;

    protected void Page_Init(object sender, EventArgs e)
    {
    c = LoadControl("WebUserControl1.ascx");
    plcHolder.Controls.Add(c);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    WebUserControl1 wc = (WebUserControl1) c;
    ((TextBox) wc.FindControl("controlTextBox")).Text = "Test";
    }

    In the ascx there's a textbox with id="controlTextBox".

    HTH


    --
    Gianluca Gravina
    http://blogs.ugidotnet.org/thinkingingrava
    grava, Jan 17, 2008
    #4
  5. This sort of thing requires a familiarity with the ASP.Net Control Execution
    LifeCycle, as the Page (which is a Control) and all the Controls in it, host
    other Controls, and there is a cascade of events which occurs when the class
    is loaded. See the following:

    http://www.digcode.com/default.aspx...&article=d3ba7954-6235-446f-9801-584539cbb6bf

    --
    HTH,

    Kevin Spencer
    Chicken Salad Surgeon
    Microsoft MVP

    <> wrote in message
    news:...
    > I'm having trouble programmatically adding a user control and then
    > setting some of it's server controls.
    >
    > I add the user control to the code behind and add it to a placeholder:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > UserControls_WebUserControl myControl = new
    > UserControls_WebUserControl();
    > PlaceHolder1.Controls.Add(web);
    > }
    >
    > And I make a reference to it on the webpage:
    >
    > <%@ Register Src="~/UserControls/WebUserControl.ascx"
    > TagName="myUserControl" TagPrefix="CustomControl" %>
    >
    > All good so far?
    >
    > For the user contorl, to simplify it the the Page_Load of the user
    > control is trying to set a hyperlink text (as later on a will
    > hopefully pass a load of values to a constructor of the user control)
    > like so:
    >
    > HyperLink1.Text = "Hi I'm a hyperlink";
    >
    > I'm expecting the obvious (well obvious to me anyways). For one
    > hyperlink with the text "Hi I'm a hyperlink".
    >
    > instead when I run this I get the error:
    >
    > Exception Details: System.NullReferenceException: Object reference not
    > set to an instance of an object.
    >
    > When debugging the hyperlink I've placed on the user control doesn't
    > seem to exist! Could any kind programmer tell this noobie what he's
    > doing wrong. Thanks in advance.
    >
    > Alun
    Kevin Spencer, Jan 17, 2008
    #5
  6. Guest

    Thanks for the working code.

    From that I was able to figure out what I was doing wrong. I was
    trying to instantate the user contorl like a server control like:

    UserControls_WebUserControl myControl = new
    UserControls_WebUserControl();

    where as like you have done, I should have been using the
    LoadControl() method.

    I stumbled accross this posting that confirmed the different
    instanition methods:

    http://groups.google.com/group/micr...=user control null exception#843f908c1cae7af0

    Just as a side note for anyone interested in passing parameters to a
    user control constructor, there's a great article here that sorts it
    out:

    http://blah.winsmarts.com/2006/05/2...pass-in-constructor-parameters.aspx?postID=12

    Thanks grava for your help. My problems are now solved! At least for
    the next hour or so ;)
    , Jan 17, 2008
    #6
  7. Howdy,

    Exception you get is because UserControl are treated differently (you can't
    use constructor to instantiate), use LoadControl method instead (then, all
    references to your controls within usercontrol will be properly intantiated):

    UserControls_WebUserControl myControl =
    (UserControls_WebUserControl)
    this.LoadControl("~/UserControls/WebUserControl.ascx")
    PlaceHolder1.Controls.Add(myControl);

    In addition move the code to Page_Init event because the view state.

    Hope this helps
    --
    Milosz


    "" wrote:

    > I'm having trouble programmatically adding a user control and then
    > setting some of it's server controls.
    >
    > I add the user control to the code behind and add it to a placeholder:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > UserControls_WebUserControl myControl = new
    > UserControls_WebUserControl();
    > PlaceHolder1.Controls.Add(web);
    > }
    >
    > And I make a reference to it on the webpage:
    >
    > <%@ Register Src="~/UserControls/WebUserControl.ascx"
    > TagName="myUserControl" TagPrefix="CustomControl" %>
    >
    > All good so far?
    >
    > For the user contorl, to simplify it the the Page_Load of the user
    > control is trying to set a hyperlink text (as later on a will
    > hopefully pass a load of values to a constructor of the user control)
    > like so:
    >
    > HyperLink1.Text = "Hi I'm a hyperlink";
    >
    > I'm expecting the obvious (well obvious to me anyways). For one
    > hyperlink with the text "Hi I'm a hyperlink".
    >
    > instead when I run this I get the error:
    >
    > Exception Details: System.NullReferenceException: Object reference not
    > set to an instance of an object.
    >
    > When debugging the hyperlink I've placed on the user control doesn't
    > seem to exist! Could any kind programmer tell this noobie what he's
    > doing wrong. Thanks in advance.
    >
    > Alun
    >
    Milosz Skalecki [MCAD], Jan 17, 2008
    #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. Philipp Sumi
    Replies:
    2
    Views:
    337
    Philipp Sumi
    Jul 23, 2003
  2. Rob Meade
    Replies:
    1
    Views:
    2,216
    bruce barker
    Aug 2, 2004
  3. Travis Pruitt

    Dynamically Adding User Control with Child User Control

    Travis Pruitt, Jan 13, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    179
    Travis Pruitt
    Jan 13, 2005
  4. itmanager

    Setting impersonation values programmatically

    itmanager, Sep 14, 2006, in forum: ASP .Net Security
    Replies:
    1
    Views:
    446
    Joe Kaplan
    Sep 14, 2006
  5. Jason Dunbar

    Programmatically Adding Web User Controls

    Jason Dunbar, Nov 8, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    123
    Jason Dunbar
    Nov 8, 2004
Loading...

Share This Page