Strong ASP. NET data binding Ultimate Edition 2nd

Source: Internet
Author: User

There is a problem with the previous method:

Ivony... said: "It is uncomfortable to write an Eval method for each type ."

Kingthy said, "Will there be too many Func everywhere ?"

Li yongjing said: "The methods used anywhere in the project should be collected and written as extension methods in Core-level Dll ."

If you use the extension method to solve this problem, we will face new problems. See the extension method:

 public static class Binder
{
public static TResult Eval<TEntity, TResult>(this System.Web.UI.Page p,
Func<TEntity, TResult> func)
{
return func((TEntity)p.GetDataItem());
}
}

This problem occurs when calling, you need to specify the return type, for example:

 <%# this.Eval<NewsInfo, string>(n => n.Title) %>


You can use another method to solve this problem. The Code is as follows:

 public static class Binder<TEntity> where TEntity : class
{
public static TResult Eval<TResult>(System.Web.UI.Page p,
Func<TEntity, TResult> func)
{
return func((TEntity)p.GetDataItem());
}
}

The call code is as follows:

 <%# Binder<NewsInfo>.Eval(this, n => n.Title) %>


Is there a better solution? Currently, I can only find this method. I think the ideal call method should be:

 <%# Eval<NewsInfo>(n => n.Title)%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.