Notice: This site is no longer being updated!

For more recent posts, please visit my blog.

ControlBuilderFaq
   > Home
   > Search
   > About

Links
   > MSDN
   > ASP.net Forums

Search

String Collections in Controls
by Eilon Lipton
1/31/2006 10:36:30 PM

Many controls have the need for a collection of strings that the page developer can configure. One way to do this is to simply have a property that can get and set a string array. With the proper metadata, ASP.net can persist this using a comma-separated list. However, this is somewhat inflexible. For example, what happens if the data itself has a comma in it?

The first thought might be to simply create a collection that contains individual string items. After all, ASP.net is great at persisting collections! The problem with this solution is that for ASP.net to process a collection item that item must have public properties containing the necessary values. In the case of strings, the only public property is Length, which is of course not very useful here.

The proper solution is to create a collection of custom items, each of which has its own string property.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;
 
namespace ControlBuilderFaq.Samples {
    [ParseChildren(true)]
    [PersistChildren(false)]
    public class ControlWithStrings : Control {
        private MyItemCollection _items;
 
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public MyItemCollection Items {
            get {
                if (_items == null) {
                    _items = new MyItemCollection();
                }
                return _items;
            }
        }
    }
 
    public class MyItem {
        private string _value;
 
        [DefaultValue("")]
        public string Value {
            get {
                if (_value == null) {
                    return String.Empty;
                }
                return _value;
            }
            set {
                _value = value;
            }
        }
    }
 
    public class MyItemCollection : List<MyItem> {
    }
}

Now we can persist the control in the rather familiar format:

<%@ Page Language="C#" %>
 
<%@ Register TagPrefix="cbf" Namespace="ControlBuilderFaq.Samples" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <cbf:ControlWithStrings runat="server" ID="cws1">
                <Items>
                    <cbf:MyItem Value="item 1" />
                    <cbf:MyItem Value="item 2" />
                    <cbf:MyItem Value="item 3" />
                    <cbf:MyItem Value="item 4" />
                </Items>
            </cbf:ControlWithStrings>
        </div>
    </form>
</body>
</html>

The same technique applies any time you want to have a collection of simple types. The trick is to create a container that will hold the actual value you want to have in your collection.

- Eilon




Comments
Trying to programmatically get items in a template
by Peter Kellner
(Posted on 8/13/2006 9:47:00 AM)
"Hi Eilon,
I'm pulling what's left of my hair out trying to figure out how to programmatically add a template to a custom control. That is, I want to be able to set the template to be something like:
<cc1:mycontrol id=...>
<template1>
<asp:label...
</

In other words, I'm trying to get the template1 stuff to happen automatically either based on an action from the designer, or when the custom control gets dropped on the design surface.

thanks, -Peter"


Post a comment:
Name:  
E-mail:
(Optional; Will not be shown)
Title:
 
Content:  

(Maximum length 1000 characters. Newlines will be preserved, and HTTP URLs will become links automatically.)
Post Comment