views:

85

answers:

3

When building a custom control, how would you access the content between the opening and closing control tags?

<my:tag runat="server">
 <p>...markup</p>...
</my:tag>

I am currently successfully using the Render method to output arbitrary markeup, but cannot seem to find out how to access the contained markup.

+2  A: 

Take a look at this.Controls. This article : http://msdn.microsoft.com/en-us/library/system.web.ui.control.controls(VS.71).aspx states "On an ASP.NET page, when controls are added declaratively between the opening and closing tags of a server control, ASP.NET automatically adds the controls to the containing server control's ControlCollection. "

As far as I understand, if you have

<yourcode:yourcontrol id="asdf" runat="server">
  <p id="innerP" runat="server">Text here</p>
</yourcode:yourcontrol>

Then it would be possible to call this.FindControl("innerP").text="Other text here, since the P tag is generated on the server side.

However, if you do not have the runat="server" set on the P element:

<yourcode:yourcontrol id="asdf" runat="server">
  <p id="innerP">Text here</p>
</yourcode:yourcontrol>

then you only can only find it through this.controls[0] since all the content will be rendered into a single Literal control.

naivists
Awesome. Inside my Custom Control, iterating over the this.Controls and calling .Render on them allows me to capture their "output". Thanks!
James Maroney
A: 

I think you want to do this:

<my:tag runtat="server">
    <p><asp:Label id="markupLabel" runat="server"/></p>
</my:tag>

And from the code-behind

markupLabel.text = "Foo";
Whistle
A: 

If you add an ID to the my:tag tag, you should be able to access the controls inside of it using the .Controls collection of the tag.

Yoenhofen