今天做主界面,突然发现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
后台:
2

3

4

5

6

7

8



9

10

11

12

13

14

15

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16


17

18

19

20

21

22

23
