How do you use extension methods in web form data binding expressions?

Has anyone successfully used extension methods in data binding expressions?

Let's say I have an extension method called "GetName" attached to "MyClass".

In the code behind, I confirmed that this works:

MyClass myObject = new MyClass();   
MyClass.GetName();

However, in a web form, I try this:

<%@ Import Namespace="My.Namespace" %>

Then in the Repeater ItemTemplate:

<%# ((MyClass)Container.DataItem).GetName() %>

Visual Studio is cool, Intellisense agrees with everything, and the project is under construction. But when I run it, I get:

Compilation error
"My.Namespace.MyClass" does not contain a definition for "GetName"

Thus, the code-code will take the extension method, but not the web form. I suspect this is a problem with names, but I imported the same namespace in both places.

+3
2

aspx/ascx . , , . :

:

<%# String.Format("{0:C}", DataBinder.Eval("Foo")) %>

:

<%# String.Format("{0:C}", Bind("Foo")) %>

? , DataBinder.Eval , Bind . , , , binder/parser - . , DataBinder.Eval, , ASP.NET 1.1/1.0.

, , :

<%# Bind("Foo", "{0:C}") %>

, ,

: # . asp.net .

+3

MyClass, codebehind.

<%# GetObjectName((MyClass)Container.DataItem) %>

CodeBehind :

protected string GetObjectName(MyClass obJect)
    {
        return obJect.GetName();
    }

.

+3

Source: https://habr.com/ru/post/1716728/


All Articles