截取DataList中某列的文本
在用DataList显示数据时,由于第一列的值太长,所以想只让它显示10个字
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.FindControl( "lbProductName ") as Label).Text.Length > 10)
{
e.Item.ToolTip = (e.Item.FindControl( "lbProductName ") as Label).Text;
(e.Item.FindControl( "lbProductName ") as Label).Text = (e.Item.FindControl( "lbProductName ") as Label).Text.Substring(0, 10) + "... ";
}
}
但报错说未将对象引用设置到实例,错误行是:
if ((e.Item.FindControl( "lbProductName ") as Label).Text.Length > 10)
------解决方案--------------------要先判断一下当前datalist的索引是否是在表头上或者为datalist里面的行.如果是侧跳出
如果不是就执行你的
if ((e.Item.FindControl( "lbProductName ") as Label).Text.Length > 10)
{
e.Item.ToolTip = (e.Item.FindControl( "lbProductName ") as Label).Text;
(e.Item.FindControl( "lbProductName ") as Label).Text = (e.Item.FindControl( "lbProductName ") as Label).Text.Substring(0, 10) + "... ";
}
这句话