我有一个用户控件,其属性类型为List<Something>
:
Private p_myList As New List(Of Guid)
Public Property MyList() As List(Of Guid)
Get
Return p_myList
End Get
Set(ByVal value As List(Of Guid))
If value Is Nothing Then Throw New ArgumentNullException()
p_myList = value
End Set
End Property
使用UserControl在页面的aspx源中设置此属性是否可行,例如:
<uc1:myUserControl runat="server" MyList="3c7d794e-7645-46e7-bdde-a0bc42679261, 3c7d794e-7645-46e7-bdde-a0bc42679262" />
或者我是否需要创建string类型的“兼容属性”然后进行解析? (我知道我可以通过代码隐藏设置这些值,但我更喜欢在aspx源代码中设置它们。)
答案 0 :(得分:3)
您需要创建TypeConvertor
像
这样的东西public class GuidListTypeConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(Guid);
}
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
string val = value as string;
string[] vals = val.Split(',');
System.Collections.Generic.List<Guid> ret = new System.Collections.Generic.List<Guid>();
foreach (string s in vals)
{
ret.Add(Guid.Parse(s));
}
return ret;
}
}
然后
[TypeConverter(typeof(GuidListTypeConverter))]
public List<Guid> MyList {get;set;}
答案 1 :(得分:0)
QUICK & DIRTY TRICK : make the property public and use the code
((MyPageClassName)this.Page).PropertyName
通过这种方式,您可以访问从UserControl到Parent Page和Vice Versa的控件。要使控件可用,请在各自的页面designer.cs文件中公开它们。
****<<Update>>****
感谢您纠正我。 AFAICU,如果用户控制在aspx页面中,则要设置属性。就像我们设置gridview的DataKeyNames一样,你可以放置多个关键字段。所以他们是一个问题,你怎么知道你在列表中放置的GUID始终是唯一的。或者此GUID是固定的,来自数据库。你会如何保持一致性?
我建议,对于Codebehind这样的游戏,你可以从aspx开始拥有更多的控制权。