Server Control Collection Properties Solutions,Problems MVP Advice Requested

B

Brian

NOTE ALSO POSTED IN
microsoft.public.dotnet.framework.aspnet.buildingcontrols

I have solved most of my Server Control Collection property issues.
I wrote an HTML page that describes all of the problems that I have
encountered to date and the solutions (if any) that I found.
http://users.adelphia.net/~brianpclab/ServerControlCollectionIssues.htm
This page also has all of the source code in a compressed file that you are
free to download and I have included links where I found solutions to most
of my problems.

I will paste the contents of the web page here. However, I did not want to
include the 57K zip of the source code in a News Group post.
After all of these problems I am wondering if I should even be writing
server controls using collection properties in this manner?
Do any of the Microsoft MVP's out there have any advice regarding this
matter?
Thanks

For the record this is what is included in the source code:

Two classes that have collection properties
These classes each have a Designer,ControlBuilder, and CollectionEditor for
the collection property
The ImageUrl property of the BaseButton control has a custom UITypeEditor.
The collection properties uses a custom type for the collection that will
allow the custom UITypeEditor to work and will stop the Ambiguous match
found error.

The ScheduleNavigation and ScheduleGlobal controls should fully work as
expected in design time.
The project is a C# Web Application so you will need to create a virtual
directory named TestServerControls



Server Control Collection Properties Issues and Advice



The purpose of this document is three fold. First, I want to document what
I've learned in the hopes that others will help me find solutions to the
problems that still remain. Second, I would like to prevent developers from
having to spend three weeks to get collection properties to work. Third, I
hope that this documentation will lead someone at Microsoft to document what
we SHOULD be doing in this area.

Source code is included here TestServerControls.zip



Background:

This was my first attempt at developing an ASP .NET web server control. I
have worked in C-Sharp on various projects for the past two years and after
spending two days reading up on the subject, I felt confident that I could
design a server control for use by the other developers in my company.



The Plan:

I wanted to develop a group of composite server controls that would give
other developers the ability to construct selected portions of a web page.
A developer would drop these controls on the design surface and use the
property grid to add and remove other pre-developed server controls. These
controls would automatically add any necessary supporting logic and keep the
look and feel consistent. With my background using C-Sharp in Windows
Forms, I felt I could get a prototype working within two days. That was
three weeks ago.

In short, I wanted the behavior of the Table Row Collection Editor (Drop a
table on the design surface and edit the Rows property) except I wanted the
developer to be able to choose from a variety of controls to add.

The Problems:

Problem 1:

My first problem still exists but only on my PC at work. It involves my
Visual Studio IDE. I could NOT get my collection editor to allow the
developer to select multiple items. In fact the collection editor was not
working at all. I found that this problem ONLY exists on my PC at work at
not on any of the other 5 PC's that I tested against. My best guess for
this problem comes from the following MSDN Library information on the
CollectionEditor.CreateInstance method

..NET Framework Security:

a.. Full trust for the immediate caller. This member cannot be used by
partially trusted code. For more information, see Using Libraries From
Partially Trusted Code
I'm guessing that I have some security issues with my PC that is preventing
the collection editor from working correctly. To get around this problem, I
am now coding under Virtual PC until I can either resolve the issue or get
the time to rebuild my PC. (What JOY)



Problem 2:

I am getting the following error message when I try and use my collection
property.

Ambiguous match found

Knowledge base article KB823194 provides the solution.

Problem 3:

The collection property appears to be working. However the control whose
properties I am attempting to edit will not allow me to edit the ImageUrl
property. My control inherits from System.Web.UI.WebControls.Image and it
is critical that I'm able to edit this property.

Partial Solution Part 1.

Articles at http://www.artima.com/forums/flat.jsp?forum=152&thread=38250 and
http://www.devx.com/DevX/Article/20920/0/page/1 followed by looking at the
ImageUrlEditor in reflector leads me to the following conclusion.

The editor is unable to get a service provider when my control is hosted
within my collection property. This is a major problem because other
controls will have a similar problem. The partial solution is to create a
UITypeEditor as indicated in the first article and to override the
UITypeEditor for the ImageUrl property in my derived class. This will work
only as long as the new UITypeEditor can obtain the Parent property in my
derived class. This leads us directly into Partial Solution Part 2.

Partial Solution Part 2.

The only way for my derived control's Parent property to be set is for it to
be in the Controls collection of my server control. Let me state this
differently. The only way for the modified UITypeEditor to work is if my
collection property also adds/removes items into the Controls collection of
the server control. So that is what my collection property now does. I was
hoping that the custom UITypeEditor would not be needed after I modified the
collection property but I was wrong.

Problem 4:

The HTML output from my collection property is malformed. There is no begin
tag just the inner content and an end tag. Experiments with various
attributes leads to the following settings that fix the problem.

On the server control class I have the following attributes set:

ParseChildren(true),

PersistChildren(false),

On the property collection I have the following attributes set:

PersistenceMode(PersistenceMode.InnerProperty),

DesignerSerializationVisibility(

DesignerSerializationVisibility.Visible),

NotifyParentProperty(true),



Problem 5:

I created a designer for my class but it was not working. This was a logic
problem based on assumptions that I had made by reading various books and
articles. I assumed that I was developing a composite control and that I
should have the designer force the server control to create its children.
That way I could let the base GetDesignTimeHtml method render the control
correctly. But to my surprise (I HAD NOT CREATED A COMPOSITE CONTROL). I
thought I had a composite control because:

1.. I was overriding the Controls property.
2.. I was creating all of my objects in CreateChildControls and adding
them to my Controls collection.
3.. I was letting the default rendering logic render the HTML.
But I had forgotten about problem #3.

Problem 3 forced me to use the Controls collection to hold every object that
was added into my collection property. Using the designer/rendering logic
in this fashion changes the state of the Controls collection, which changes
the state of the Parent property in all of the controls in the Controls
collection. In short, my designer needed to render the state of the server
control WITHOUT modifying the contents of the Controls collection.



In my humble opinion, a composite control meets the criteria stated in items
1,2, and 3 but it also has an important fourth criteria. It does not
negatively effect the state of its properties during rendering. Meeting
this criteria allows you to use the default rendering logic within the
designer and at run-time without side effects.

Questions and problems remaining to be solved:

1.. Problem 1 of course.
2.. When System.Web.UI.WebControl objects are created and added into the
collection property they do not have the same properties set as if they were
dropped on the design surface. I would like the ID property defaulted for
every control that gets added. However, in order to do this I have to
inherit from the control and override either the DefaultProperty
attribute,the ShouldSerialize, or the Reset methods. This works but is a
royal pain.
3.. Is there a better method of getting collection properties to work and
if so will the controls within the property still work correctly within the
property grid?(Problem 3)
4.. Am I attempting to use collection properties in a manner that is
outside the design paradigm?
 
N

Natty Gur

Hi,

great work. just want to add that if you want your collection persist as
sub control tags you should set PersistenceMode to InnerDefaultProperty.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
B

BMukes

My collection is correctly persisting its contents as sub control tags.
Its just that the HTML generated within the sub control tags is not the same
as what is generated if the control was dropped on the design surface.
And if memory serves me correctly, the HTML that is persisted is the same
regardless of the PersistenceMode attribute setting.

I also can't use the InnerDefaultProperty on the ScheduleGlobal class
because it has two collection properties.

I'm going out on a limb here for this next statement.
I think that to get the contents of the collection properties to persist
within the ASPX page correctly I am going to have to write a custom
DesignerSerializer. Is this guess correct or will implementing a custom
DesignSerializer only allow me to control the serialization of the code in
the code behind page?

Thanks
Brian



However, I don't
 

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