ASP.NET Custom control, templatesection only allow custom class to be added inside

112 views Asked by At

I want to extend gridview, add a custom templatefield in which i can enter ONLY 1 item type namely (multiple) <Filter> items.

so for instance i want:

<FvmControls:FilteredGridView ID="fgvTest" runat="server">
    <Filters>
        <Filter labelText="ID" typeName="Int32" saveCookie="false" DBfield="Id" />
        <Filter labelText="Name" typeName="String" saveCookie="false" DBfield="Name" />
        <Filter labelText="Email" typeName="String" saveCookie="false" DBfield="Email" />
        <Filter labelText="Mobile Private" typeName="Boolean" saveCookie="false" DBfield="MobileIsPrivate" />
    </Filters>
    <Columns>
        ......
    </Columns>
</FvmControls:FilteredGridView>

Right now i can enter anything i want in my template field, i want to restrict it to only the classes (on in this case single class) of my choosing. How can i do this? Any good articles on this? Below is the code i tried:

[ParseChildren(true), PersistChildren(true)]
    [ToolboxData("<{0}:FilteredGridView runat=\"server\"></{0}:FilteredGridView>")]
    public class FilteredGridView : GridView
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(FilterCollection))]
        public FilterCollection Filters { get; set; }
    }

    public class FilterCollection : ITemplate
    {
        private List<Filter> _paramList = new List<Filter>();

        #region innerProperties
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Filter filter
        {
            get { return filter; } // want to make private but then visual studio starts giving errors on the <param .... /> in front end
            set { _paramList.Add(value); }
        }
        #endregion

        public void InstantiateIn(Control container)
        {
        }
    }

    public class Filter
    {
        private Type _type;
        private String _typeName;
        private String _labelText = "";
        private Boolean _saveCookie = false;
        private Object _value;
        private String _dbfield;

        public String labelText
        {
            get { return _labelText; }
            set { _labelText = value; }
        }
        public String typeName
        {
            private get { return _typeName; }
            set
            {
                String FullValue = value;
                if (!value.Contains("System.")) FullValue = value.Insert(0, "System.");
                this.type = Type.GetType(FullValue, true, true);
                _typeName = value;
            }
        }
        public Type type
        {
            get { return _type; }
            private set { _type = value; }
        }
        public Boolean saveCookie
        {
            get { return _saveCookie; }
            set { _saveCookie = value; }
        }
        public Object value
        {
            get { return _value; }
            set { _value = value; }
        }
        public String DBfield
        {
            get { return _dbfield; }
            set { _dbfield = value; }
        }
    }
2

There are 2 answers

0
Thijs Dalhuijsen On BEST ANSWER

I think you are overcomplicating things by implementing ITemplate, not sure what exactly you are trying to accomplish, but maybe, instead of creating a template that allows you to do everything, you can create a List of stuff you actually want to use instead... (whitelist vs blacklist i guess ;) ..

Try something like this:

[ParseChildren(true), PersistChildren(true), DefaultProperty("Filters")] 
public class FilteredGridView : GridView 
{ 
    private FilterList _filters;  
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
    [PersistenceMode(PersistenceMode.InnerProperty)] 
    [TemplateContainer(typeof(FilteredGridView))] 
    public FilterList Filters { 
        get { return _filters; } 
        set { _filters = value; } 
    } 
} 

public class FilterList : List<Filter> 
{ 
}

HTH!

bovako

0
Thierry Verhaegen On

Your a live saver, that totally worked