ASP.NET: custom control (or user control) with property of type List<Something>. Can the property be set in the ASPX source?
I have a user control with a property of type 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
Is it popssible to set this property in the aspx source of the page using the UserControl, e.g., something like:
<uc1:myUserControl runat="server" MyList="3c7d794e-7645-46e7-bdde-a0bc42679261, 3c7d794e-7645-46e7-bdde-a0bc42679262" />
or do I need to create a "compatibility property" of type string开发者_如何学Go which is then parsed? (I know that I could set these values through codebehind, but I'd prefer to do it in the aspx source.)
You need to create a TypeConvertor
Something like
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;
}
}
Then
[TypeConverter(typeof(GuidListTypeConverter))]
public List<Guid> MyList {get;set;}
QUICK & DIRTY TRICK : make the property public and use the code
((MyPageClassName)this.Page).PropertyName
In this way you can provide access to controls to and from UserControl to Parent Page and Vice Versa. To make controls available, make them public in their respective page designer.cs file.
****<<Update>>****
Thanks for correcting me. AFAICU, you want to set the property if user control within the aspx page. Like we set the DataKeyNames of the gridview, where you can put multiple key fields. So their is a question how do you know the GUID you are placing in the list is unique always. Or this GUID is fixed and is coming from database. How would you maintain the consistency?
I would suggest , for such things play from Codebehind, you can have more control as of aspx.
精彩评论