1,把代碼放在sqlhelper中。 復制代碼 代碼如下:
#region 截取字符串
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="str_value"></param>
/// <param name="str_len"></param>
/// <returns></returns>
public static string leftx(string str_value, int str_len)
{
int p_num = 0;
int i;
string New_Str_value = "";
if (str_value == "")
{
New_Str_value = "";
}
else
{
int Len_Num = str_value.Length;
for (i = 0; i <= Len_Num - 1; i++)
{
if (i > Len_Num) break;
char c = Convert.ToChar(str_value.Substring(i, 1));
if (((int)c > 255) || ((int)c < 0))
p_num = p_num + 2;
else
p_num = p_num + 1;
if (p_num >= str_len)
{
New_Str_value = str_value.Substring(0, i + 1);
break;
}
else
{
New_Str_value = str_value;
}
}
}
return New_Str_value;
}
#endregion
2,前台調用: 復制代碼 代碼如下:
<%# UserInfo.DAL.SqlHelper.leftx(DataBinder.Eval(Container.DataItem,"Title").ToString(),20) %>