Add a new parent control to an existing user control...? RRS feed

  • Question

  • User1572508060 posted

    Hi there. Sorry if this has been covered before but I couldn't find any reference to it.

    I have a user control which is based on an asp.net image control. One of the custom properties allows you to make the image 'clickable', which I would like to do by surrounding the custom image tag with a hyperlink (although also have an option to make it a linkbutton eventually).

    I've tried a couple of ways, and neither has quite worked:

    * Overriding the render method, and rendering the begin and end tags of a new hyperlink control right before and just after rendering the base control. This worked fine except that it ignored the rest of a template that I'd allowed to be defined for this user control (for various reasons). Incidentally, if I didn't elect to hyperlink the control, the full template was rendered correctly.

    * In the onload method, trying to add a new hyperlink to the parent control, and changing the parent of the user control to this new hyperlink. I wasn't entirely expecting this to work, and it didn't! (It complains that "The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.".)


    Code for the first test is:

    HyperLink hyperLink = new HyperLink();
    RenderThroughChildren(this, writer)

    For the second I tried:


    Should that be the other way around (getting a reference to the Parent first), or should I just be doing this in another event? Or am I trying something insane that can be achieved much better?

    Thanks so much for your time. 

    Wednesday, April 23, 2008 10:01 AM

All replies

  • User1735976268 posted

    Couldn't you just inherit from ImageButton?

    Wednesday, April 23, 2008 10:29 AM
  • User1572508060 posted

     Thanks for your feedback, but not really, for a few reasons:

    • The image-based component is significant and in-use. (It's a system for creating and displaying a thumbnail of a supplied image.) I really want to enhance it rather than completely change it.
    • The ImageButton is fine if I always wanted LinkButton-style functionality, but most of the time I just want it to be a standard hyperlink.
    • This is something the developer should be able to switch on and switch off in design time. The reason it's templated is that you could just have the thumbnail image, or you can add a placeholder for the filename, filesize, etc., to enhance the 'look'.

    I appreciate your thoughts, though.




    Wednesday, April 23, 2008 10:47 AM