用模板绑定了数据,想控制字数在4个以内,如果大于4个字则多余的字显示成"...",若小于4个字则全部显示.

解决方案 »

  1.   

    /// <summary>
        /// 字符拆分处理
        /// </summary>
        /// <param name="strTemp">数据源</param>
        /// <param name="intLen">拆取的长度</param>
        /// <param name="AddPoint">是否加在后面加"...",为true则添加,否则不添加</param>
        /// <returns></returns>
        public string SubstringLength(string strTemp,int intLen, bool AddPoint)
        {
            string strApp = "";        int strLength = System.Text.Encoding.Default.GetBytes(strTemp).Length;        if (strLength > intLen)
            {
                strApp = strTemp.Substring(0, intLen);
                if (AddPoint == true) strApp += "...";
            }
            else
            {
                strApp = strTemp.ToString();
            }        //****返回重新构造的字符
            return strApp;
        }//****调用函数
    <%#SubstringLength(DataBinder.Eval(Container.DataItem,"Context").ToString(),4,true)%>//****第二种方法用SQL
    SELECT *,CASE WHEN LEN(CONTEXT)>4 THEN SUBSTRING(CONTEXT,0,4)+'...' ELSE CONTEXT END AS CONTEXTNAME FROM TB_INFO
      

  2.   

    是在ASP.NET中写这个函数吗?
    有在HTML绑定的时候做判断的吗?
      

  3.   

    我记得好像css2.0里有这个功能
      

  4.   

    你参考一下下面的代码吧<html xmlns:rdl>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>Rainer's Handbook</title>
    <style>
    body{margin:0px;padding:12px;background:#FFFFFF;overflow:auto;}
    body,table,input,select,textarea,a{font-family:verdana,tahoma,arial;font-size:11px;color:#000000;word-break:break-all;}
    table,img{border:0px;}
    a{text-decoration:none;color:#003399;}
    a:hover{color:#000000;text-decoration:underline;}
    #id_div3{padding-top:8px;border-top:1px solid #000000;line-height:15px;}
    #id_span3{font-size:10px;font-family:tahoma;}
    </style>
    <style>
    #idDIV{width:100%;height:90px;background-color:#87CEEB;padding:8px;overflow:hidden; text-overflow:ellipsis;white-space:nowrap; }
    #idCodeDiv{width:100%;padding:4px;font-family:verdana,tahoma;margin:12px 0px 0px 0px;background-color:#EEEEEE;font-weight:bold;}
    </style><script>
    function rdl_change(e){
    var oCodeDiv=document.all("idCodeDiv");
    var oDiv=document.all("idDiv");
    with (document.all("idSel1")) var sValue1=options[selectedIndex].value;
    with (document.all("idSel2")) var sValue2=options[selectedIndex].value;
    with (oDiv.style) {
    overflow=sValue1;textOverflow=sValue2;
    }
    oCodeDiv.innerText="overflow : "+sValue1+"\r\n"+"text-overflow : "+sValue2+" ;";
    }
    </script></head>
    <body>
    <div id=idDIV>请您从下面的选择框中选择这段文字的相应属性的值。看一看会发生什么,然后您就会明白他们的意义。</div>
    <br>
    <table><tr><td>
    <select id="idSel1" onchange="rdl_change();">
    <option value="hidden">---overflow---
    <option value="hidden">hidden
    <option value="visible">visible
    </select>
    </td><td>
    <select id="idSel2" onchange="rdl_change();">
    <option value="clip">---text-overflow---
    <option value="clip">clip
    <option value="ellipsis" selected>ellipsis
    </select>
    </td></tr></table>
    <div id=idCodeDiv>overflow : ellipsis ;<br>text-overflow : hidden ;</div><br>&nbsp;<br>&nbsp;<br>
    <div id=id_div3>苏昱作品·版权所有<br><span id=id_span3>&copy;2002 Rainer Su . All rights reserved .</span></div>
    </body>
    </html>
      

  5.   

    Re:弹出错误“SubstringLength”方法没有采用“1”个参数的重载
    因为绑定的是一个字段,函数中的参数为三个.....//*****将下面的函数写在后台,然后在前台调用即可./// <summary>
        /// 字符拆分处理
        /// </summary>
        /// <param name="strTemp">数据源</param>
        /// <param name="intLen">拆取的长度</param>
        /// <param name="AddPoint">是否加在后面加"...",为true则添加,否则不添加</param>
        /// <returns></returns>
        public string SubstringLength(string strTemp,int intLen, bool AddPoint)
        {
            string strApp = "";        int strLength = System.Text.Encoding.Default.GetBytes(strTemp).Length;        if (strLength > intLen)
            {
                strApp = strTemp.Substring(0, intLen);
                if (AddPoint == true) strApp += "...";
            }
            else
            {
                strApp = strTemp.ToString();
            }        //****返回重新构造的字符
            return strApp;
        }
    //****例子 <asp:Repeater ID="RepList" runat="server">
                    <ItemTemplate>
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td>
                                    企业名称:</td>
                                <td>
                                    <!--调用后台函数 -->
                                    <%#SubstringLength(DataBinder.Eval(Container.DataItem, "CopationName").ToString(),4,true)%>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <asp:Repeater ID="ChileReption" runat="server" DataSource='<%#((DataRowView)Container.DataItem).Row.GetChildRows("myparent")%>'>
                                        <ItemTemplate>
                                            <table border="0" cellpadding="0" cellspacing="0">
                                                <tr>
                                                    <td>
                                                        产口名称:</td>
                                                    <td>
                                                        <%#DataBinder.Eval(Container.DataItem,"[\"ProductName\"]")%>
                                                    </td>
                                                    <td>
                                                        单价:</td>
                                                    <td>
                                                        <%#DataBinder.Eval(Container.DataItem,"[\"Proice\"]","{0:c}")%>
                                                    </td>
                                                </tr>
                                            </table>
                                        </ItemTemplate>
                                    </asp:Repeater>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:Repeater>