Dynamically load webcontrol

Discussion in 'ASP .Net Web Controls' started by Allan Ebdrup, Jan 5, 2006.

  1. Allan Ebdrup

    Allan Ebdrup Guest

    Hi
    I have a scenario where I have a webcontrol on an ASP.Net 2.0 page, in some
    cases I want to dynamically load another webcontrol instead of the control I
    have. I want to load one of several possible webcontrols instead, and I
    would like it to be so that I don't have to add a reference to all the
    possible alternate webcontrols.
    Is this possible?

    Kind Regards,
    Allan Ebdrup
     
    Allan Ebdrup, Jan 5, 2006
    #1
    1. Advertising

  2. Allan Ebdrup

    Demetri Guest

    Page.Controls.Add() is your friend.
    --
    -Demetri


    "Allan Ebdrup" wrote:

    > Hi
    > I have a scenario where I have a webcontrol on an ASP.Net 2.0 page, in some
    > cases I want to dynamically load another webcontrol instead of the control I
    > have. I want to load one of several possible webcontrols instead, and I
    > would like it to be so that I don't have to add a reference to all the
    > possible alternate webcontrols.
    > Is this possible?
    >
    > Kind Regards,
    > Allan Ebdrup
    >
    >
    >
     
    Demetri, Jan 5, 2006
    #2
    1. Advertising

  3. As is Control.LoadControl(String)
     
    Keith Patrick, Jan 11, 2006
    #3
  4. Allan Ebdrup

    Allan Ebdrup Guest

    OK
    Now I've tried the following:
    Create a control that's called DynamicLoad with a Label in it, and inserted
    it on an ASP page Now I want to swap the DynamicLoad control with another
    control, I try to find the control in Page.Controls like this:
    ---
    foreach(Control c in Page.Controls){
    Response.Write("c.GetType:" + c.GetType().ToString() + "<br>");
    }
    ---
    But the output is:
    ---
    c.GetType:System.Web.UI.LiteralControl
    c.GetType:System.Web.UI.HtmlControls.HtmlHead
    c.GetType:System.Web.UI.LiteralControl
    c.GetType:System.Web.UI.HtmlControls.HtmlForm
    c.GetType:System.Web.UI.LiteralControl
    ---

    So how do I find the DynamicLoad control and swap it, when Page.Controls
    contains the controls inside the DynamicLoad control?

    Kind Regards,
    Allan Ebdrup
     
    Allan Ebdrup, Jan 12, 2006
    #4
  5. Allan Ebdrup

    Allan Ebdrup Guest

    Found the problem, the control was nested inside the form
     
    Allan Ebdrup, Jan 12, 2006
    #5
  6. Allan Ebdrup

    Allan Ebdrup Guest

    Now I've found the control I want to swap in the variable c, and I try to
    execute the following:
    Page.Controls[Page.Controls.IndexOf(c)] = new
    System.Web.UI.HtmlControls.HtmlButton();

    But I get an error that Page.Controls is read only, how do I swap the
    control?
    Is there some other event than Page_Load that allows you to modify
    Page.Controls?

    Kind Regards,
    Allan Ebdrup
     
    Allan Ebdrup, Jan 12, 2006
    #6
  7. Allan Ebdrup

    Allan Ebdrup Guest

    Tried Page_Init, still can't modify Page.Controls
     
    Allan Ebdrup, Jan 12, 2006
    #7
  8. Allan Ebdrup

    Allan Ebdrup Guest

    Now I've tried to override FrameworkInitialize, and the first thing I do in
    my override is call base.FrameworkInitialize, but when I loop through the
    controls on the page I can't find the control I want to swap, it doesn't
    seem to be loaded yet.

    Kind Regards,
    Allan Ebdrup
     
    Allan Ebdrup, Jan 12, 2006
    #8
  9. Allan Ebdrup

    Allan Ebdrup Guest

    Found the problem, seems you cant change page.controls in the control, if i
    do the swap in the main pages Page_Load there is no problem.
     
    Allan Ebdrup, Jan 12, 2006
    #9
    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. Guest
    Replies:
    2
    Views:
    755
  2. venky

    Page Load and webcontrol page load

    venky, Jul 6, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    3,180
    venky
    Jul 11, 2005
  3. Luis Ramírez.

    Can a webcontrol (webcontrol.dll) have a configuration file?

    Luis Ramírez., Jul 5, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    218
    Luis Ramírez
    Jul 6, 2004
  4. Kasabaarde Sumta

    WebControl inside of WebControl and attaching events

    Kasabaarde Sumta, Dec 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    212
    Kasabaarde Sumta
    Dec 13, 2004
  5. Fabricio

    Custom webcontrol that references another webcontrol

    Fabricio, Sep 21, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    201
    Fabricio
    Sep 21, 2005
Loading...

Share This Page