<%# DataBinder.Eval(Container.DataItem, "smalldir")%> 要取这个字段前2位.....怎么写?

解决方案 »

  1.   

    <%# getfirsttwo(DataBinder.Eval(Container.DataItem, "smalldir").ToString())%>
    后台写函数
    public string getfirsttwo(string temp)
    {
        return temp.Substring(0,2);
    }
      

  2.   

    <%# ((string)DataBinder.Eval(Container.DataItem, "smalldir")).Substring(0,2)%>
      

  3.   

    <%# ((string)DataBinder.Eval(Container.DataItem, "smalldir")).Substring(0,2)%>
    这种方法比较简单.
      

  4.   

    <%# ((DataBinder.Eval(Container.DataItem, "smalldir")).ToString().Length>3)?(DataBinder.Eval(Container.DataItem, "smalldir")).ToString().Substring(0,2): DataBinder.Eval(Container.DataItem, "smalldir")%>
      

  5.   


    <%# getfirsttwo(DataBinder.Eval(Container.DataItem, "smalldir").ToString())%>
    后台写函数
    public string getfirsttwo(string temp)
    {
        return temp.Substring(0,2);
    }
      

  6.   

    <%#((string)DataBinder.Eval(Container.DataItem,"name")).Substring(0,2)%>
      

  7.   

    长度不到2为的话会报错,建议在函数中加个判断:
    <%#getnewstring(DataBinder.Eval(Container.DataItem, "name").ToString())%>
    public string getnewstring(string wangzhe)
    {
    if(wangzhe.Length<2)
    {
    return wangzhe;
    }
    else
    {
    return wangzhe.Substring(0,2);
    }
    }
      

  8.   

    你可以在cs代码里面写一个方法:
    protected string CutString(string str,int length)
    {
     string newString="";
     if(str!="")
     {
      if(str.Length>length)
      {
       newString=str.Substring(0,length)+"...";
      }
      else
      {
       newString=str;
      }  
     }
     return newString;
    }
    然后:
    <%# CutString(DataBinder.Eval(Container.DataItem,"newtitle").ToString(),10) %>