如何在数据绑定控件使用eval调用扩展方法 [英] How to call extension methods using Eval in a databound control
问题描述
我对int类型的简单扩展方法,所以我可以做到以下几点:
I have a simple extension method on the int type so I can do the following:
string timeLength = 61.ToTime() // timeLength will be "1:01"
这工作在code很棒,但我想在一个中继器模板使用这个扩展方法。绑定时我要做到以下几点:
This works great in code, but I want to use this extension method in a Repeater Template. When databinding I want to do the following:
<%# Eval("LengthInSeconds").ToTime() %>
这没有工作,所以我尝试:
That didn't work so I tried:
<%# ((int) Eval("LengthInSeconds")).ToTime() %>
和它仍然没有工作。 JIT编译器是不是看到我的扩展方法,我确实有在页面中正确导入语句。
and it still didn't work. The JIT compiler is not seeing my extension method and I do have the proper import statement in the page.
我对解决这个唯一的想法就是用文字控制取代评估和演示,并呼吁在code-后面的扩展方法,但无论哪种方式,我还是想知道为什么这是行不通的。
My only idea for solving this is to replace the Eval with a Literal control and call the extension method in the code-behind, but either way, I would still like to know why this isn't working.
感谢
推荐答案
看起来像我可以回答我的问题! Asp.Net被编译的.aspx,使用.net 2.0编译器的.ascx模板。我需要以下添加到我的web.config,使其工作
Looks like I get to answer my own question! Asp.Net was compiling the .aspx,.ascx templates using the .Net 2.0 compiler. I needed to add the following to my web.config to make it work
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
我还是要执行的评估和演示投,与(int),但至少对我来说很有意义。
I still have to perform the cast to (int) in the Eval, but that at least makes sense to me.
这篇关于如何在数据绑定控件使用eval调用扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!