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();
hyperLink.Controls.Add(this);
hyperLink.RenderBeginTag(writer);
base.Render(writer);
RenderThroughChildren(this, writer)
hyperLink.RenderEndTag(writer);
For the second I tried:
this.Parent.Controls.Add(hyperLink);
hyperLink.Controls.Add(this);
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.