DataList綁定時控制字元串長度

來源:互聯網
上載者:User
今天做主介面,突然發現DataList綁定的字元是需要控制長度,暈倒的問題,後來整理了一下,問題解決了
前台: 1<asp:datalist id="pt_zx" runat="server" Width="100%" Height="80px">
 2<ItemTemplate>
 3<table width="100%" border="0" cellspacing="0" cellpadding="0">
 4<tr>
 5<td width="5%"><img src="Images/dec.jpg" width="7" height="8">
 6</td>
 7<td width="95%"><a href='news/news.aspx?id=<%# DataBinder.Eval(Container.DataItem,"id").ToString() %>' target="_blank">
 8<%# CutString1(DataBinder.Eval(Container.DataItem,"title").ToString(),12) %>
 9</a>
10</td>
11</tr>
12</table>
13</ItemTemplate>
14</asp:datalist>
15

後台: 1public string CutString1(string str,int length)
 2{
 3int i = 0, j = 0;
 4foreach(char chr in str)
 5{
 6if((int)chr > 127)
 7{
 8i += 2;
 9}
10else
11{
12i ++;
13}
14if (i > length)
15{
16str = str.Substring(0, j) + "";
17break;
18}
19j ++;
20}
21return str;
22
23}

謝謝“人在哈爾濱@net”
上面的英文和中文都是同樣處理的,這是最好不過的啦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.