开发者

extension method in gridview eval

开发者 https://www.devze.com 2023-02-05 00:43 出处:网络
I have an extension method that I wrote but when I use it on an aspx page to render a label inside a gridview, it doesn\'t work.

I have an extension method that I wrote but when I use it on an aspx page to render a label inside a gridview, it doesn't work.

<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("TheStatus").ToMyStatus() %>'></asp:Label>

The extension method is a file called Appfunctions.cs:

public static class Extensions
{
    public static string ToMyStatus(byte TheStatus)
    {
        //mycode
    }
}

I've seen posts that say a namespace needs to be added with the Imports statement in the aspx page but the e开发者_开发百科xtention ToMyStatus is not in any particular namespace.

Any suggestions much appreciated.

Thanks.


You have missed this and type conversion.

public static class Extensions
{
    public static string ToMyStatus(this byte TheStatus)
    {
        return "Hello : " + TheStatus;
    }
}

Markup

<asp:Label ID="lblStatus" 
           runat="server" 
           Text='<%#  ((byte)Eval("TheStatus")).ToMyStatus() %>'>
</asp:Label>
0

精彩评论

暂无评论...
验证码 换一张
取 消