在使用 datalist 等控件绑定前台数据的时候,有时希望一行只显示定长字符,多出来的部分以省略号代替,我使用三元运算符来做: < asp:DataList id ="DataList1" runat ="server" RepeatColumns ="5" Width ="616px" > < ItemTemplate > < table border ="0" cellpadding ="2" cellspacing ="0" align ="center" width ="100%" height ="100%" > < tr > < td align ="center" > < div style ="height:10%;width:10%" > <% # DataBinder.Eval(Container.DataItem,"picexplanation").ToString().Length>10?DataBinder.Eval(Container.DataItem,"picexplanation").ToString().Substring(0,10)+"":DataBinder.Eval(Container.DataItem,"picexplanation") %> </ div > </ td > </ tr > < tr > < td align ="center" > < div style ="height:90%;width:90%" > < a href ='viewphoto.aspx?imgurl=<%# DataBinder.Eval(Container.DataItem,"primitivepic")% > ' target="_blank"> < IMG height =88 src ='../../../../../../upload_pic/userpic_small/<%# DataBinder.Eval(Container.DataItem,"primitivepic")% > ' width=88 border=0> </ a ></ div > </ td > </ tr > </ table > </ ItemTemplate > </ asp:DataList > 用 Substring(x,x)定你需要显示的长度,而且表格高度都是固定的,这样在首页显示起来比较美观,不至于因为数据过长而导致变形,其实使用 div+css可以做的更好些,但是俺还没有学好那个东西,呵呵。大家有好的办法多多讨论,共同进步 转载于:https://www.cnblogs.com/devngm/archive/2007/04/11/708416.html