Server Control Collection Properties Solutions,Problems MVP Advice Requested

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Server Control Collection Properties Solutions,Problems MVP Advice Requested

    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.
    [url]http://users.adelphia.net/~brianpclab/ServerControlCollectionIssues.htm[/url]
    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 [url]http://www.artima.com/forums/flat.jsp?forum=152&thread=38250[/url] and
    [url]http://www.devx.com/DevX/Article/20920/0/page/1[/url] 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?





    Brian Guest

  2. Similar Questions and Discussions

    1. Collection Property of Another Custom Server Control
      Hi, I read more and figured out how to build what I needed: <cc> <item></item> <item></item> </cc> when <item> is an editable control in...
    2. server control with collection
      Hello, I wrote a simple server control from a sample I found on the web in the following URL: http://west-wind.com/weblog/posts/200.aspx The...
    3. server control collection with several types of properties
      Is it possible to implement a server control that will look like this: <just:control> <columns> <columnTypeA id=1></columnTypeA> <columnTypeA...
    4. Server Control using a Collection
      Ok I am brand new at C#. I am attempting to build a server control that contains a collection. Trouble is when I place my control on my page and...
    5. Multiple Collection Properties in a Custom Control
      I am attempting to create control with 2 collection properties that are persisted with mode PersistenceMode.InnerProperty. When I create the...
  3. #2

    Default Re: Server Control Collection Properties Solutions,Problems MVP Advice Requested

    Brain,

    Your message too long and cut in the middle...

    Natty Gur[MVP]

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


    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur Guest

  4. #3

    Default Re: Server Control Collection Properties Solutions,Problems MVP Advice Requested

    Sorry about the length. It shows up fine in my news group reader.
    You can read the message and download the source code at the following URL
    [url]http://users.adelphia.net/~brianpclab/ServerControlCollectionIssues.htm[/url]
    Thanks

    "Natty Gur" <natty@dao2com.com> wrote in message
    news:eD6gEmKREHA.1312@TK2MSFTNGP12.phx.gbl...
    > Brain,
    >
    > Your message too long and cut in the middle...
    >
    > Natty Gur[MVP]
    >
    > blog : [url]http://weblogs.asp.net/ngur[/url]
    > Mobile: +972-(0)58-888377
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    BMukes Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139