Accessing Content INSIDE a Web Control


G

Guest

Hi. If I create a WebControl (User Control, actually), I know how, easily, to
access the design-time Properties that have been set as Propertiy nodes in
the tag used on the ASPX page. But I've never tried having CONTENT inside my
Control's Tag, e.g.:

<Ax:MyCtl id="Something" runat="server">
Some stuff inside the tag
</Ax:MyCtl>

How do I access the body from the code of my Control (i.e. above, "Some
stuff inside the tag")?

Thanks.

Alex
 
Ad

Advertisements

L

Laurent Bugnion

Hi,

Alex said:
Hi. If I create a WebControl (User Control, actually), I know how, easily, to
access the design-time Properties that have been set as Propertiy nodes in
the tag used on the ASPX page. But I've never tried having CONTENT inside my
Control's Tag, e.g.:

<Ax:MyCtl id="Something" runat="server">
Some stuff inside the tag
</Ax:MyCtl>

How do I access the body from the code of my Control (i.e. above, "Some
stuff inside the tag")?

Thanks.

Alex

I might be wrong (I don't usually do user controls, only custom
controls), but isn't the content ("children") of a control placed in the
Controls collection? This is what happens for Custom controls at least.

HTH,
Laurent
 
G

Guest

Hi. Well, my UserControl *does* have a Controls collection property, but I
really don't know where to go from there. I mean, the text inside the
opn/close tag of my control isn't really a control, right? It's just the body
text. How do I get to that?

Alex
 
G

Guest

ALSO, I found in the documentation for UserControls, under "Explicit
Interface Implementations" (whatever that means),
"System.Web.UI.IUserControlDesignerAccessor.InnerText." That looks perfect,
but I have no idea how you call it.

Alex
 
L

Laurent Bugnion

Hi,

Alex said:
Hi. Well, my UserControl *does* have a Controls collection property, but I
really don't know where to go from there. I mean, the text inside the
opn/close tag of my control isn't really a control, right? It's just the body
text. How do I get to that?

Alex

All Controls have a Controls collection, it's inherited from the Control
class from which all Controls (User controls and Custom controls, as
well as built in controls) derive.

You must understand the hierarchical nature of HTML. It's like XML.
Everything is contained in something, from single text up to the
document. Even text is a control, in HTML it will be rendered in a text
node (which, when rendered, displays just text).

This hierarchical structure is reflected in ASP.NET, where Controls have
children and parents.

That said, I am confused. I tried to reproduce this from your original post:

<Ax:MyCtl id="Something" runat="server">
Some stuff inside the tag
</Ax:MyCtl>

Turns out this is illegal, and on my VS2005, it won't even run, stating
that placing content in the tag is not allowed.

OK, the easiest for you is to set a breakpoint in your page's
codebehind, and then to look what is in the Controls collection. Try to
navigate the hierarchy, until you find the text you want to access...

HTH,
Laurent
 
Ad

Advertisements

W

Walter Wang [MSFT]

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.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top