Click here to Skip to main content
15,917,997 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi all!
who cant help me this proplem
i am show the Title of one News from database, and i want limit the displayed text length, i do this but it not work. my code below

ASP.NET
<a ID="A1"  runat="server" href='<%# "~/detailNews.aspx?id=" + Eval("id") %>'> <%# Eval("Title").ToString().Substring(0,39)%></a>


thanks special!
Posted

< % #GetTitle(Eval("Title"))%>
public string GetTitle(object title)
{
string _title=Convert.Tostring(title);
       if(_title.Length>39)
{
           return  _title.SubString(0,39);
}
else
{
 return _title;
}
}
 
Share this answer
 
v2
ASP.NET
<asp:datalist id="dlNewsPromotion" runat="server" bordercolor="Black" xmlns:asp="#unknown">
    CellPadding="0" Height="16px" RepeatColumns="1" 
    style="margin-right: 0px;padding-left:20px;" Width="250px">
    <itemtemplate>
        <div id="idLink">
            <table align="left" cellpadding="0" cellspacing="0">
                <tr>
                    <td>
                        »   <a id="A1" runat="server">
                            href='<%# "~/detailNews.aspx?id=" + Eval("id") %>'> <%#Eval("Title").ToString().Length > 39) ? Eval("Title").ToString().Substring(0, 39) : Eval("Title")%></a><br />
                        <div style="margin-left:15px;">
                        <<asp:Label 
                            ID="lblDateCreate" runat="server" Font-Size="10px" ForeColor="#006666" 
                            Text='<%# Eval("DateCreate").ToString().Substring(0, 10) %>'>> 
                        </div>   
                    </td>
                        
                </tr>
            </table>
            <br />
        </div>
    </itemtemplate>
</asp:datalist>
 
Share this answer
 
When the string is shorter than 39, this will give an exception "Index and length must refer to a location within the string."

this should work :

REPLACE

Eval("Title").ToString().Substring(0,39)

with this

(Eval("Title").ToString().Length > 39) ? Eval("Title").ToString().Substring(0, 39) : Eval("Title")
 
Share this answer
 
Comments
killerqx1 13-Mar-12 23:31pm    
thank you! let me try it
killerqx1 13-Mar-12 23:34pm    
http://nj9.upanh.com/b6.s13.d3/161ca90c128e147111bb9b16f22bcf8c_42042049.untitled.jpg

error!
Malek Chtiwi 13-Mar-12 23:48pm    
i just tried it and works fine, hit f5 and give me the error message you get
killerqx1 14-Mar-12 0:02am    
here this is errors

http://nj6.upanh.com/b2.s16.d1/fd609ed03af146ba721049efccd94586_42043446.untitled.jpg
Malek Chtiwi 14-Mar-12 0:06am    
you're missing a '(' in the beginning, just before Eval :

(Eval("Title").ToString().Length > 39) ? Eval("Title").ToString().Substring(0, 39) : Eval("Title")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900