用GRIDVIEW绑定数据库里面的某个字段因为这个字段内容太多 想只绑定前几个字 然后点击看详细内容 应该怎么做啊?怎么才能绑定前几个字 像这样“csdn...”
模板列
<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;
}上面这段代码 老提示“未将对象引用设置到对象的实例。”string str = lbl.Text;
哪不对啊  

解决方案 »

  1.   

    在label加入class="clip_text" width="200" //200是你限制的宽度在相关css文件中加入
    .clip_text
    {
    text-overflow:ellipsis; white-space:nowrap; overflow:hidden;
    }可以自动剪去多余字符,并显示...
      

  2.   

    在RowItemBound事件加上
    if(e.Row.Rowtype=DataControlRowType.dataRow)
    {
        .....
    }
      

  3.   

    <asp:Label runat='server' id='lbl1' Text=' <%# Eval("MyField").ToString().Length > 10 ?  Eval("MyField").ToString().Substring(0, 10) + "..." : Eval("MyField").ToString())%>' ToolTip = '<%# Eval("MyField") %>' > </asp:Label> 
      

  4.   

    另外gridView只有RowDataBound,没有RowItemBound
      

  5.   


    似乎 firefox 中不好用.. 
      

  6.   


    也可以实现,就是麻烦些
    http://www.aoao.org.cn/blog/2008/07/firefox-text-overflow-ellipsis/
      

  7.   

    使用4楼的方法也可以,但是汉字和字母剪取的实际长度不一致,会很难看
    使用text-overflow是按px计算,很精确看楼主自己取舍了