Truncate a databound string at runtime
December 30, 2009 by: B.HardingSo here is the scenario,
I want to display the first 25 characters of a text field in a grid.
You can do this a few ways such as, truncate when the datasource selects it, alter it on rowdatabound and probably a few others. But I wanted to try to find a way using the a format string.
<asp:Label ID="amanotesLabel"
runat="server"
Text='<%#String.Format("{0}",Eval("notes")).Substring(0,25)%>'
it throws an error when the string is less than 25 characters which can be corrected like so….
There are 25 spaces after the {0}
<asp:Label ID="amanotesLabel"
runat="server"
Text='<%#String.Format("{0} ",Eval("notes")).Substring(0,25)%>'
So there you have it, no events needed.


