Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET GridView TemplateField Call function(:int)

My scenario is the following:

I have a GridView and made a class the represents a row in it. I was expecting to show the properties and function's result of the class in the cells.

For this i'm using columns like this:

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

the class has the following function:

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

and I bind to the GridView like this:

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

private List<MatchesGridViewRow> GetGridViewData(int month);

The error that I get is: CS0103: The name 'MatchesCount' doesnt exist in current context.

Isnt this the right way to call the method? If not how should i call it? Thanks from now, looking forware to answers.

like image 254
user2149152 Avatar asked Dec 07 '25 19:12

user2149152


2 Answers

Try this for change the method to static method

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

then call

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

Please check this same issue

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

i check below code and it useful : i hope

 <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();
    }
like image 40
Ali Sarshogh Avatar answered Dec 09 '25 14:12

Ali Sarshogh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!