I need to display a multi-line text in a grid cell. How must I format it?
If I try to replace \n\r with <br />, the "<br />" test is displayed in the output.
2 Answers, 1 is accepted
0
Accepted
Marin Bratanov
Telerik team
answered on 14 May 2020, 10:53 AM
Hi Patrick,
Blazor escapes HTML by default - to prevent XSS issues. If you explicitly want to render HTML coming from the code/data, you must use a MarkupString in the cell template.
Here's a modified example from the docs:
<TelerikGrid Data="@MyData" Height="500px">
<GridColumns>
<GridColumn Field="@(nameof(SampleData.ID))" Title="Photo">
<Template>
@{
var employee = context as SampleData;
<img class="rounded" src="@($"/images/{employee.ID}.jpg")" alt="employee photo" />
}
</Template>
</GridColumn>
<GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name">
<Template>
Employee name is:
<br />
@(new MarkupString((context as SampleData).Name) )
</Template>
</GridColumn>
<GridColumn Field="HireDate" Title="Hire Date - Default string">
</GridColumn>
<GridColumn Field="HireDate" Title="Hire Date - Custom string">
<Template>
@((context as SampleData).HireDate.ToString("dd MMM yyyy"))
</Template>
</GridColumn>
</GridColumns>
</TelerikGrid>
@code {
publicclassSampleData
{
publicint ID { get; set; }
publicstring Name { get; set; }
public DateTime HireDate { get; set; }
}
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 50).Select(x => new SampleData
{
ID = x,
Name = "<strong>name</strong><br /> " + x,
HireDate = DateTime.Now.AddDays(-x)
});
}
Regards,
Marin Bratanov
Progress Telerik
Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic. Our thoughts here at Progress are with those affected by the outbreak.