public partial class Controls_ImageBanner : System.Web.UI.UserControl
{
[WebEditor("Telerik.Libraries.WebControls.ImageSelector, Telerik.Libraries")]
[Bindable(true)]
[DefaultValue("")]
[UrlProperty]
[TypeConverter(typeof(ContentItemUrlConverter))]
public string BackgroundImageUrl
{
get;
set;
}
private class ContentItemUrlConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string o = value as string;
return o ?? base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
string o = value as string;
if (o != null && ((destinationType == typeof(string))))
{
Match match = Regex.Match(o, @"\[(\w+)\](\w{8}-\w{4}-\w{4}-\w{4}-\w{12})");
if (match.Success)
{
string provider = match.Groups[1].Value;
string id = match.Groups[2].Value;
if (ContentManager.Providers.ContainsKey(provider))
{
IContent cnt = ContentManager.Providers[provider].GetContent(new Guid(id));
if (cnt != null)
{
return cnt.UrlWithExtension;
}
}
}
else if (Regex.IsMatch(o, "^[a-zA-Z]:/"))
{
var appPath = HttpContext.Current.Server.MapPath("~");
return "~/" + o.Replace(appPath.Replace("\\", "/"), string.Empty);
}
return o;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}