ASP.NET User Control - accessing InnerHtml
We have a user control using DHTML that displays a button which when clicked renders a floating DIV layer with an image. It is easy to define global string variables that are set from attributes of the user control to pass parameters for the image to show and caption, but then we wanted to inject HTML from between the opening and closing tags of the User control. Creating another element for this was ruled out because this would make the control too hard for our HTML designers to use.
I could not find a solution on the internet and until someone tells me a better one, here is a technique that works:
I could not find a solution on the internet and until someone tells me a better one, here is a technique that works:
- Define a global property in the User Control with public accessor called Span.
- Place an opening and closing SPAN tag between the opening and closing tags of the User Control.
- Place your HTML code inside the SPAN.
- Add a Literal control to the design view of the User control in the place where the additional HTML should be injected.
- In the User control code, override the Render method (Note constructor cannot be used as the control has not been instantiated yet)
- Set the literal's Text property to the Span.InnerHtml property. (For gotchas, using Render, you have to check that your span object is not null because it is called several times, and remember to call base.Render(writer);