现在开发个网站,.net2.0环境在一个.aspx页面中使用了datagrid绑定数据是新闻标题代码为
<asp:DataList ID="datalistimg" runat="server">
  <ItemTemplate>
    · <a href='PictureNewsDetail.aspx?ID=<%#DataBinder.Eval(Container.DataItem,"id" )%>'>
    <%#DataBinder.Eval(Container.DataItem, "title").ToString()%>
    </a>                                                  
  </ItemTemplate>
</asp:DataList>由于有些标题长度太长,现在我想把标题
<%#DataBinder.Eval(Container.DataItem, "title").ToString()%>
截取为20个长度,不知如何,实现以前在asp里面是直接使用left函数,现在在c#里不知如何实现请教高手,在线等,谢谢

解决方案 »

  1.   

    /// <summary>
        /// 截获定长的字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="length">需要截获的长度</param>
        /// <returns>截获后的字符串</returns>
        static public string FixLenth( string source, int length )
        {
          return FixLenth( source, length, "..." );
        }
        /// <summary>
        /// 截获定长的字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="length">需要截获的长度</param>
        /// <param name="postfix">如果字符串被截短,需要添加什么样的后缀</param>
        /// <returns>截获后的字符串</returns>
        static public string FixLenth( string source, int length, string postfix )
        {
          if ( source == null )
            throw new ArgumentNullException( "source" );
          if ( postfix == null )
            postfix = "...";      if ( length < postfix.Length )
            throw new ArgumentOutOfRangeException( "length" );
          int postfixLength = System.Text.Encoding.GetEncoding( "GB2312" ).GetByteCount( postfix );
          int srcLength = System.Text.Encoding.GetEncoding( "GB2312" ).GetByteCount( source );      if ( srcLength > length )
          {
            for ( int i = source.Length; i > 0; i-- )
            {
              srcLength = System.Text.Encoding.GetEncoding( "GB2312" ).GetByteCount( source.Substring( 0, i ) );          if ( srcLength <= length - postfixLength )
                return source.Substring( 0, i ) + postfix;
            }
            return "";
          }
          else
            return source;
        }
      

  2.   

    sql里面处理:
    select (cast when len(列名)>20 then left(列名,20)+'.....' else 列名 end) as 新的列名  from 表名
      

  3.   

    其实你不用在datagrid中做处理,你直接在DataTable中做处理要简单多了,例如:
    yourDataTable.Columns.Add( "FixLengthTitle", typeof(string), "SUBSTRING(Title, 0, 10)" );
      

  4.   

    <ItemTemplate>
        · <a href='PictureNewsDetail.aspx?ID=<%#DataBinder.Eval(Container.DataItem,"id" )%>'>
        <%# GetSubString(DataBinder.Eval(Container.DataItem, "title").ToString(),20)%>
        </a>                                                  
      </ItemTemplate>
    ***************************
     /// <summary>
            /// 按字节截断字符串。
            /// </summary>
            public static string GetSubString(string mText,int byteCount)
            {
                if(byteCount < 1 ) return mText;
         
                
                if(System.Text.Encoding.Default.GetByteCount(mText) <= byteCount)
                {
                    return mText;
                }
                else
                {
                    byte[] txtBytes = System.Text.Encoding.Default.GetBytes(mText);
                    byte[] newBytes = new byte[byteCount-6];
                    
                    for(int i=0;i<byteCount-6;i++)
                        newBytes[i] = txtBytes[i];                return System.Text.Encoding.Default.GetString(newBytes) + "...";
                }
            }
      

  5.   

    谢谢Eddie005(♂) №.零零伍 (♂) 
    用你的方法实现了也谢谢ym2000(tt) 和 Knight94(愚翁) 的建议,小弟刚学c#,以后还要多请教大家现在结贴了
      

  6.   

    To: Eddie005(♂) №.零零伍 (♂) 
    你就不怕截出半个字符出来?
      

  7.   

    不会,试试就知道了~System.Text.Encoding.Default.GetString()方法会处理的~
      

  8.   

    不用截字符串,用样式来控制,看下这个<DIV style="BORDER-RIGHT: blue 1px solid; BORDER-TOP: blue 1px solid; OVERFLOW: hidden; BORDER-LEFT: blue 1px solid; WIDTH: 120px; BORDER-BOTTOM: blue 1px solid; HEIGHT: 50px; TEXT-OVERFLOW: ellipsis"><NOBR>就是比如有一行文字,很长,表格内一行显示不下.</NOBR> </DIV>