现在开发个网站,.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#里不知如何实现请教高手,在线等,谢谢
<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#里不知如何实现请教高手,在线等,谢谢
解决方案 »
- C#根据某行文本的部分字符删除该行
- 部门汇总统计
- 用Ajax取出数据绑定到dropdownlist上,在服务器端如何获取dropdownlist的selectedvalue?
- 在日历控件中如何规定用户只能选择每个星期的星期四,我找到了部分代码,可是不行啊,有谁可以帮我啊/
- 帮忙介绍一下combobox 的绑定问题,(不好意思,我没有分给大家了)
- 求几道实用的代码(有关光区的)
- 有没有哪位知道高级程序员什么时候报名啊!
- 如何将含有中文的string转换为sbyte[]型
- 能否提供一个net2005的下载地址 谢谢了
- 急!急!WEB页中怎样连接帮助?在线等待!!
- C#编程封局域网中其它计算机IP地址或端口号
- 关于.net及IIS的超变态问题。
/// 截获定长的字符串
/// </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;
}
select (cast when len(列名)>20 then left(列名,20)+'.....' else 列名 end) as 新的列名 from 表名
yourDataTable.Columns.Add( "FixLengthTitle", typeof(string), "SUBSTRING(Title, 0, 10)" );
· <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) + "...";
}
}
用你的方法实现了也谢谢ym2000(tt) 和 Knight94(愚翁) 的建议,小弟刚学c#,以后还要多请教大家现在结贴了
你就不怕截出半个字符出来?