<asp:DataList Runat="server" ID="dlst1" CellPadding="0" CellSpacing="0" Width="200"
HorizontalAlign="Center">
             <ItemTemplate>
<a href='javascript:void(0)'>
<%# ((System.Data.DataRow)Container.DataItem)["title"] %></a>
   </ItemTemplate>
</asp:DataList>
//------------------------------------------------------------
当<%# ((System.Data.DataRow)Container.DataItem)["title"] %>超过表格宽度时,截取,并后加"..."
//---------------------------
还有就是,通常,标题显示一行,如果一行显示不到,省略并加"..."
如果要求,显示两行,第一行显满,接到第二行显示,如果显示不下省略并加"...",又该如何实现

解决方案 »

  1.   

    请参考:1.前台
      <asp:TemplateColumn>
    <ItemStyle HorizontalAlign="Left" Wrap="true"></ItemStyle>
    <ItemTemplate>
    <asp:Label runat="server" Text='<%# GetDemand(DataBinder.Eval(Container, "DataItem.demand").ToString())%>'>
    </asp:Label>
    </ItemTemplate>
     </asp:TemplateColumn>
    2.后台
    protected string GetDemand(string strDemand)
    {
    string subDemand = strDemand;

    //取前10个字串(如果需求长度小于十个,则取原串) if(subDemand.Length > 10)     
    {
    subDemand = subDemand.Substring(0,10) + "...";
    }
    string titleDemand = strDemand.Replace("<br>","\r\n").Replace("&nbsp;"," ");
    string HtmlTitle = "<A title=\"" + titleDemand + "\" >" + subDemand + "</A>"; return HtmlTitle;  
    }
      

  2.   

    后台:
        public string GetInstancy(string sInstancy)
        {
            string strReturn = "";
            if (sInstancy.Trim().length>???)
                 strReturn = sInstancy.Trim().Substring(0,10);
            else 
                 strReturn = sInstancy;
            return strReturn;
        }
    前台調用:<%# GetInstancy(((DataRowView)Container.DataItem)["????"].ToString().Trim())%>
      

  3.   

    zeng_c_h(嘿嘿)  的方法
    是用后台函数处理,最普遍的方法,但是这样的话,如果标题中英文字和中文汉字都算一个字符,
    那么当英文多的时候,看起来短些~~  当标题汉字多的时候看起来长些,因此在设置表格宽度时要设为最大情况下的宽度,以免又产生换行~~还有迁就,有些不快
       其实从意思来讲,我们是要求当!长度超过表格宽度的时候截断,而不是保留多少个字符,比如10个
      

  4.   

    后台写一个public 的方法,比如
    public string cutTitle(string title, int cutLength)

        title = title.length > cutLength ? title.Substring(0, cutLength) : title;
        return title;
    }
    前台调用:
    <%# cutTitle(Container.DataItem, "title", 10)%>
      

  5.   

    用css如何具体实现
    麻烦,给个大概的代码
      

  6.   

    我做过的是用substring判断是否足够长.然后截取相应的字符.详细内容绑定ToolTip
      

  7.   

    我听说用css可以实现
    但不知道怎么用
    麻烦会的大哥给个大概的代码  配合下段代码
    <asp:DataList Runat="server" ID="dlst1" CellPadding="0" CellSpacing="0" Width="200"
    HorizontalAlign="Center">
                 <ItemTemplate>
    <a href='javascript:void(0)'>
    <%# ((System.Data.DataRow)Container.DataItem)["title"] %></a>
       </ItemTemplate>
    </asp:DataList>
      

  8.   

    /// <summary>
    /// 截取以字节计算的文本长度
    /// </summary>
    /// <param name="msytr">要截取的文本串</param>
    /// <param name="intLength">要截取的字节数</param>
    /// <returns>返回截取的字节数</returns>
    public string myCutString(string mystr,int intLength)
    {
    Byte[] myByte = System.Text.Encoding.Default.GetBytes(mystr);
    string str = "";
    if(myByte.Length < intLength)
    {
    str = mystr;
    }
    else
    {
    str = System.Text.Encoding.Default.GetString(myByte,0,intLength);
    switch(System.Text.Encoding.Default.GetByteCount(str) - intLength)
    {
    case 1:
    str += "..";
    break;
    case -1:
    str += "....";
    break;
    default:
    str += "...";
    break;
    }
    }
    return(str);
    }
      

  9.   

    我听说用css可以实现
    但不知道怎么用
    麻烦会的大哥给个大概的代码  配合下段代码
    <asp:DataList Runat="server" ID="dlst1" CellPadding="0" CellSpacing="0" Width="200"
    HorizontalAlign="Center">
                 <ItemTemplate>
    <a href='javascript:void(0)'>
    <%# ((System.Data.DataRow)Container.DataItem)["title"] %></a>
       </ItemTemplate>
    </asp:DataList>
      

  10.   

    我听说用css可以实现
    但不知道怎么用
    麻烦会的大哥给个大概的代码  配合下段代码
    <asp:DataList Runat="server" ID="dlst1" CellPadding="0" CellSpacing="0" Width="200"
    HorizontalAlign="Center">
                 <ItemTemplate>
    <a href='javascript:void(0)'>
    <%# ((System.Data.DataRow)Container.DataItem)["title"] %></a>
       </ItemTemplate>
    </asp:DataList>
      

  11.   

    给你两段 两个实现方法
    int zf=0;//字符记数
    string strpoint="";
    string str="";//暂存列信息
    e.Item.Cells[3].Width=140;
    e.Item.Cells[4].Width=60;
    e.Item.Cells[5].Width=60;
    e.Item.Cells[6].Width=60;
    e.Item.Cells[7].Width=160;
    if(e.Item.ItemType==ListItemType.Item ||e.Item.ItemType==ListItemType.AlternatingItem)
    {
    for(int i=3;i<=7;i++)
    {
    if(e.Item.Cells[i].Text!="&nbsp;")
    {
    for(int ii=0;ii<e.Item.Cells[i].Text.Length;ii++)
    {
    if(Convert.ToChar(e.Item.Cells[i].Text.Substring(ii,1))>255)
    {
    if(zf+14<e.Item.Cells[i].Width.Value)//24为3个.的预留长度
    {
    zf+=14;//估计汉字宽度
    str+=e.Item.Cells[i].Text.Substring(ii,1);
    }
    else
    {
    strpoint="...";
    }
    }
    else
    {
    if(zf+8<e.Item.Cells[i].Width.Value)
    {
    zf+=8;//估计字符宽度
    str+=e.Item.Cells[i].Text.Substring(ii,1);
    }
    else
    {
    strpoint="...";
    }
    }
    }
    //=======================================================
    if(strpoint=="...")
    {
    str=str.Remove(str.Length-3,3);
    }
    e.Item.Cells[i].Text=str;
    e.Item.Cells[i].Text+=strpoint;
    strpoint="";
    str="";
    zf=0;
    //==========================================================
    }//end if

    }
    }
    第二个
    Bitmap bm = new Bitmap(1, 1); System.Drawing.Graphics g = Graphics.FromImage(bm); Font font = new Font(grid.Font.Name,11);

    string test = Convert.ToString((int)grid.Font.Size.Type);
    string test1 = test;
    // Calculate the dimensions of the text with the current font
    //grid.Font
    SizeF textSize = g.MeasureString(text,font); // Compare the size with the column's width 
    int colWidth = (int) grid.Columns[colIndex].ItemStyle.Width.Value;
    if(textSize.Width > colWidth)
    {
    // Get the exceeding pixels 
    int delta = (int) (textSize.Width - colWidth); //text.Substring(0,(text.Length-delta));
      
    // Calculate the average width of the characters (approx)
    int avgCharWidth = (int) (textSize.Width/text.Length);
      
    // Calculate the number of chars to trim to stay in the fixed width (approx)
    int chrToTrim = (int) (delta/avgCharWidth);
      
    // Get the proper substring + the ellipsis
    // Trim 2 more chars (approx) to make room for the ellipsis
    string rawText = text.Substring(0, text.Length-(chrToTrim+4)) + ".."; 
      
    // Format to add a tooltip
    string fmt = "{1}";
    return  String.Format(fmt, text, rawText);


    return text;
      

  12.   

    #region 截取字符
    static public string Left(string str,int L)
    {
    string tmpStr;
    tmpStr=str;
    if(str.Length > L)
    {

    tmpStr=str.Substring(0,L)+"......";
    }
    return tmpStr;
    }
           
    #endregion引用:<%#Left(Convert.ToString(DataBinder.Eval(Container.DataItem,"ArtTitle")),90)%>
      

  13.   

    我听说用css可以实现
    但不知道怎么用
    麻烦会的大哥给个大概的代码  配合下段代码
    <asp:DataList Runat="server" ID="dlst1" CellPadding="0" CellSpacing="0" Width="200"
    HorizontalAlign="Center">
                 <ItemTemplate>
    <a href='javascript:void(0)'>
    <%# ((System.Data.DataRow)Container.DataItem)["title"] %></a>
       </ItemTemplate>
    </asp:DataList>
      

  14.   

    我听说用css可以实现
    但不知道怎么用
    麻烦会的大哥给个大概的代码  配合下段代码
    <asp:DataList Runat="server" ID="dlst1" CellPadding="0" CellSpacing="0" Width="200"
    HorizontalAlign="Center">
                 <ItemTemplate>
    <a href='javascript:void(0)'>
    <%# ((System.Data.DataRow)Container.DataItem)["title"] %></a>
       </ItemTemplate>
    </asp:DataList>