ASP.NET显示数据库文章标题前多少个字符加省略号
- C# code
- publicstring SubStr(string sString, int nLeng)
- {
- if (sString.Length <= nLeng)
- {
- return sString;
- }
- string sNewStr = sString.Substring(0, nLeng);
- sNewStr = sNewStr +"..."; return sNewStr;
- }
nLeng字符长度 sString给定的字符串
前台页面代码
- HTML code
- <%# SubStr(Convert.ToString(DataBinder.Eval(Container, "DataItem.NewsTitle")), 16)%>
- HTML code
- <asp:TemplateField HeaderText="新闻内容" SortExpression="新闻内容的列名">
- <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="grid1" Height="20px"/>
- <ItemTemplate>
- <a href='.\News\news_more.aspx?NewsID=<%# DataBinder.Eval(Container,"DataItem.NewsID") %>'>
- <span title='<%#DataBinder.Eval(Container, "DataItem.NewsTitle") %>'>
- <%# SubStr(Convert.ToString(DataBinder.Eval(Container, "DataItem.NewsTitle")), 16)%>
- </a>
- </ItemTemplate>
- </asp:TemplateField>
-