<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"] %>超过表格宽度时,截取,并后加"..."
//---------------------------
还有就是,通常,标题显示一行,如果一行显示不到,省略并加"..."
如果要求,显示两行,第一行显满,接到第二行显示,如果显示不下省略并加"...",又该如何实现
HorizontalAlign="Center">
<ItemTemplate>
<a href='javascript:void(0)'>
<%# ((System.Data.DataRow)Container.DataItem)["title"] %></a>
</ItemTemplate>
</asp:DataList>
//------------------------------------------------------------
当<%# ((System.Data.DataRow)Container.DataItem)["title"] %>超过表格宽度时,截取,并后加"..."
//---------------------------
还有就是,通常,标题显示一行,如果一行显示不到,省略并加"..."
如果要求,显示两行,第一行显满,接到第二行显示,如果显示不下省略并加"...",又该如何实现
解决方案 »
- 如何做到一个萝卜一个坑??(获得mac地址只能在局域网下有效,过了路由就不好使了)
- 自定义控件包含include文件时报错
- asp.net网页小偷问题,在线等【急】
- 这段js代码的错误在哪?
- 请问在ASP.NET页面中的页面状态属性“ViewState”具体是什么意思呢?
- 每页显示8张照片,要分成2行,每行四张,要怎么做?用什么来做?
- 客户端操作文件的问题 分数不够可以再加,前10名up者有分
- asp.net中有没有SPLIT这个函数把一个字符串分成数组?
- 获取远程图片问题!
- 求教:freamwork1.1是不是不支持CustomValidator?
- 想做个论坛,有没有asp.net2.0的开源代码?
- ajax.net的一个问题,参与有分!
<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(" "," ");
string HtmlTitle = "<A title=\"" + titleDemand + "\" >" + subDemand + "</A>"; return HtmlTitle;
}
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())%>
是用后台函数处理,最普遍的方法,但是这样的话,如果标题中英文字和中文汉字都算一个字符,
那么当英文多的时候,看起来短些~~ 当标题汉字多的时候看起来长些,因此在设置表格宽度时要设为最大情况下的宽度,以免又产生换行~~还有迁就,有些不快
其实从意思来讲,我们是要求当!长度超过表格宽度的时候截断,而不是保留多少个字符,比如10个
public string cutTitle(string title, int cutLength)
{
title = title.length > cutLength ? title.Substring(0, cutLength) : title;
return title;
}
前台调用:
<%# cutTitle(Container.DataItem, "title", 10)%>
麻烦,给个大概的代码
但不知道怎么用
麻烦会的大哥给个大概的代码 配合下段代码
<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>
/// 截取以字节计算的文本长度
/// </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);
}
但不知道怎么用
麻烦会的大哥给个大概的代码 配合下段代码
<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>
但不知道怎么用
麻烦会的大哥给个大概的代码 配合下段代码
<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>
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!=" ")
{
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;
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)%>
但不知道怎么用
麻烦会的大哥给个大概的代码 配合下段代码
<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>
但不知道怎么用
麻烦会的大哥给个大概的代码 配合下段代码
<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>