Strong ASP. NET data binding

Source: Internet
Author: User

Data Binding seems to be an old thing in ASP. NET. But you know, you only need a small change to replace Eval, get rid of string dependencies and greatly improve performance.

First, add the following method to code behind:

protected virtual object ExpHelper<TEntity, TREsult>(Func<TEntity, TREsult> func){    var itm = GetDataItem();    return func((TEntity)itm);}

This code is the core secret. You can ignore what it is doing. In fact, each bound data item is intercepted and forced type conversion is performed.

Suppose we have defined the student class

public class Student{   public string Name { get; set; }   public int Age { get; set; }}

If you want to use a strong type of access to the student class on the page instead of using Eval, define a method to access the student

protected object Stu<TResult>(Func<Student, TResult> func){   return ExpHelper<Student, TResult>(func);}

So we can bind data in this way on the page.

<ul><asp:Repeater ID="rptStudents" runat="server"><ItemTemplate>   <li><%#Stu(_=>_.Name + "(" +_.Age+")")%></li></ItemTemplate></asp:Repeater></ul>

This has four advantages:

  1. Compile-time detection
  2. Enjoy Smart Tips
  3. Stronger type conversion performance than eval reflection
  4. The content on the page is richer. As shown above, we can freely splice the desired string, very similar to MVC.

What's more amazing is that it supports multi-layer nesting. For example, we define a set group class and accessors for students, and then we can use nested repeater to display group information. The complete procedure is as follows:

<% @ Page Language = "C #" autoeventwireup = "true" %> <SCRIPT runat = "server"> public class student {public string name {Get; set ;} public int age {Get; Set ;}} public class group {public ienumerable <student> students {Get; Set ;}} protected void page_load (Object sender, eventargs E) {// a group of students var students = new [] {New Student {name = "Mike", age = 23}, new student {name = "Jane ", age = 12}, new student {name = "Frank", age = 25}, new student {name = "Susan", age = 32},}; rptstudents. datasource = students; // var group0 = new group (); group0.students = students. take (2); var group1 = new group (); group1.students = students. skip (2 ). take (2); rptgroups. datasource = new [] {group0, group1}; databind ();} protected virtual object exphelper <tentity, tresult> (func <tentity, tresult> func) {var ITM = getdataitem (); R Eturn func (tentity) ITM);} // student accessors protected object Stu <tresult> (func <student, tresult> func) {return exphelper <student, tresult> (func);} // group accessor protected object GRP <tresult> (func <group, tresult> func) {return exphelper <group, tresult> (func );} </SCRIPT> <! Doctype HTML> <body> <% -- single layer -- %> <ul> <asp: repeater id = "rptstudents" runat = "server"> <itemtemplate> <li> <% # Stu (_ => _. name + "(" + _. age + ")") %> </LI> </itemtemplate> </ASP: repeater> </ul> <% -- nested -- %> <ul> <asp: repeater id = "rptgroups" runat = "server"> <itemtemplate> <li> <ol> <asp: repeater id = "repeater1" runat = "server" datasource = '<% # GRP (_ => _. students) %> '> <itemtemplate> <li> <% # Stu (_ => _. name + "(" + _. age + ")") %> </LI> </itemtemplate> </ASP: repeater> </OL> </LI> </itemtemplate> </ASP: repeater> </ul> </body> 

PS

This article is a small invention that I have not previously written. It is mainly because there are very few people who know this method. I hope you can help test the performance. If you think it is appropriate, you can apply it to your actual work.

 

Update:

Thanks to the suggestions of dacey Wayne's mean Dudu boss and others.
I have added the extension method version. I like the concept of expansion.

Now, you only need to add a static help class and name it as you like.

public static class Helper{   static object ExpHelper<TEntity, TResult>(Page page, Func<TEntity, TResult> func)   {      var itm = page.GetDataItem();      return func((TEntity)itm);   }   public static object Eval<T>(this Page page, Func<T, object> func)   {      return ExpHelper<T, object>(page, func);   }}

On the page, you can

<%#this.Eval<Student>(_ => _.Name + "(" + _.Age + ")")%>
  • Note that this is required
  • The extension method has good adhesion.
  • No need for a parent class to define a common method
  • The generic model provides multiple copies and is easy to see the type.
  • In addition, refactor is supported. You can use Ctrl + R to change the attribute name.
Related Article

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.