ASP.NET 自定义控件中,模板节仅允许在内部添加自定义类

本文关键字:在内部 添加 自定义 自定义控件 NET ASP | 更新日期: 2023-09-27 18:33:58

我想扩展网格视图,添加一个自定义模板字段,我只能在其中输入 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; }
        }
    }

ASP.NET 自定义控件中,模板节仅允许在内部添加自定义类

我认为您通过实现 ITemplate 使事情过于复杂,不确定您到底要完成什么,但也许,与其创建一个允许您做所有事情的模板,不如创建一个您实际想要使用的东西列表......(白名单与黑名单我猜;).

尝试这样的事情:

[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> 
{ 
}

哼!

博瓦科

你的一个活救星,完全有效