2013-03-08 65 views
3

我的场景如下:ASP.NET GridView的TemplateField调用函数(:int)

我有一个GridView并做了一个类表示它的一行。 我期待在单元格中显示类的属性和函数的结果。

为此,我正在使用的列是这样的:

<asp:TemplateField HeaderText="1"> 
    <ItemTemplate> 
     <asp:Label runat="server" text='<%# MatchesCount((Int32)1) %>' /> 
    </ItemTemplate> 
</asp:TemplateField> 

类具有以下功能:

public class MatchesGridViewRow 
{ 
... 
    public string MatchesCount(int day) {...} 
} 

,我绑定到GridView这样的:

GridView.DataSource = GetGridViewData(DateTime.Now.Month); 
GridViewCalendar.DataBind();  

private List<MatchesGridViewRow> GetGridViewData(int month); 

我得到的错误是: CS0103:名称'MatchesCount'不存在于当前的区域吨。

这不是正确的方法来调用该方法吗?如果不是,我应该怎么称呼它? 从现在开始感谢,寻找答案。

回答

2

试试这个变革的方法静态方法

public static string MatchesCount(int day) {...} 

然后调用

'<%# MatchesGridViewRow.MatchesCount((Int32)1)%>' 

请检查同样的问题

Accessing public static class files from .ASPX file using Eval("") from gridView

+0

它不适用于我的情况,因为函数的结果取决于MatchesGridViewRow类的当前实例。 我可以使函数的数组属性。但是,如何在.asxp页面的数组中引用一个位置? – user2149152 2013-03-08 20:56:21

+0

我所做的功能: '公共字符串MatchesCount(INT天){...}' 到 '公共字符串列表 [] {获得;组; '的 '/> user2149152 2013-03-08 21:46:24

1

我检查下面的代码,它有用: 我希望

<asp:TemplateField HeaderText="type" ItemStyle-Width="200"> 
        <ItemTemplate> 
         <asp:Label ID="lbtype" runat="server" Text='<%# GetDescrptionHumanType(Convert.ToInt32(Eval("HumanType"))) %>' ></asp:Label> 
        </ItemTemplate> 
        <ItemStyle Width="200px"></ItemStyle> 
       </asp:TemplateField> 

    public static string GetDescrptionHumanType(int val) 
    { 
     return Utility.EnumEx.GetEnumDescription((HumanResourceType)val); 

    } 


    public static string GetEnumDescription(Enum value) 
    { 
     FieldInfo fi = value.GetType().GetField(value.ToString()); 

     DescriptionAttribute[] attributes = 
      (DescriptionAttribute[])fi.GetCustomAttributes(
      typeof(DescriptionAttribute), 
      false); 

     if (attributes != null && 
      attributes.Length > 0) 
      return attributes[0].Description; 
     else 
      return value.ToString(); 
    } 
相关问题