限制显示的文字长度 [英] limit the displayed text length

查看:88
本文介绍了限制显示的文字长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
谁不能帮助我这个问题
我从数据库中显示了一个新闻的标题,并且我想限制显示的文本长度,但是这样做是行不通的.我的下面的代码

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

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





thanks special!

推荐答案

当字符串短于39时,将出现异常"索引和长度必须引用字符串中的位置." /i>"

这应该可以工作:

更换

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

与此

(Eval("Title").ToString().Length> 39)吗? Eval("Title").ToString().Substring(0,39):Eval("Title")
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")


<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>


< % #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;
}
}


这篇关于限制显示的文字长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆