Hi Alex,
To capture the content inside your UserControl tags, you need to add
following two attributes to your UserControl:
1) ParseChildren(false)
This will instruct the parser to interpret the elements that are contained
within the server control's tags as content that will be parsed with an
associated ControlBuilder, that is, as controls.
#ParseChildrenAttribute Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.parsechildrenattribut
e.aspx
With default ControlBuilder, the content will be added to your
UserControl's Controls collection as a literal control. Based on my
research, it seem this literal control is always added as the last control.
So you could use Controls[Controls.Count-1] to access it.
But we can also utilize the LiteralControl's ControlBuilder:
LiteralControlBuilder to let the ControlBuilder capture the content into
the "Text" property. Hence we need another attribute:
2) [ControlBuilder(typeof(LiteralControlBuilder))]
This will assign the LiteralControlBuilder to your UserControl. We also
need to implement interface ITextControl to provide a Text property.
#ControlBuilderAttribute Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.controlbuilderattribu
te.aspx
#ControlBuilder Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.controlbuilder.aspx
Here's a complete code listing of the Code-Behind class of UserControl:
[ParseChildren(false)]
[ControlBuilder(typeof(LiteralControlBuilder))]
public partial class WebUserControl : System.Web.UI.UserControl,
ITextControl
{
#region ITextControl Members
public string Text
{
get
{
object text = ViewState["Text"];
if (text == null) return string.Empty;
return text as string;
}
set
{
ViewState["Text"] = value;
}
}
#endregion
}
Hope this helps. Please feel free to post here if anything is unclear.
Regards,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.