Re: WebControl with Collection Property in Design Time



Hey Mirek,

Thanks for getting back to me.
I've just add your code to my VS.NET 2005 IDE and seems it report error
when I just drag this control onto a form. Here the two things I found in
the code you pasted:


1. I think we should put some checking code to ensure that the collection
is not empty. like:
if (_Hyperlinks != null && _Hyperlinks.Count > 0)
{ ....}else{...}


2. Why do you put the following Attribute on the HyperLinks property?

[Editor(typeof(List<HyperLink>), typeof(UITypeEditor))]

Since HyperLink is buildin control types, the IDE can correctly use the
property edit for it. Also, even if you use it, you should assign a valid
Control Editor's typename rather than

typeof(List<HyperLink> (this is a Class type , not Editor type....)


After adjusting the above two things, here is the final code I got working
correctly on myside. I can drag the control from ToolBox onto webform.
Then, Edit the "HyperLinks" collection through property Grid( a Collection
Editor) , and I can add /remove HyperLinks in the Collection and apply
styles to each of the HyperLinks in the Collection. In addition, when I
switch between DesignView and Html View, I can see the HyperLinnks has been
persisted in HTML correctly. e.g:

=================
<cc1:WebNavigation ID="WebNavigation1" runat="server"
Hyperlinks-Capacity="4">
<asp:HyperLink runat="server" BackColor="Blue"
BorderColor="#FFFF80" BorderStyle="Solid"
BorderWidth="1px" ForeColor="Fuchsia"
NavigateUrl="sdfsfdsfdsfdsfdsfsdfd"
>HyperLink1</asp:HyperLink>
<asp:HyperLink runat="server" NavigateUrl="sdfdsfdsfdsfsd"
BackColor="Black" ForeColor="White" BorderStyle="Solid"
BorderWidth="1px"
BorderColor="Yellow">HyperLink2</asp:HyperLink>
<asp:HyperLink runat="server"
NavigateUrl="fdsfdsfdf">HyperLink3</asp:HyperLink>
<asp:HyperLink runat="server" NavigateUrl="fdsfsdfsdfsf"
BackColor="#FFFFC0" BorderColor="Purple" BorderStyle="Dashed"
BorderWidth="2px" ForeColor="Navy">HyperLink4</asp:HyperLink>
</cc1:WebNavigation>
====================


Here is the modified code, you can have a try on your side. If you still
get any problem on working on it at runtime or design-time, I think we have
to do some further throubleshooting on environment specific issue. BTW, I'm
using VS.NET 2005 Professional edition RTM.


===============================================
namespace CustomControl
{
public class WebNavigationDesigner : ControlDesigner
{
public override string GetDesignTimeHtml()
{
WebNavigation wNavigation = (WebNavigation)Component;

if (wNavigation.Hyperlinks != null &&
wNavigation.Hyperlinks.Count > 0)
{
StringBuilder sb = new StringBuilder();
StringWriter sWriter = new StringWriter(sb);
HtmlTextWriter htWriter = new HtmlTextWriter(sWriter);
foreach (HyperLink hyperlink in wNavigation.Hyperlinks)
hyperlink.RenderControl(htWriter);

return sWriter.ToString();
}
else
{
return "<font size='30'>Empty Collection.</font>";
}
}
}

[DefaultProperty("Hyperlinks")]
[ToolboxData("<{0}:WebNavigation runat=server></{0}:WebNavigation>")]
[Designer("WebNavigationDesigner", "WebNavigation")]
[ParseChildren(true, "Hyperlinks")]
public class WebNavigation : WebControl
{
private List<HyperLink> _Hyperlinks = null;

protected override void RenderContents(HtmlTextWriter output)
{
if (_Hyperlinks != null && _Hyperlinks.Count > 0)
{
foreach (HyperLink hLink in _Hyperlinks)
hLink.RenderControl(output);
}
else
{
Label lbl = new Label();
lbl.ID = "lblMessage";
lbl.Text = "Empty Collection.";
lbl.RenderControl(output);
}
}
[Bindable(true)]
[Category("Data")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public List<HyperLink> Hyperlinks
{
get
{
if (_Hyperlinks == null)
_Hyperlinks = new List<HyperLink>();

return _Hyperlinks;
}
}
}
}
========================================================

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Mirek Endys" <MirekE@xxxxxxxxxxxxxxxx>
| References: <#1WrfQ63FHA.2872@xxxxxxxxxxxxxxxxxxxx>
<lYD#xPE4FHA.2904@xxxxxxxxxxxxxxxxxxxxx>
<l28wCl64FHA.1172@xxxxxxxxxxxxxxxxxxxxx>
| Subject: Re: WebControl with Collection Property in Design Time
| Date: Fri, 11 Nov 2005 09:33:52 +0100
| Lines: 411
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <OnhLqqp5FHA.3416@xxxxxxxxxxxxxxxxxxxx>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: gw.coty.cz 195.47.52.129
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:31079
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| No, it is not working. I will try to explain it again.
| What I need
| ------------------------------------------------------
| I need to create my own Web Server Control.
| This control will contain a count of HyperLink controls.
| I need this control has design-time support:
| - I will add hyperlinks by design-time property toolbox.
| - This properties (including hyperlinks) will be stored and will be
| editable (add/remove)
| whole design time by property toolbox.
| - This control and its style will be displayed in design-time on the web
| page
|
|
| My control was able to edit hyperlinks, by property toolbox (collection
| editor)
| in designtime, but I can only add the HyperLinks but the second time
| I open the property collection editor, the hyperlinks are gone.
| Thats because they arent stored.
|
| I think, that the control must working by this way:
| - in case HyperLinks are edited (added, removed) they must be
| parsed from/to the page (or into the control) in design time.
| I was able (but not now) to parse them to page in design time, but not
FROM
| page
| into property toolbox (collection editor)
|
| Well, and now, what I did
| --------------------------------------
| public class WebNavigationDesigner : ControlDesigner
| {
| public override string GetDesignTimeHtml()
| {
| WebNavigation wNavigation = (WebNavigation)Component;
| StringWriter sWriter = new StringWriter();
| HtmlTextWriter htWriter = new HtmlTextWriter(sWriter);
| foreach(HyperLink hyperlink in wNavigation.Hyperlinks)
| hyperlink.RenderControl(htWriter);
|
| return sWriter.ToString();
| }
| }
|
| [DefaultProperty("Hyperlinks")]
| [ToolboxData("<{0}:WebNavigation runat=server></{0}:WebNavigation>")]
| [Designer("WebNavigationDesigner", "WebNavigation")]
| [ParseChildren(true, "Hyperlinks")]
| public class WebNavigation : WebControl
| {
| private List<HyperLink> _Hyperlinks = null;
|
| protected override void RenderContents(HtmlTextWriter output)
| {
| foreach(HyperLink hLink in _Hyperlinks)
| hLink.RenderControl(output);
| }
| [Bindable(true)]
| [Category("Data")]
| [Editor(typeof(List<HyperLink>), typeof(UITypeEditor))]
|
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
| [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
| public List<HyperLink> Hyperlinks
| {
| get
| {
| if(_Hyperlinks == null)
| _Hyperlinks = new List<HyperLink>();
| return _Hyperlinks;
| }
| }
| }
|
| ==============================================================
| Thats simplified problem, please help me to get this simple
| control to life.
|
| Thanks
|
| Mirek
| --------------------------------------------------------------
|
| "Steven Cheng[MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
| news:l28wCl64FHA.1172@xxxxxxxxxxxxxxxxxxxxxxxx
| > Hi Mirek,
| >
| > How are you doing on this issue. Does the suggestions in my last reply
| > helps you resolve the problem? if there're anything else we can help,
| > please feel free to post here. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| > --------------------
| > | X-Tomcat-ID: 120657561
| > | References: <#1WrfQ63FHA.2872@xxxxxxxxxxxxxxxxxxxx>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain
| > | Content-Transfer-Encoding: 7bit
| > | From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
| > | Organization: Microsoft
| > | Date: Thu, 03 Nov 2005 06:57:47 GMT
| > | Subject: RE: WebControl with Collection Property in Design Time
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | Message-ID: <lYD#xPE4FHA.2904@xxxxxxxxxxxxxxxxxxxxx>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | Lines: 133
| > | Path: TK2MSFTNGXA01.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:11644
| > | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| > |
| > | Hi Mirek,
| > |
| > | Welcome to ASPNET newsgroup.
| > | From you description, you're developing a custom web server control
(in
| > | asp.net 2.0) which contains a collection property and you found that
the
| > | control always throw out "could not be set on property" error when
| > | swtiching between HTML view and design-view in VS.NET IDE(2005) , YES?
| > |
| > | Based on my experience, this problem is caused by our Collection
| > property's
| > | declaration. For Collection property in .NET Custom Control, we can
only
| > | provide getter accessor for it, in other world it should be defined as
| > | readonly. Like:
| > | ===========
| > | public List<HyperLink> Hyperlinks
| > | {
| > |
| > | get
| > | {
| > |
| > | if ( _Hyperlinks == null )
| > |
| > | _Hyperlinks = new List<HyperLink> ();
| > |
| > | return _Hyperlinks;
| > |
| > | }
| > |
| > | //no setter needed
| > |
| > | }
| > | ==============
| > |
| > | For Collection property, we just need to add/remove items from it, do
| > not
| > | need to modify the Collection reference itself. Also, we can find that
| > | those collection property in ASP.NET buildin controls are just
defined
| > as
| > | readonly(Getter only...). So you can remove the setter accessor in
your
| > | control's Hyperlinks property.
| > |
| > | Hope helps. Thanks,
| > |
| > | Steven Cheng
| > | Microsoft Online Support
| > |
| > | Get Secure! www.microsoft.com/security
| > | (This posting is provided "AS IS", with no warranties, and confers no
| > | rights.)
| > |
| > |
| > |
| > |
| > |
| > |
| > |
| > | --------------------
| > | | From: "Mirek Endys" <MirekE@xxxxxxxxxxxxxxxx>
| > | | Subject: WebControl with Collection Property in Design Time
| > | | Date: Wed, 2 Nov 2005 12:54:02 +0100
| > | | Lines: 209
| > | | X-Priority: 3
| > | | X-MSMail-Priority: Normal
| > | | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | | X-RFC2646: Format=Flowed; Original
| > | | Message-ID: <#1WrfQ63FHA.2872@xxxxxxxxxxxxxxxxxxxx>
| > | | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | | NNTP-Posting-Host: gw.coty.cz 195.47.52.129
| > | | Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
| > | | Xref: TK2MSFTNGXA01.phx.gbl
| > | microsoft.public.dotnet.framework.aspnet.webcontrols:11628
| > | | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | |
| > | | Hallo,
| > | |
| > | | I need to create WebControl, that stores List of Hyperlinks.
| > | | I have problem in design-time.
| > | | When I add an hyperlinks, they are rendered in contents, hyperlinks
| > are
| > | | vsible on page.
| > | | In case I switch into source and then back into design, the control
| > says:
| > | |
| > | | Error Creating Control - WebNavigation1
| > | | 'cc1:WebNavigation' could not be set on property 'Hyperlinks'.
| > | |
| > | | Im wrong, but I dont know where. Could you help me? Source is
bellow.
| > | |
| > | | using System;
| > | |
| > | | using System.IO;
| > | |
| > | | using System.Collections.Generic;
| > | |
| > | | using System.Collections;
| > | |
| > | | using System.Drawing.Design;
| > | |
| > | | using System.ComponentModel;
| > | |
| > | | using System.Text;
| > | |
| > | | using System.Web;
| > | |
| > | | using System.Web.UI;
| > | |
| > | | using System.Web.UI.Design;
| > | |
| > | | using System.Web.UI.WebControls;
| > | |
| > | | namespace WebNavigation
| > | |
| > | | {
| > | |
| > | | public class WebNavigationDesigner :
| > System.Web.UI.Design.ControlDesigner
| > | |
| > | | {
| > | |
| > | | public override string GetDesignTimeHtml ()
| > | |
| > | | {
| > | |
| > | | WebNavigation wn = (WebNavigation) Component;
| > | |
| > | | StringWriter sw = new StringWriter ();
| > | |
| > | | HtmlTextWriter tw = new HtmlTextWriter ( sw );
| > | |
| > | |
| > | | foreach ( HyperLink hl in wn.Hyperlinks )
| > | |
| > | | {
| > | |
| > | | hl.RenderControl ( tw );
| > | |
| > | | }
| > | |
| > | | return sw.ToString ();
| > | |
| > | |
| > | | }
| > | |
| > | | }
| > | |
| > | | //[DefaultProperty ( "Hyperlinks" )]
| > | |
| > | | [ToolboxData ( "<{0}:WebNavigation
| > runat=server></{0}:WebNavigation>" )]
| > | |
| > | | [Designer ( "WebNavigation.WebNavigationDesigner, WebNavigation" )]
| > | |
| > | | [ParseChildren(true, "Hyperlinks")]
| > | |
| > | | public class WebNavigation : WebControl
| > | |
| > | | {
| > | |
| > | |
| > | | [DefaultValue(100)]
| > | |
| > | | [Browsable(true)]
| > | |
| > | | [Bindable ( true )]
| > | |
| > | | public override Unit Width
| > | |
| > | | {
| > | |
| > | | get
| > | |
| > | | {
| > | |
| > | | return base.Width;
| > | |
| > | | }
| > | |
| > | | set
| > | |
| > | | {
| > | |
| > | | base.Width = value;
| > | |
| > | | }
| > | |
| > | | }
| > | |
| > | | [Bindable ( true )]
| > | |
| > | | [Category ( "Appearance" )]
| > | |
| > | | [DefaultValue ( "" )]
| > | |
| > | | [Localizable ( true )]
| > | |
| > | | public string Caption
| > | |
| > | | {
| > | |
| > | | get
| > | |
| > | | {
| > | |
| > | | String s = (String) ViewState["Caption"];
| > | |
| > | | return ( ( s == null ) ? String.Empty : s );
| > | |
| > | | }
| > | |
| > | | set
| > | |
| > | | {
| > | |
| > | | ViewState["Caption"] = value;
| > | |
| > | | }
| > | |
| > | | }
| > | |
| > | |
| > | | protected override void RenderContents ( HtmlTextWriter writer )
| > | |
| > | | {
| > | |
| > | | foreach ( HyperLink hl in _Hyperlinks )
| > | |
| > | | this.Controls.Add ( hl );
| > | |
| > | | base.RenderContents ( writer);
| > | |
| > | | }
| > | |
| > | | private List<HyperLink> _Hyperlinks;
| > | |
| > | | //[Editor(typeof(List<HyperLink>), typeof(UITypeEditor))]
| > | |
| > | | [DesignerSerializationVisibility (
| > | DesignerSerializationVisibility.Content)]
| > | |
| > | | [PersistenceMode ( PersistenceMode.InnerDefaultProperty)]
| > | |
| > | | public List<HyperLink> Hyperlinks
| > | |
| > | | {
| > | |
| > | | get
| > | |
| > | | {
| > | |
| > | | if ( _Hyperlinks == null )
| > | |
| > | | _Hyperlinks = new List<HyperLink> ();
| > | |
| > | | return _Hyperlinks;
| > | |
| > | | }
| > | |
| > | | set
| > | |
| > | | {
| > | |
| > | | foreach(HyperLink hl in value)
| > | |
| > | | this.Controls.Add(hl);
| > | |
| > | | _Hyperlinks = value;
| > | |
| > | | }
| > | |
| > | | }
| > | |
| > | |
| > | | protected override void AddedControl ( Control control, int index )
| > | |
| > | | {
| > | |
| > | | base.AddedControl ( control, index );
| > | |
| > | | }
| > | |
| > | | }
| > | |
| > | | }
| > | |
| > | |
| > | |
| > | |
| > | |
| > |
| > |
| >
|
|
|

.



Relevant Pages

  • Re: WebControl with Collection Property in Design Time
    ... > when I just drag this control onto a form. ... Why do you put the following Attribute on the HyperLinks property? ... > get any problem on working on it at runtime or design-time, ... > public class WebNavigationDesigner: ControlDesigner ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: WebControl with Collection Property in Design Time
    ... Microsoft Online Support ... |> when I just drag this control onto a form. ... Why do you put the following Attribute on the HyperLinks property? ... |> | Subject: Re: WebControl with Collection Property in Design Time ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: WebControl with Collection Property in Design Time
    ... This control will contain a count of HyperLink controls. ... - I will add hyperlinks by design-time property toolbox. ... parsed from/to the page in design time. ... > | Subject: RE: WebControl with Collection Property in Design Time ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • RE: WebControl with Collection Property in Design Time
    ... you're developing a custom web server control (in ... For Collection property in .NET Custom Control, ... public ListHyperlinks ... | WebNavigation wn = Component; ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • English eBooks Engineering
    ... Adaptive Voltage Control in Power Systems Fusco, ... Advanced Design Techniques for RF Power Amplifiers Krizhanovski, ... Advanced Fuzzy Logic Technologies in Industrial Applications Bai, ... Bioinformatics Using Computational Intelligence Paradigms Jain, ...
    (sci.med.nutrition)