在DataGrid,DataList裡面怎麼控制列表裡得內容不換行,並且在800的分辨率下顯示12個漢字,24個英文字符,而在1024的分辨率下顯示15個漢字?
     virtual protected void dgShow_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGrid grid = (DataGrid)sender;
if( e.Item.ItemType == ListItemType.Item)
{
foreach(TableCell cell in e.Item.Cells )
cell.Attributes.Add("style","word-wrap:normal;break-word:keep-all");
}
                  }
還有,上面得代碼問什麼不能控制列表內容不換行啊

解决方案 »

  1.   

    你如果想在不同分辨率下,显示不同的长度,那么你就要取客户端的分辨率,根据分辨率设定一个参数,来决定取多少.
    下面给你一个根据文字的字节取长度的函数,你调用一下./// <summary>
    /// 按字符串实际长度截取定长字符窜
    /// </summary>
    /// <param name="str">原字符串</param>
    /// <param name="length">要截取的长度</param>
    public static string GetString(string str, int length)
    {
    int i = 0, j = 0;
    foreach(char chr in str)
    {
    if((int)chr > 127)
    {
    i += 2;
    }
    else
    {
    i ++;
    }
    if (i > length)
    {
    str = str.Substring(0, j) + "...";
    break;
    }
    j ++;
    }
    return str;
    }
      

  2.   

    TO: hchxxzx(NET?摸到一点门槛)
        關鍵是自動換行,就像Iput type = text 用px固定大小後,不會撐大表格,也不自動換行.
        截取字符串,我覺得用BitConvert.GetBetyes()要好一些