新闻标题过长,显示省略号
我将数据库中读出的新闻标题用Eval绑定到Datalist控件的ItemTemplate模板上,请问如何将过长的标题显示为省略号?<%# Eval("a").ToString().Length>12 ? Eval("a").ToString().Substring(0,12) + "..." : Eval("a") %>这方法为什么行不通 --------------------编程问答-------------------- substring() --------------------编程问答-------------------- 最好在cs页面写一个方法来处理使用subString()来截取,大于数值长度就加...就OK了 --------------------编程问答-------------------- 你的太复杂了 直接substring()就可以了吧 --------------------编程问答-------------------- 在行绑定事件里面
//判断数据行状态
if (e.Row.RowType == DataControlRowType.DataRow)
{
string text = e.Row.Cells[0].Text;
if (text.Length > 12)
{
e.Row.Cells[0].Text = text.Substring(0, 12);
}
} --------------------编程问答-------------------- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判断数据行状态
if (e.Row.RowType == DataControlRowType.DataRow)
{
string text = e.Row.Cells[0].Text;
if (text.Length > 12)
{
e.Row.Cells[0].Text = text.Substring(0, 12);
}
}
} --------------------编程问答-------------------- 设置列的宽度.. 用CSS 控制
css代码:
div{width:200px;}
span {
display:block;
width:200px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
html代码:
<div>
<span>CSS Web Design</span>
</div>
--------------------编程问答-------------------- up --------------------编程问答-------------------- up --------------------编程问答-------------------- up --------------------编程问答-------------------- .protected string cutString(sring str,int len)
{
if(str.Length>len)
return str
else
return str.subString(0,len);
} --------------------编程问答-------------------- 后台加入这个方法
/// <summary>
/// 截取指定长达字符串 多余的用...代替
/// </summary>
/// <param name="str"></param>
/// <param name="length"></param>
/// <returns></returns>
protected string StringCut(string str,int length)
{
if(str ==string.Empty|| str == null)
return str;
string temp=string.Empty;
if(str.Length < length)
{
temp = str;
}
else
{
temp = str.Substring(0,length-6)+"......";
}
return temp;
}
前面绑定这么写
<%# StringCut(Eval("a").ToString(),12) %>
--------------------编程问答-------------------- /// <summary>
/// 从开始位置,根据截取长度正负,向左或向右截取固定长度的字符串
/// </summary>
/// <param name="inputString">输入要截取的字符串</param>
/// <param name="startIndex">开始位置</param>
/// <param name="length">截取内容字节数:正=右;负=左;</param>
/// <param name="strMore">表示还有更多字符的符号</param>
/// <returns></returns>
public static string SubString(string inputString, int startIndex, int length, string strMore)
{
//字符串实际长度
int stringLen = System.Text.Encoding.Default.GetBytes(inputString).Length;
int iWord = 0,iByte = 0;
string returnString = "";
//向左截取
if (length < 0)
{
if (stringLen <= startIndex)
{
if (-length > stringLen) startIndex = stringLen; else startIndex = stringLen - 1;
}
if (startIndex >= -length)
{
startIndex = startIndex + length + 1;
length = -length;
}
else
{
length = startIndex+1;
startIndex = 0;
}
return SubString(inputString, startIndex, length, strMore);
}
//向右截取
if (stringLen > startIndex)
{
for (int i = 0; i < inputString.Length; i++)
{
int c = (int)inputString[i];
if (c < 0) c += 65536;
if (c > 255) iByte += 2; else iByte += 1;
if (iByte > startIndex)
{
iWord = i;
break;
}
}
}
else
{
return returnString;
}
iByte = 0;
if (stringLen > (startIndex + length))
{
for (int i = iWord; i < inputString.Length; i++)
{
int c = (int)inputString[i];
if (c < 0) c += 65536;
if (c > 255) iByte += 2; else iByte += 1;
if (iByte > length) break; else returnString += inputString[i].ToString();
}
if (returnString.Length > 0) returnString += strMore;
}
else
{
returnString = inputString.Substring(iWord);
}
return returnString;
}
public static int FindRepeatChar(string inputString, char repeatString)
{
int counter = 0;
for (int i = 0; i < inputString.Length; i++)
{
if (inputString[i] == repeatString)
{
counter++;
}
}
return counter;
} --------------------编程问答-------------------- 上楼代码多贴了一个查找字符在字符串中出现次数的方法,在此说明下
/// <summary>
///此方法是判断一个字符在一个字符串中出现的次数
/// </summary>
///<param name="inputString">输入字符串 </param>
///<param name="repeatString">输入字符 </param>
///一共三种判断方法,此方法代码最多,但性能最好
public static int FindRepeatChar(string inputString, char repeatString)
{
int counter = 0;
for (int i = 0; i < inputString.Length; i++)
{
if (inputString[i] == repeatString)
{
counter++;
}
}
return counter;
} --------------------编程问答-------------------- 其实你要想单用程序来控制的话是很难保证所有的都能按照你的要求来显示的,不同的编码,不同的转意字符,比如:&nbsp这样的算几个长度?但显示的时候只是一位罢了,所以要想办法用css+程序一块来实现才能正真的解决这个问题.... --------------------编程问答--------------------
--------------------编程问答-------------------- <%# Eval("a").ToString().Length>12 ? Eval("a").ToString().Substring(0,12) + "..." : Eval("a") %>
public static string SubString(string stringToSub, int length, string stringToAdd)
{
//stringToSubChar = Convert.ToByte(stringToSub);
Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
char[] stringChar = stringToSub.ToCharArray();
char[] stringToAddChar = stringToAdd.ToCharArray();
length = length - stringToAddChar.Length;
StringBuilder sb = new StringBuilder();
int nLength = 0;
for (int i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString()))
{
sb.Append(stringChar[i]);
nLength += 2;
}
else
{
sb.Append(stringChar[i]);
nLength = nLength + 1;
}
if (nLength >= length)
{
if (i + 1 < stringChar.Length)
sb.Append(stringToAdd);
break;
}
}
return sb.ToString();
}
Eval("a").ToString().Substring(0,12) + "..." 字符串
Eval("a") 对象
--------------------编程问答-------------------- 直接subString当然有问题的 --------------------编程问答-------------------- 楼主用的三元符号 看起来没什么问题呀! 但是为什么不能不限显示呢! 当然在cs 写个函数来这个方法更好! --------------------编程问答-------------------- 直接substring截取 --------------------编程问答-------------------- 五楼的ok --------------------编程问答-------------------- <%#Eval("a").ToString().Length>12?Eval("a").ToString().Substring(0,12) + "...":Eval("a")%>
把我写的复制过去,再试试,我已经试过,肯定能行,要不然的话就是你的数据短 --------------------编程问答-------------------- Eval("a").ToString().Length改为Convert.ToString(Eval("a")).Length试试 --------------------编程问答-------------------- aspx页面
<%# Content1(Eval("标题"))%>
.cs页面
public string Content1(object article)
{
string content = (string)article;
string i = ".....................";
if (content.Length > 16)
{
content = content.Substring(0, 13) + "....";
}
else
{
content = content + i.Substring(0, 17 - content.Length);
}
return content;
}
不过标题确认没有空格,或数字,这里的点号是中文全角的 --------------------编程问答-------------------- <%# Eval("a").ToString().Length>12?Eval("a").ToString().Substring(0,12) + "..." : Eval("a") %>
此处不要加空格 --------------------编程问答-------------------- 学习~!帮顶·! --------------------编程问答-------------------- 我这个性能非常好。
#region 截短字串的函数,兼容中英文
/// <summary>
/// 截短字串的函数,兼容中英文
/// </summary>
/// <param name="mText">要加工的字串</param>
/// <param name="byteCount">长度</param>
/// <returns>被加工过的字串</returns>
public static string Left(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 - 4];
for (int i = 0; i < byteCount - 4; i++)
{
newBytes[i] = txtBytes[i];
}
string OutPut = System.Text.Encoding.Default.GetString(newBytes) + "...";
if (OutPut.EndsWith("?...") == true)
{
OutPut = OutPut.Substring(0, OutPut.Length - 4);
OutPut += "...";
}
return OutPut;
}
}
#endregion
补充:.NET技术 , ASP.NET