I need a GridView column that is always a hyperlink when a URL is provided.
I was not able to find one looking through the available controls and/or the documentation.
I created one that inherits from DotVVM.BusinessPack.Controls.GridViewTextColumn
, which I’ll include at the end.
Is there an existing GridView column that is a hyperlink and not just plain text?
If there is not, hopefully this helps others out with a similar need.
public class GridViewLinkColumn : DotVVM.BusinessPack.Controls.GridViewTextColumn
{
/// <summary>
/// Gets or sets the value of the HREF property on the anchor tag.
/// </summary>
public string Url
{
get => (string)GetValue(UrlProperty);
set => SetValue(UrlProperty, value);
}
public static readonly DotvvmProperty UrlProperty = DotvvmProperty.Register<string, GridViewLinkColumn>(c => c.Url, null);
public GridViewLinkColumn(BindingCompilationService bindingService) : base(bindingService) {}
public override void BuildCell(HtmlGenericControl cell, GridViewColumn column)
{
if (UrlProperty.IsSet(column,false))
{
var span = new HtmlGenericControl("span");
span.AddCssClass(BusinessPackCss.PrimitiveText);
var href = new LinkButton();
href.Attributes.Add("title", GetBinding(GridViewValueColumn<string, object>.ValueProperty));
href.Attributes.Add("href", GetBinding(GridViewLinkColumn.UrlProperty));
href.SetProperty((LinkButton c) => c.Text,
GetBinding(GridViewValueColumn<string, object>.ValueProperty));
span.Children.Add(href);
cell.Children.Add(span);
}
// If no value is provided for the URL, then use the default GridViewTextColumn contents
else
{
var control = new Literal();
control.AddCssClass<Literal>(BusinessPackCss.PrimitiveText);
control.Attributes.Add("title", GetBinding(GridViewValueColumn<string, object>.ValueProperty));
control.FormatString = this.FormatString;
control.SetProperty<Literal, string>((Expression<Func<Literal, string>>)(c => c.Text), this.GetBinding(GridViewValueColumn<string, object>.ValueProperty));
cell.Children.Add((DotvvmControl)control);
}
}
}