Problem with WebControl and dynamic UserControl..

Discussion in 'ASP .Net Building Controls' started by Rocky Moore, Jul 20, 2003.

  1. Rocky Moore

    Rocky Moore Guest

    I am trying to make a WebControl as a content panel which given the user
    control name and location, will create the specified user control to fill
    the content of the WebControl. It can be any user control which may contain
    buttons, edits, etc. and require postbacks.

    At this point, I dynamically create and add the control to the
    WebControl.Controls in the CreateChildren. Then in the Render, I specify to
    RenderChildren. It all appears ok but the problem is I do not see how to
    hookup validitors or state in the UserControl that is created.

    Any ideas, if this task is even possible?
    Rocky Moore, Jul 20, 2003
    #1
    1. Advertising

  2. Rocky Moore

    Rocky Moore Guest

    Not quite the target. I would like to have a ContentPanel that can display
    _any_ UserControl and allow the UserControl to function as normal. The
    ContentPanel would only know the filename and folder of the control and
    nothing more about it.

    I can currently obtain the functionality I desire by putting the code to
    dynamically generate an instance of the UserControl in the Page CodeBehind
    but if I put that code in a WebControl, the UserControl no longer maintains
    state and the Validators on differnet controls in the UserControl are never
    triggered in the UserControl.

    At this point, I am sure I am missing something that the WebControl is
    limiting on the UserControl, but not sure what it is. The UserControl is
    not getting any postback. I can set break points or traces all over any of
    the UserControls and only the constructors are being called. None of the
    custom validators. Of couse, the fact that no data is persisting was
    another clue to me ;)

    Thanks!


    "Victor Garcia Aprea [MVP]" <> wrote in message
    news:u$A4G#...
    > Hi Rocky,
    >
    > Sure its possible. Depending on your needs, you could expose some

    high-level
    > custom events in your usercontrols (ie.: LoginAttempt) that get fired by

    its
    > child controls (ie. in response to a click button of one child button).

    You
    > would then attach to this event from the custom control loading the
    > usercontrol (you will need to cast the Control-type returned by the
    > LoadControl method to your UserControl-derived type first).
    > Another way could be to just access the child controls of your user

    control
    > by accessing its Controls property, getting the child control you want to
    > attach an event handler to, casting it to the proper control-type (same as
    > with the LoadControl method, because the ControlCollection return Control
    > types), and then attaching to any event that specific control exposes.
    >
    > Wow, I repeated the word 'control' like a hundred of times, let me know if
    > this is not clear enough,
    >
    > --
    > Victor Garcia Aprea
    > Microsoft MVP | ASP.NET
    > Looking for insights on ASP.NET? Read my blog:
    > http://obies.com/vga/blog.aspx
    > To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    > and not by private mail.
    >
    > "Rocky Moore" <> wrote in message
    > news:%...
    > > I am trying to make a WebControl as a content panel which given the user
    > > control name and location, will create the specified user control to

    fill
    > > the content of the WebControl. It can be any user control which may

    > contain
    > > buttons, edits, etc. and require postbacks.
    > >
    > > At this point, I dynamically create and add the control to the
    > > WebControl.Controls in the CreateChildren. Then in the Render, I specify

    > to
    > > RenderChildren. It all appears ok but the problem is I do not see how to
    > > hookup validitors or state in the UserControl that is created.
    > >
    > > Any ideas, if this task is even possible?
    > >
    > >
    > >

    >
    >
    Rocky Moore, Jul 21, 2003
    #2
    1. Advertising

  3. Rocky,

    I've just had a great deal of luck with figuring out what was going on in
    the control lifecycle by overriding almost every method that can be
    overridden, tracing, and then calling the base class version. Here's a list:

    LoadViewState, SaveViewState, TrackViewState
    OnInit, OnLoad, OnPreRender, OnUnload
    OnBubbleEvent
    DataBind, OnDataBinding
    Render

    In each case, I had one line of Page.Trace.Write, followed by calling the
    base class version as appropriate.

    Note a problem with Page.Trace. It fails badly at design time. I centralized
    my "Page.Trace.Write" calls and added the following code:


    Debug.WriteLine(string.Format("{0:hh:mm:ss.ff} {1}", DateTime.Now, message),
    category);
    if (!InDesignMode(control) && CanTrace(control))
    {
    control.Page.Trace.Write(category, message, errorInfo);
    }

    ....

    private static bool InDesignMode(Control control)
    {
    return control != null && control.Site != null &&
    control.Site.DesignMode;
    }

    // Note that sometimes, Page.Trace is non-null, yet throws an exception when
    referenced
    private static bool CanTrace(Control control)
    {
    if (control.Page != null)
    {
    try
    {
    return (control.Page.Trace != null);
    }
    catch
    {
    return false;
    }
    }
    else
    {
    return false;
    }
    }

    This almost returned too much information. When I decided I wasn't getting
    enough information overload, I formatted and dumped out the contents of each
    control's ViewState on LoadViewState and SaveViewState, and now my page
    traces are about as large as the Sunday paper...

    John Saunders



    "Rocky Moore" <> wrote in message
    news:%236n%...
    > Not quite the target. I would like to have a ContentPanel that can

    display
    > _any_ UserControl and allow the UserControl to function as normal. The
    > ContentPanel would only know the filename and folder of the control and
    > nothing more about it.
    >
    > I can currently obtain the functionality I desire by putting the code to
    > dynamically generate an instance of the UserControl in the Page CodeBehind
    > but if I put that code in a WebControl, the UserControl no longer

    maintains
    > state and the Validators on differnet controls in the UserControl are

    never
    > triggered in the UserControl.
    >
    > At this point, I am sure I am missing something that the WebControl is
    > limiting on the UserControl, but not sure what it is. The UserControl is
    > not getting any postback. I can set break points or traces all over any

    of
    > the UserControls and only the constructors are being called. None of the
    > custom validators. Of couse, the fact that no data is persisting was
    > another clue to me ;)
    >
    > Thanks!
    >
    >
    > "Victor Garcia Aprea [MVP]" <> wrote in message
    > news:u$A4G#...
    > > Hi Rocky,
    > >
    > > Sure its possible. Depending on your needs, you could expose some

    > high-level
    > > custom events in your usercontrols (ie.: LoginAttempt) that get fired by

    > its
    > > child controls (ie. in response to a click button of one child button).

    > You
    > > would then attach to this event from the custom control loading the
    > > usercontrol (you will need to cast the Control-type returned by the
    > > LoadControl method to your UserControl-derived type first).
    > > Another way could be to just access the child controls of your user

    > control
    > > by accessing its Controls property, getting the child control you want

    to
    > > attach an event handler to, casting it to the proper control-type (same

    as
    > > with the LoadControl method, because the ControlCollection return

    Control
    > > types), and then attaching to any event that specific control exposes.
    > >
    > > Wow, I repeated the word 'control' like a hundred of times, let me know

    if
    > > this is not clear enough,
    > >
    > > --
    > > Victor Garcia Aprea
    > > Microsoft MVP | ASP.NET
    > > Looking for insights on ASP.NET? Read my blog:
    > > http://obies.com/vga/blog.aspx
    > > To contact me remove 'NOSPAM'. Please post all questions to the

    newsgroup
    > > and not by private mail.
    > >
    > > "Rocky Moore" <> wrote in message
    > > news:%...
    > > > I am trying to make a WebControl as a content panel which given the

    user
    > > > control name and location, will create the specified user control to

    > fill
    > > > the content of the WebControl. It can be any user control which may

    > > contain
    > > > buttons, edits, etc. and require postbacks.
    > > >
    > > > At this point, I dynamically create and add the control to the
    > > > WebControl.Controls in the CreateChildren. Then in the Render, I

    specify
    > > to
    > > > RenderChildren. It all appears ok but the problem is I do not see how

    to
    > > > hookup validitors or state in the UserControl that is created.
    > > >
    > > > Any ideas, if this task is even possible?
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
    John Saunders, Jul 21, 2003
    #3
  4. Rocky Moore

    Rocky Moore Guest

    Thanks for the help. I discovered my problem that I should have noticed in
    the first place. It needed the INamingContainer interface to allow the
    UserControl to function properly. I figured it had to be something simple.
    Just could not see the forest through all the trees ;)

    Rocky

    "John Saunders" <> wrote in message
    news:#...
    > Rocky,
    >
    > I've just had a great deal of luck with figuring out what was going on in
    > the control lifecycle by overriding almost every method that can be
    > overridden, tracing, and then calling the base class version. Here's a

    list:
    >
    > LoadViewState, SaveViewState, TrackViewState
    > OnInit, OnLoad, OnPreRender, OnUnload
    > OnBubbleEvent
    > DataBind, OnDataBinding
    > Render
    >
    > In each case, I had one line of Page.Trace.Write, followed by calling the
    > base class version as appropriate.
    >
    > Note a problem with Page.Trace. It fails badly at design time. I

    centralized
    > my "Page.Trace.Write" calls and added the following code:
    >
    >
    > Debug.WriteLine(string.Format("{0:hh:mm:ss.ff} {1}", DateTime.Now,

    message),
    > category);
    > if (!InDesignMode(control) && CanTrace(control))
    > {
    > control.Page.Trace.Write(category, message, errorInfo);
    > }
    >
    > ...
    >
    > private static bool InDesignMode(Control control)
    > {
    > return control != null && control.Site != null &&
    > control.Site.DesignMode;
    > }
    >
    > // Note that sometimes, Page.Trace is non-null, yet throws an exception

    when
    > referenced
    > private static bool CanTrace(Control control)
    > {
    > if (control.Page != null)
    > {
    > try
    > {
    > return (control.Page.Trace != null);
    > }
    > catch
    > {
    > return false;
    > }
    > }
    > else
    > {
    > return false;
    > }
    > }
    >
    > This almost returned too much information. When I decided I wasn't getting
    > enough information overload, I formatted and dumped out the contents of

    each
    > control's ViewState on LoadViewState and SaveViewState, and now my page
    > traces are about as large as the Sunday paper...
    >
    > John Saunders
    >
    >
    >
    > "Rocky Moore" <> wrote in message
    > news:%236n%...
    > > Not quite the target. I would like to have a ContentPanel that can

    > display
    > > _any_ UserControl and allow the UserControl to function as normal. The
    > > ContentPanel would only know the filename and folder of the control and
    > > nothing more about it.
    > >
    > > I can currently obtain the functionality I desire by putting the code to
    > > dynamically generate an instance of the UserControl in the Page

    CodeBehind
    > > but if I put that code in a WebControl, the UserControl no longer

    > maintains
    > > state and the Validators on differnet controls in the UserControl are

    > never
    > > triggered in the UserControl.
    > >
    > > At this point, I am sure I am missing something that the WebControl is
    > > limiting on the UserControl, but not sure what it is. The UserControl

    is
    > > not getting any postback. I can set break points or traces all over any

    > of
    > > the UserControls and only the constructors are being called. None of

    the
    > > custom validators. Of couse, the fact that no data is persisting was
    > > another clue to me ;)
    > >
    > > Thanks!
    > >
    > >
    > > "Victor Garcia Aprea [MVP]" <> wrote in message
    > > news:u$A4G#...
    > > > Hi Rocky,
    > > >
    > > > Sure its possible. Depending on your needs, you could expose some

    > > high-level
    > > > custom events in your usercontrols (ie.: LoginAttempt) that get fired

    by
    > > its
    > > > child controls (ie. in response to a click button of one child

    button).
    > > You
    > > > would then attach to this event from the custom control loading the
    > > > usercontrol (you will need to cast the Control-type returned by the
    > > > LoadControl method to your UserControl-derived type first).
    > > > Another way could be to just access the child controls of your user

    > > control
    > > > by accessing its Controls property, getting the child control you want

    > to
    > > > attach an event handler to, casting it to the proper control-type

    (same
    > as
    > > > with the LoadControl method, because the ControlCollection return

    > Control
    > > > types), and then attaching to any event that specific control exposes.
    > > >
    > > > Wow, I repeated the word 'control' like a hundred of times, let me

    know
    > if
    > > > this is not clear enough,
    > > >
    > > > --
    > > > Victor Garcia Aprea
    > > > Microsoft MVP | ASP.NET
    > > > Looking for insights on ASP.NET? Read my blog:
    > > > http://obies.com/vga/blog.aspx
    > > > To contact me remove 'NOSPAM'. Please post all questions to the

    > newsgroup
    > > > and not by private mail.
    > > >
    > > > "Rocky Moore" <> wrote in message
    > > > news:%...
    > > > > I am trying to make a WebControl as a content panel which given the

    > user
    > > > > control name and location, will create the specified user control to

    > > fill
    > > > > the content of the WebControl. It can be any user control which may
    > > > contain
    > > > > buttons, edits, etc. and require postbacks.
    > > > >
    > > > > At this point, I dynamically create and add the control to the
    > > > > WebControl.Controls in the CreateChildren. Then in the Render, I

    > specify
    > > > to
    > > > > RenderChildren. It all appears ok but the problem is I do not see

    how
    > to
    > > > > hookup validitors or state in the UserControl that is created.
    > > > >
    > > > > Any ideas, if this task is even possible?
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Rocky Moore, Jul 22, 2003
    #4
    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. huobazi
    Replies:
    1
    Views:
    682
    David Waz...
    Jul 3, 2003
  2. Guest
    Replies:
    2
    Views:
    729
  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:
    195
    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:
    183
    Kasabaarde Sumta
    Dec 13, 2004
  5. CubeFish

    Webcontrol containing a UserControl?? Problem ...

    CubeFish, Feb 21, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    113
    CubeFish
    Feb 21, 2004
Loading...

Share This Page