posts - 7,  comments - 3,  trackbacks - 0

1. 绑定方法

 公共类(C#)

 1public class Common
 2{
 3    public Common()
 4    {
 5        //
 6        // TODO: 在此处添加构造函数逻辑
 7        //
 8    }

 9
10    //根据要求返回特定长度的字符串
11    public string GetStringByLength(string Str, int Length)
12    {
13        if (Str.Length > Length)
14            return Str.Substring(0, Length);
15        else if (Str.Length > 0)
16            return Str;
17        else
18            return "";
19    }

20
21    //根据要求返回精简的文本
22    public string GetShowText(string Text,int MaxLength)
23    {
24        if (Text.Length < MaxLength)
25            return Text;
26        else
27            return GetStringByLength(Text, MaxLength - 2+ "";
28    }

29
30
31    //判断日期是否是新的
32    public bool IsNew(DateTime Date)
33    {
34        TimeSpan days = DateTime.Now.Subtract(Date);
35
36        int newDays = Int32.Parse(ConfigurationManager.AppSettings["NewDays"].ToString());
37
38        if (days.Days < newDays)
39            return true;
40        else
41            return false;
42    }

.aspx
 1<asp:GridView ID="gvMeiTiNews" runat="server">
 2<asp:TemplateField>
 3                <ItemStyle HorizontalAlign="Left" />
 4                <ItemTemplate>
 5                    <asp:HyperLink ID="hlnkNewsTitle" runat="server" NavigateUrl='<%# "ReadNews.aspx?NewsID=" + Eval("NewID") %>'
 6                        Text='<%# (new Common()).GetShowText(Eval("Title").ToString(),23) %>' ToolTip='<%# Eval("Title") %>'></asp:HyperLink>
 7                    <asp:Image ID="imgNew" runat="server" ImageUrl="~/theme/images/new.gif"  Visible='<%# (new Common()).IsNew(DateTime.Parse(Eval("EditDate").ToString())) %>' />
 8                    <asp:Image ID="imgHot" runat="server"  ImageUrl="~/theme/images/hot.gif" Visible='<%# (new Common()).IsHot(Int32.Parse(Eval("Clicks").ToString())) %>' />
 9                </ItemTemplate>
10            </asp:TemplateField>
11
12</asp:GridView>


2. 使用<% %>块

公共类(C#)
 1public class Common
 2{
 3    public Common()
 4    {
 5        //
 6        // TODO: 在此处添加构造函数逻辑
 7        //
 8    }

 9    //检查Count是否为0
10    public string CheckIteams(int Count)
11    {
12        if (Count <= 0)
13            return "<span>暂无</span>";
14        else
15            return string.Empty;
16    }

17}

18

.aspx
1<%  =(new Common()).CheckIteams(gvClassNewsList.Rows.Count) %>
posted on 2008-07-13 10:46 reddyone 阅读(797) 评论(0)  编辑 收藏 引用 所属分类: .net
只有注册用户登录后才能发表评论。