模板列
<itemtemplate>
<asp:Label runat='server' id='lbl1' Text='<%# Eval("MyField") %> '></asp:Label>
在RowItemBound事件里写
Label lbl = (Label)e.row.findControl("lbl1");
string str = lbl.Text;
if(str.Length > 10)
{
lbl.Text = str.Substring(0, 10) + ".....";
lbl.tooltip= str;
}
这样即可以截取过多的字符 ,当鼠标放上去还可以显示全部内容
其他:
将字符串截取显示的3个方法
/// <summary>
/// 超长的字符串变为:"我是..."形式
/// </summary>
/// <param name="sString">显示的字段或值 </param>
/// <param name="nLeng">正常显示的长度 </param>
/// <returns> </returns>
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng - 2);
sNewStr = sNewStr + "...";
sNewStr = " <a href=#>" + sNewStr + " <span>" + sString + " </span> </a>";
return sNewStr;
}
/// <summary>
/// 超长的字符串变为:"我是..."形式
/// </summary>
/// <param name="sString">显示的字段或值 </param>
/// <param name="nLeng">正常显示的长度 </param>
/// <param name="css">样式 </param>
/// <returns> </returns>
public string SubStr(string sString, int nLeng,string css)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng - 2);
sNewStr = sNewStr + "...";
sNewStr = " <a class=" + css + " href=#>" + sNewStr + " <span>" + sString + " </span> </a>";
return sNewStr;
}
/// <summary>
/// 超长的字符串变为:"我是..."形式,并添加连接
/// </summary>
/// <param name="sString">显示的字段或值 </param>
/// <param name="nLeng">正常显示的长度 </param>
/// <param name="css">样式 </param>
/// <param name="url">连接的URL </param>
/// <returns> </returns>
public string SubStr(string sString, int nLeng, string css,string url)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng - 2);
sNewStr = sNewStr + "...";
sNewStr = " <a class=" + css + " href=" + url + ">" + sNewStr + " <span>" + sString + " </span> </a>";
return sNewStr;
}
页面上就用
<asp:TemplateColumn HeaderText="备注">
<ItemTemplate>
<asp:Label Text=' <%# SubStr(DataBinder.Eval(Container.DataItem,"title").ToString(),10)%>' ID="Label1" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>