我在GRIDVIEW里绑定数据
<%# Bind("commContent") %>
因那这表该字段里有长的有短的.为方便显示
我改成这样
<%# (Eval("commContent")).ToString().Substring(0,10) %>
有小于10字符时就报错了:
索引和长度必须引用该字符串内的位置。
参数名: length PS:由于其他原因,我不能在sql语句中使用left/substring 截取字符长度我用自定义函数在绑定时处理,不成功,不知失败在哪..
请教如何做?

解决方案 »

  1.   

    自定义个个切割字符串的函数。如CutString,
    然后:CutString(Bind("commContent").ToString(),10)
      

  2.   

    public string GetTen(string s)
    {
        if(s.Length>10)
              return s.Substring(0,10);
        else
            return s;
    }<%# GetTen(Bind("字段名"))%>
      

  3.   

    结帖,谢谢,开始在VS里写ASPX时用自定义函数,我CTRL+J它不出代码提示我以为不行.运行下可以.不知什么不出提示...
      

  4.   

    不用函数,直接一个判断!
    <%# (Eval("commContent")).ToString().Length<=10?Eval("commContent")).ToString():Eval("commContent")).ToString().Substring(0,10) %>
      

  5.   

    同意楼上的<%# (Eval("commContent")).ToString().Length<=10?Eval("commContent")).ToString():Eval("commContent")).ToString().Substring(0,10) %>