我的方案如下:
我有一个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'在当前上下文中不存在。
这不是调用方法的正确方法吗?如果不是我怎么称呼它? 感谢从现在开始,寻找答案。
答案 0 :(得分:2)
尝试将此方法更改为静态方法
public static string MatchesCount(int day) {...}
然后致电
'<%# MatchesGridViewRow.MatchesCount((Int32)1)%>'
请检查同一问题
Accessing public static class files from .ASPX file using Eval("") from gridView
答案 1 :(得分: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();
}