ASP.NET自定义控件,templatesection只允许在里面添加自定义类

时间:2014-12-11 21:37:55

标签: c# asp.net custom-controls itemplate

我想扩展gridview,添加一个自定义模板字段,我只能在其中输入1个项目类型,即(多个)<Filter>个项目。

所以我想要:

<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>

现在我可以在我的模板字段中输入我想要的任何内容,我想将其限制为我选择的类(在本例中为单个类)。我怎样才能做到这一点?关于这个的任何好文章?以下是我试过的代码:

[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 个答案:

答案 0 :(得分:2)

我认为你通过实施ITemplate使事情过于复杂,不确定你究竟想要完成什么,但也许,你可以创建一个你真正想要使用的东西的列表,而不是创建一个允许你做所有事情的模板。相反...(白名单vs黑名单我猜;)..

尝试这样的事情:

[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

答案 1 :(得分:0)

你的现场救星,完全有效