How can I pass parameters to a user control that is loaded at into a Placeholder at runtime?

Discussion in 'ASP .Net' started by keith, Apr 29, 2007.

  1. keith

    keith Guest

    I use this code to load a user control at runtime:

    Control c = Page.LoadControl(Request.ApplicationPath +
    "/User_Controls/website_design.ascx");
    UserControlPlaceHolder.Controls.Add(c);

    Immediately after loading the control, I need to set a property of the user
    control.
    I have tried various ways of directly referencing the control without
    success. I have also tried using FindControl, but all I get is a null value.
    Any suggestions?

    Thanks,

    Keith
    keith, Apr 29, 2007
    #1
    1. Advertising

  2. You already have a reference to the control in variable c. You just need to
    typecast it to your type. One of the options:

    MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
    "/User_Controls/website_design.ascx");
    c.MyProperty = myValue;



    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "keith" <> wrote in message
    news:u6qg%...
    >I use this code to load a user control at runtime:
    >
    > Control c = Page.LoadControl(Request.ApplicationPath +
    > "/User_Controls/website_design.ascx");
    > UserControlPlaceHolder.Controls.Add(c);
    >
    > Immediately after loading the control, I need to set a property of the
    > user control.
    > I have tried various ways of directly referencing the control without
    > success. I have also tried using FindControl, but all I get is a null
    > value. Any suggestions?
    >
    > Thanks,
    >
    > Keith
    >
    Eliyahu Goldin, Apr 29, 2007
    #2
    1. Advertising

  3. keith

    Vili Guest

    Re: How can I pass parameters to a user control that is loaded atinto a Placeholder at runtime?

    keith wrote:
    > I use this code to load a user control at runtime:
    >
    > Control c = Page.LoadControl(Request.ApplicationPath +
    > "/User_Controls/website_design.ascx");
    > UserControlPlaceHolder.Controls.Add(c);
    >
    > Immediately after loading the control, I need to set a property of the user
    > control.
    > I have tried various ways of directly referencing the control without
    > success. I have also tried using FindControl, but all I get is a null value.
    > Any suggestions?
    >
    > Thanks,
    >
    > Keith


    Hi Keith

    I hope I remember this right (can't check it atm).
    Try to set the id property for the control or it will have a generic id.
    Vili, Apr 29, 2007
    #3
  4. keith

    keith Guest

    Thanks for your help. I changed my code to:

    MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
    "/User_Controls/website_design.ascx");
    c.Item = "abc";
    UserControlPlaceHolder.Controls.Add(c);

    When I build the page, I get the following error messages:

    The type or namespace name 'MyControl' could not be found (are you missing a
    using directive or an assembly reference?)

    The best overloaded method match for
    'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some
    invalid arguments

    Argument '1': cannot convert from 'MyControl' to 'System.Web.UI.Control'

    What am I doing wrong here?

    Thanks,

    Keith



    "Eliyahu Goldin" <> wrote in
    message news:...
    > You already have a reference to the control in variable c. You just need
    > to typecast it to your type. One of the options:
    >
    > MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
    > "/User_Controls/website_design.ascx");
    > c.MyProperty = myValue;
    >
    >
    >
    > --
    > Eliyahu Goldin,
    > Software Developer & Consultant
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "keith" <> wrote in message
    > news:u6qg%...
    >>I use this code to load a user control at runtime:
    >>
    >> Control c = Page.LoadControl(Request.ApplicationPath +
    >> "/User_Controls/website_design.ascx");
    >> UserControlPlaceHolder.Controls.Add(c);
    >>
    >> Immediately after loading the control, I need to set a property of the
    >> user control.
    >> I have tried various ways of directly referencing the control without
    >> success. I have also tried using FindControl, but all I get is a null
    >> value. Any suggestions?
    >>
    >> Thanks,
    >>
    >> Keith
    >>

    >
    >
    keith, Apr 29, 2007
    #4
  5. keith

    keith Guest

    Thanks for answering. How can I set the ID property for the user control?
    When I look in Properties, all I see are File Name and Full Path.

    -Keith


    "Vili" <> wrote in message
    news:4634503c$0$31530$...
    > keith wrote:
    >> I use this code to load a user control at runtime:
    >>
    >> Control c = Page.LoadControl(Request.ApplicationPath +
    >> "/User_Controls/website_design.ascx");
    >> UserControlPlaceHolder.Controls.Add(c);
    >>
    >> Immediately after loading the control, I need to set a property of the
    >> user control.
    >> I have tried various ways of directly referencing the control without
    >> success. I have also tried using FindControl, but all I get is a null
    >> value. Any suggestions?
    >>
    >> Thanks,
    >>
    >> Keith

    >
    > Hi Keith
    >
    > I hope I remember this right (can't check it atm).
    > Try to set the id property for the control or it will have a generic id.
    keith, Apr 29, 2007
    #5
  6. Did you include the control in the page?

    How to: Include a User Control in an ASP.NET Web Page
    http://msdn2.microsoft.com/en-us/library/sbz9etab.aspx


    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "keith" <> wrote in message
    news:...
    > Thanks for your help. I changed my code to:
    >
    > MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
    > "/User_Controls/website_design.ascx");
    > c.Item = "abc";
    > UserControlPlaceHolder.Controls.Add(c);
    >
    > When I build the page, I get the following error messages:
    >
    > The type or namespace name 'MyControl' could not be found (are you missing
    > a using directive or an assembly reference?)
    >
    > The best overloaded method match for
    > 'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some
    > invalid arguments
    >
    > Argument '1': cannot convert from 'MyControl' to 'System.Web.UI.Control'
    >
    > What am I doing wrong here?
    >
    > Thanks,
    >
    > Keith
    >
    >
    >
    > "Eliyahu Goldin" <> wrote in
    > message news:...
    >> You already have a reference to the control in variable c. You just need
    >> to typecast it to your type. One of the options:
    >>
    >> MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
    >> "/User_Controls/website_design.ascx");
    >> c.MyProperty = myValue;
    >>
    >>
    >>
    >> --
    >> Eliyahu Goldin,
    >> Software Developer & Consultant
    >> Microsoft MVP [ASP.NET]
    >> http://msmvps.com/blogs/egoldin
    >> http://usableasp.net
    >>
    >>
    >> "keith" <> wrote in message
    >> news:u6qg%...
    >>>I use this code to load a user control at runtime:
    >>>
    >>> Control c = Page.LoadControl(Request.ApplicationPath +
    >>> "/User_Controls/website_design.ascx");
    >>> UserControlPlaceHolder.Controls.Add(c);
    >>>
    >>> Immediately after loading the control, I need to set a property of the
    >>> user control.
    >>> I have tried various ways of directly referencing the control without
    >>> success. I have also tried using FindControl, but all I get is a null
    >>> value. Any suggestions?
    >>>
    >>> Thanks,
    >>>
    >>> Keith
    >>>

    >>
    >>

    >
    >
    Eliyahu Goldin, Apr 29, 2007
    #6
  7. keith

    Vili Guest

    Re: How can I pass parameters to a user control that is loaded atinto a Placeholder at runtime?

    Hi

    In your code behind when you set the control

    Control c = Page.LoadControl(Request.ApplicationPath +
    "/User_Controls/website_design.ascx");
    c.Id = "myId"
    UserControlPlaceHolder.Controls.Add(c);

    After this you should be able to find the control with FindControl("myId")

    keith wrote:
    > Thanks for answering. How can I set the ID property for the user control?
    > When I look in Properties, all I see are File Name and Full Path.
    >
    > -Keith
    >
    >
    > "Vili" <> wrote in message
    > news:4634503c$0$31530$...
    >> keith wrote:
    >>> I use this code to load a user control at runtime:
    >>>
    >>> Control c = Page.LoadControl(Request.ApplicationPath +
    >>> "/User_Controls/website_design.ascx");
    >>> UserControlPlaceHolder.Controls.Add(c);
    >>>
    >>> Immediately after loading the control, I need to set a property of the
    >>> user control.
    >>> I have tried various ways of directly referencing the control without
    >>> success. I have also tried using FindControl, but all I get is a null
    >>> value. Any suggestions?
    >>>
    >>> Thanks,
    >>>
    >>> Keith

    >> Hi Keith
    >>
    >> I hope I remember this right (can't check it atm).
    >> Try to set the id property for the control or it will have a generic id.

    >
    >
    Vili, Apr 29, 2007
    #7
  8. keith

    keith Guest

    Thanks for helping. Taking your suggestion, using the following code,
    FindControl still turns up a null;

    Control c;
    c = Page.LoadControl(Request.ApplicationPath +
    "/User_Controls/website_design.ascx");
    c.ID = "website_design_user_control";
    UserControlPlaceHolder.Controls.Add(c);
    Control thisControl = FindControl("website_design_user_control");



    "Vili" <> wrote in message
    news:46346f17$0$31516$...
    > Hi
    >
    > In your code behind when you set the control
    >
    > Control c = Page.LoadControl(Request.ApplicationPath +
    > "/User_Controls/website_design.ascx");
    > c.Id = "myId"
    > UserControlPlaceHolder.Controls.Add(c);
    >
    > After this you should be able to find the control with FindControl("myId")
    >
    > keith wrote:
    >> Thanks for answering. How can I set the ID property for the user control?
    >> When I look in Properties, all I see are File Name and Full Path.
    >>
    >> -Keith
    >>
    >>
    >> "Vili" <> wrote in message
    >> news:4634503c$0$31530$...
    >>> keith wrote:
    >>>> I use this code to load a user control at runtime:
    >>>>
    >>>> Control c = Page.LoadControl(Request.ApplicationPath +
    >>>> "/User_Controls/website_design.ascx");
    >>>> UserControlPlaceHolder.Controls.Add(c);
    >>>>
    >>>> Immediately after loading the control, I need to set a property of the
    >>>> user control.
    >>>> I have tried various ways of directly referencing the control without
    >>>> success. I have also tried using FindControl, but all I get is a null
    >>>> value. Any suggestions?
    >>>>
    >>>> Thanks,
    >>>>
    >>>> Keith
    >>> Hi Keith
    >>>
    >>> I hope I remember this right (can't check it atm).
    >>> Try to set the id property for the control or it will have a generic id.

    >>
    keith, Apr 29, 2007
    #8
  9. keith

    Vili Guest

    Re: How can I pass parameters to a user control that is loaded atinto a Placeholder at runtime?

    Try to test if you can find the control right after you add it.

    keith wrote:
    > Thanks for helping. Taking your suggestion, using the following code,
    > FindControl still turns up a null;
    >
    > Control c;
    > c = Page.LoadControl(Request.ApplicationPath +
    > "/User_Controls/website_design.ascx");
    > c.ID = "website_design_user_control";
    > UserControlPlaceHolder.Controls.Add(c);
    > Control thisControl = FindControl("website_design_user_control");
    >
    Vili, Apr 29, 2007
    #9
  10. Pass Parameters to User Control

    Control tp = (Control)Page.LoadControl("~/[YourControl].ascx");
    tp.ID = "[YourControl]";

    Type typ = tp.GetType();

    System.Reflection.PropertyInfo pi = typ.GetProperty("CategoryId");

    pi.SetValue(tp, [propertyName], null);

    [Your PlaceHolder].Controls.Add(tp);
    Graham Underwood, Aug 8, 2008
    #10
    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. MikeM

    Dynamicly loaded User Control events in a Placeholder

    MikeM, Apr 9, 2004, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    195
    Jonathan Roberts
    Apr 25, 2004
  2. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    323
  3. Ralph Krausse
    Replies:
    1
    Views:
    209
    John Saunders
    Sep 15, 2004
  4. david mccormack
    Replies:
    1
    Views:
    239
    Ishigava Butahhi
    Aug 28, 2003
  5. Ralph Krausse
    Replies:
    2
    Views:
    371
Loading...

Share This Page