I wanted to chop the news_body data so that I could limit the maximum size of the text (with the intention to add a 'click for more' link where text IS too long)
I tried :
Eval(news_body).substring(1,150)
and also:
Eval(news_body).toString.substring(1,150)
But neither works. I'm struggling to grasp whats going on here.
I tried the above but i get the following error...:
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0117: 'object' does not contain a definition for 'Length'
Source Error:
Line 13:
Line 14:
Line 15: news_body").ToString().Substring(0,Math.Min(150,Eval("news_body").Length)) %>'>
Line 16:
Line 17:
The message says it all. Object does not have any property called Length. Convert it to a string first :
Min(150,Eval("news_body").ToString().Length)
vonbondies
ompilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0117: 'object' does not contain a definition for 'Length'
Source Error:
Line 13: Line 14: Line 15: news_body").ToString().Substring(0,Math.Min(150,Eval("news_body").Length)) %>'> Line 16: Line 17:
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 26: </tr> Line 27: <tr> Line 28: <td align="left" valign="top" class="fortune_text"><asp:Literal ID="ltr" Text ='<%# DataBinder.Eval(Container.DataItem, "firstname") + " " + DataBinder.Eval(Container.DataItem, "lastname").ToString().Substring(0,Math.Min(20,DataBinder.Eval(Container.DataItem, "firstname") + " " + DataBinder.Eval(Container.DataItem, "lastname").ToString().Length)) %>' runat="server"></asp:Literal> Line 29: <br /><%#Eval("country")%> Line 30: </td><td> </td>
[FormatException: Input string was not in a correct format.] Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +216 Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) +83
[InvalidCastException: Conversion from string "alok " to type 'Double' is not valid.] Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) +234 Microsoft.VisualBasic.CompilerServices.Operators.AddObject(Object Left, Object Right) +2824 ASP.controls_newuserctrl_ascx.__DataBinding__control6(Object sender, EventArgs e) in E:\globalteleportal\portallibrary\portalsite\controls\newuserctrl.ascx:28 System.Web.UI.Control.OnDataBinding(EventArgs e) +80 System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +198 System.Web.UI.Control.DataBind() +12 System.Web.UI.Control.DataBindChildren() +214 System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +208 System.Web.UI.Control.DataBind() +12 System.Web.UI.WebControls.DataList.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +130 System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource) +620 System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +57 System.Web.UI.WebControls.BaseDataList.DataBind() +62 controls_newuserctrl.bindlst() in E:\globalteleportal\portallibrary\portalsite\controls\newuserctrl.ascx.vb:18 controls_newuserctrl.binddata() in E:\globalteleportal\portallibrary\portalsite\controls\newuserctrl.ascx.vb:14 controls_newuserctrl.Page_Load(Object sender, EventArgs e) in E:\globalteleportal\portallibrary\portalsite\controls\newuserctrl.ascx.vb:7 System.Web.UI.Control.OnLoad(EventArgs e) +80 System.Web.UI.Control.LoadRecursive() +49 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3748
string Truncate(string input, int characterLimit) { string output = input;
// Check if the string is longer than the allowed amount // otherwise do nothing if (output.Length > characterLimit && characterLimit > 0) {
// cut the string down to the maximum number of characters output = output.Substring(0,characterLimit);
// Check if the character right after the truncate point was a space // if not, we are in the middle of a word and need to remove the rest of it if (input.Substring(output.Length,1) != " ") { int LastSpace = output.LastIndexOf(" ");
// if we found a space then, cut back to that space if (LastSpace != -1) { output = output.Substring(0,LastSpace); } } // Finally, add the "..." output += "..."; } return output; }
biggerbyfar
Member
5 Points
15 Posts
substring'ing a databound label to cap character limit?
Aug 07, 2007 07:56 PM|LINK
I have some code generated by VS2005 express, it works:
<asp:Label ID="bodyLabel" runat="server" Text='<%# Eval("news_body") %>'></asp:Label><br />
I wanted to chop the news_body data so that I could limit the maximum size of the text (with the intention to add a 'click for more' link where text IS too long)
I tried :
Eval(news_body).substring(1,150)
and also:
Eval(news_body).toString.substring(1,150)
But neither works. I'm struggling to grasp whats going on here.
Connect
Contributor
2304 Points
455 Posts
Re: substring'ing a databound label to cap character limit?
Aug 07, 2007 08:03 PM|LINK
limno
All-Star
117326 Points
8003 Posts
Moderator
MVP
Re: substring'ing a databound label to cap character limit?
Aug 07, 2007 08:04 PM|LINK
<asp:TemplateField HeaderText="news_body" SortExpression="news_body">
<ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("news_body").ToString().Substring(0,Math.Min(150,Eval("news_body").Length)) %>'></asp:Label> </ItemTemplate> </asp:TemplateField>Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
vonbondies
Member
48 Points
37 Posts
Re: substring'ing a databound label to cap character limit?
Oct 15, 2007 08:25 AM|LINK
<asp:TemplateField HeaderText="news_body" SortExpression="news_body"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("news_body").ToString().Substring(0,Math.Min(150,Eval("news_body").Length)) %>'></asp:Label> </ItemTemplate> </asp:TemplateField>I tried the above but i get the following error...:
mimbert
Member
4 Points
2 Posts
Re: substring'ing a databound label to cap character limit?
Jun 08, 2008 03:05 PM|LINK
The message says it all. Object does not have any property called Length. Convert it to a string first :
Min(150,Eval("news_body").ToString().Length)
parulitengg
Member
34 Points
12 Posts
Re: substring'ing a databound label to cap character limit?
Jun 10, 2008 07:02 AM|LINK
got an error
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Source File: E:\globalteleportal\portallibrary\portalsite\controls\newuserctrl.ascx Line: 28
Stack Trace:
Parul Jain
mimbert
Member
4 Points
2 Posts
Re: substring'ing a databound label to cap character limit?
Jun 10, 2008 12:19 PM|LINK
srinivas.jak...
Member
39 Points
4 Posts
Re: substring'ing a databound label to cap character limit?
Jan 30, 2009 06:43 PM|LINK
<asp:TemplateField HeaderText="news_body" SortExpression="news_body">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Truncate(Eval("news_body").ToString(),150) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Code Behind :
string Truncate(string input, int characterLimit) {
string output = input;
// Check if the string is longer than the allowed amount
// otherwise do nothing
if (output.Length > characterLimit && characterLimit > 0) {
// cut the string down to the maximum number of characters
output = output.Substring(0,characterLimit);
// Check if the character right after the truncate point was a space
// if not, we are in the middle of a word and need to remove the rest of it
if (input.Substring(output.Length,1) != " ") {
int LastSpace = output.LastIndexOf(" ");
// if we found a space then, cut back to that space
if (LastSpace != -1) {
output = output.Substring(0,LastSpace);
}
}
// Finally, add the "..."
output += "...";
}
return output;
}
for More Information gothrow following link
Control the Length
Control the Length