Databinder.Eval and Substring

Im using a repeater and a data block to display data from the database to my site. Example: DataBinder.Eval (Container, "DataItem.title")

Sometimes the text is too long. Usually I use a substring to display the preferred string in length. But how to do it with a data block? And if the text is too long (> 20 characters), I want to truncate it and leave three dots behind. How to do it?

+3
source share
2 answers

I would suggest an extension method for heavy lifting to make markup as simple as possible:

public static string EvalTrimmed(this RepeaterItem container, string expression, int maxLength)
{ 
    string value = DataBinder.Eval(container, expression) as string;
    if ( value != null ) 
       return null;
    if (value.Length > maxLength)
       value = value.Substring(0,maxLength) + "...";
    return value;
}

Then use it in markup like:

<%# Container.EvalTrimmed("DataItem.Title", 20) %>
+2
source

ASP.NET , , Repeater.ItemDataBound. , , , , , , .

, DataBinder.Eval, .

0

Source: https://habr.com/ru/post/1756318/


All Articles