Own aspNet control - tag contents as default property

R

Rolf Welskes

Hello,
I have writen a simple aspnet control MyCtrl and want to use it as follows:
(c01 may be the tag-prefix):
<c01:MyCtrl>this is a simple text</c01:MyCtrl>.

The control-code:

[DefaultProperty("Text02")]
[ToolboxData("<{0}:MyCtrl runat=server></{0}:MyCtrl>")]
[PersistChildren(true)]
[ParseChildren(false)]
.... MyCtrl .... inhereted from Control (not from WebControl)
..................
//the property which should be used for the innertext.
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public string Text02
{
get
{
String s = (String)ViewState["Text02"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Text02"] = value;
}
}
...............

the web-page:

..................
<c01:MyCtrl ID="myId" runat="server">this is a simple text</c01:MyCtrl>
...........

Now if there is nothing between the tags means I have:
<c01:MyCtrl ID="myId" runat="server"></c01:MyCtrl>
and I go from html-view to design-view, no problem.
Now I can enter in the propertyView under the property Text02: this is a
simple text.

If I go now to the html-view I see:
<c01:MyCtrl ID="myCtrl" runat="server"></c01:MyCtrl>
means the text is not there
If I now write a text so I have
<c01:MyCtrl ID="myCtrl" runat="server">this is a simple text</c01:MyCtrl>
and if I now go back to the design View I do not see the text under the
property Text02

Seems the designer cannot write/read 'this is a simple text' within the
tags.

What is going wrong.
Any help would be nice.
Thank you.
Rolf Welskes
 
S

Steven Cheng[MSFT]

Hello Rolf,

As for the custom webcontrol, based on the code you provided, I found there
are two things we may take care here:

1) If you want to persist Control's properties inside Control's tag(inner
property), you need to set the "ParseChildrenAttribute" as "true", this
indicate that the inner content of the control tag will be parsed as
control properties.

#ParseChildrenAttribute Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.parsechildrenattribut
e.aspx


2) The "PersistChildrenAttributeA" is used together with the
"parseChildrenAttribute", from the MSDN document remark below:

===================
The PersistChildrenAttribute is used in combination with the
ParseChildrenAttribute to determine how nested content of a control is
interpreted. If PersistChildrenAttribute is true and ParseChildrenAttribute
is false, the nested content contained within an ASP.NET server control is
persisted as controls.If PersistChildrenAttribute is false and
ParseChildrenAttribute is true, the nested content is persisted as
properties of the server control.
=============

You can find that if you want to persist property into control's inner
content, you should set "PersistChildAttribute" to false and
"ParseChildrenAttribute" to true. e.g.

[ParseChildren(true,"Description")]
[PersistChildren(false)]


Here is a complete test control for your reference:

==================================
[DefaultProperty("Description")]
[ToolboxData("<{0}:propertyControl
runat=server></{0}:propertyControl>")]
[ParseChildren(true,"Description")]
public class PropertyControl : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Text"] = value;
}
}



[Bindable(true)]
[Category("Appearance")]
[DefaultValue("default description")]
[Localizable(true)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public string Description
{
get
{
String s = (String)ViewState["Description"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Description"] = value;
}
}



protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<br/><hr/>");
output.Write("<br/>Text: " + Text);
output.Write("<br/>Description: " + Description);
output.Write("<br/><hr/>");
}
}

=================================

Hope this helps.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
thank you for your help, it works.

Thank you again.
Rolf Welskes


Steven Cheng said:
Hello Rolf,

As for the custom webcontrol, based on the code you provided, I found
there
are two things we may take care here:

1) If you want to persist Control's properties inside Control's tag(inner
property), you need to set the "ParseChildrenAttribute" as "true", this
indicate that the inner content of the control tag will be parsed as
control properties.

#ParseChildrenAttribute Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.parsechildrenattribut
e.aspx


2) The "PersistChildrenAttributeA" is used together with the
"parseChildrenAttribute", from the MSDN document remark below:

===================
The PersistChildrenAttribute is used in combination with the
ParseChildrenAttribute to determine how nested content of a control is
interpreted. If PersistChildrenAttribute is true and
ParseChildrenAttribute
is false, the nested content contained within an ASP.NET server control is
persisted as controls.If PersistChildrenAttribute is false and
ParseChildrenAttribute is true, the nested content is persisted as
properties of the server control.
=============

You can find that if you want to persist property into control's inner
content, you should set "PersistChildAttribute" to false and
"ParseChildrenAttribute" to true. e.g.

[ParseChildren(true,"Description")]
[PersistChildren(false)]


Here is a complete test control for your reference:

==================================
[DefaultProperty("Description")]
[ToolboxData("<{0}:propertyControl
runat=server></{0}:propertyControl>")]
[ParseChildren(true,"Description")]
public class PropertyControl : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Text"] = value;
}
}



[Bindable(true)]
[Category("Appearance")]
[DefaultValue("default description")]
[Localizable(true)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public string Description
{
get
{
String s = (String)ViewState["Description"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Description"] = value;
}
}



protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<br/><hr/>");
output.Write("<br/>Text: " + Text);
output.Write("<br/>Description: " + Description);
output.Write("<br/><hr/>");
}
}

=================================

Hope this helps.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no
rights.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top