我已经建立了一个用户控件,它有一个公共属性string[] foo
。
在实际的标记中分配这个属性是可能的吗?
项目类型是ASP.NET WebForms / Web应用程序。
我尝试了以下语法,但它并不起作用:(它编译了,但值从未被分配)。
<abc:MyControl runat="server" foo='<%# new string[]{"Hello"} %>' />
我已经建立了一个用户控件,它有一个公共属性string[] foo
。
在实际的标记中分配这个属性是可能的吗?
项目类型是ASP.NET WebForms / Web应用程序。
我尝试了以下语法,但它并不起作用:(它编译了,但值从未被分配)。
<abc:MyControl runat="server" foo='<%# new string[]{"Hello"} %>' />
PersistenceMode(PersistenceMode.InnerProperty)
感兴趣。 PersistenceMode
属性允许你创建带有嵌套属性的属性。需要注意的是,在这种情况下,你的 aspx 代码将使用嵌套元素而不是属性,因为元素只能存储字符串。<abc:MyControl runat="server" >
<foo>
<element value="1" />
<element value="2" />
</foo>
</abc:MyControl>
<abc:MyControl runat="server" ID="control1" foo='<%# new string[]{"Hello"} %>' />
是...的简写
<abc:MyControl runat="server" ID="control1" OnDatabinding="Databind_control1" />
protected void Databind_control1(object sender, EventArgs e)
{
((MyControl) sender).foo = new string[]{"Hello"};
}
尝试这种方式,看看是否有效。
我以前从未见过这样的东西,通常对于数组,我会做类似于这样的事情:
<abc:MyControl runat="server" foo="This,Is,A,Test" />
public string foo {
get;
set;
}
public string[] foos {
get { return foo.Split(','); }
//set; if needed
}