在尝试创建应该在ASP.NET中使用的自定义数据源时,我创建了自定义数据源类,自定义编辑器和自定义可序列化类。
我无法理解为什么它不起作用......即使我可能有更多的属性而不是必需的(我已经浏览并尝试了几个小时),根据我的理解{ {1}}应该已经完成了这个诀窍......而且,在我看来,我的代码与Why can't I declare sub-elements (properties) of a UserControl in a WebForm?类似。
代码的工作原理如下:
PersistenceMode(PersistenceMode.InnerProperty)
自定义编辑器似乎有效:使用后,VS中的属性会正确更新。
但是,在更新某些内容后,信息不会保留在ASPX文件中:
[ParseChildren(true)]
[PersistChildren(true)]
public class MyDataSource : DataSourceControl
{
// [much more irrelevant code...]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[MergableProperty(false)]
[TypeConverter(typeof(ExpandableObjectConverter))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(Editors.ResultRequestEditor), typeof(System.Drawing.Design.UITypeEditor))]
public ResultRequest Request { get; set; }
}
[Serializable]
[PersistChildren(true)]
[TypeConverter(typeof(ExpandableObjectConverter))]
[ParseChildren(true)]
public class ResultRequest
{
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public string ColumnName { get; set; }
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public Type ColumnType { get; set; }
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Always)]
public object[] ResultTypeParameters { get; set; }
}
我所期望的是数据源中的一些序列化,例如:
<cc1:MyDataSource ID="SearchDataSource1" runat="server" ProviderID="MyProvider1" />
有人可以解释为什么这不起作用吗?
答案 0 :(得分:1)
请看一下我的样本,它不那么复杂,因此更容易理解:
第一个父控件(在您的情况下,这将是DataSource):
[ToolboxData("<{0}:TabContainer runat=server></{0}:TabContainer>")]
[ParseChildren(ChildrenAsProperties = false)]
[PersistChildren(true)]
public class TabContainer : Panel, INamingContainer
{
#region private properties
List<TabItem> tabs = new List<TabItem>();
#endregion
[Bindable(true)]
public event EventHandler TabClick;
[Browsable(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public List<TabItem> Tabs
{
get { return this.tabs; }
}
}
注意我已将ChildrenAsProperties设置为false。这里是子定义,TabItem:
public class TabItem : Panel, IPostBackEventHandler
{
String clientClick = String.Empty;
public event EventHandler Click;
[Bindable(true)]
[Category("Appearance")]
public string Text
{
get
{
if (ViewState["Text"] == null)
{
ViewState["Text"] = "";
}
return (string)ViewState["Text"];
}
set
{
ViewState["Text"] = value;
}
}
}
现在在设计师中我可以像这样声明TabContainer:
<cc1:TabContainer ID="TabContainer1" runat="server"
</cc1:TabContainer>
最后添加保存状态的子控件:
<cc1:TabContainer ID="TabContainer1" runat="server"
OnTabClick="TabContainer_TabClick">
<Tabs>
<cc1:TabItem ID="Tab1" Text="Hello" runat="server" />
<cc1:TabItem ID="Tab2" Text="World" runat="server" />
</Tabs>
</cc1:TabContainer>
祝你好运!