New Features of ASP. NET web forms 4.5 (3): model binding

Source: Internet
Author: User

First twoArticleWe have learned four new features of ASP. NET web forms 4.5: strong data control and bundling, HTML5 update and unobtrusive validation.

When introducing a strong-type control, I just briefly introduced its itemtype attribute. You can set a strong-type value for binding. If you only introduce this attribute, it is a bit like a syntactic sugar, it has no practical significance.

In fact, many garden friends also discovered that many MVC features are introduced in ASP. NET web forms 4.5, while model binding is more like a feature of MVC.

Model binding

If you have used the objectdatasource control, you must be impressed with its selectmethod. in ASP. NET web forms 4.5, Microsoft directly moved this method to a strongly typed control.

Replace the previous databind method with a more convenient selectmethod method. For more information, see here.

The selectmethod method accepts ienumerable or iqueryable objects. If you use the itemtype attribute described earlier, you must accept ienumerable or iqueryable objects, t is consistent with the previous itemtype.

Set a gridview configuration as follows.

 

1 < Form ID = "Form1" Runat = "Server" >
2 < Div >
3 < ASP: gridview ID = "Gridview_users" Runat = "Server" Itemtype = "Modelbinding. Users"
4 Datakeynames = "Userid" Selectmethod = "Getusers"
5 Autogeneratecolumns = "False" >
6 < Columns >
7 < ASP: boundfield Datafield = "Userid" Headertext = "User ID"   />
8 < ASP: boundfield Datafield = "Username" Headertext = "User Name"   />
9 < ASP: boundfield Datafield = "Useremail" Headertext = "Email"   />
10 </ Columns >
11 </ ASP: gridview >
12 < ASP: validationsummary ID = "Validationsummary_useredit" Runat = "Server" Showmodelstateerrors = "True"   />
13 </ Div >
14 </ Form >

Bind data to the background.

1 Public Iqueryable <users> getusers ()
2 {
3 List <users> List = New List <users> ();
4 List. Add ( New Users () {userid = 1 , Username = " Parry " , Useremail = " Parry@cnblogs.com " });
5 List. Add ( New Users () {userid = 2 , Username = " Spiderman " , Useremail = " Spiderman@cnblogs.com " });
6 List. Add ( New Users () {userid = 3 , Username = " Superman " , Useremail = " PaSupermanrry@cnblogs.com " });
7 List. Add ( New Users () {userid = 4 , Username = " Batman " , Useremail = " Batman@cnblogs.com " });
8 Return List. asqueryable <users> ();
9 }

The page is displayed.

Of course, the control also supports updatemethod and deletemethod for modification and deletion respectively.

Note that parameter attributes can also be defined in the selectmethod defined in the background.

Public Iqueryable <users> getusers ([control] Int ? Userid)

It also supports form, querystring, cookies, and URLs.

The overall perception is that ASP. NET web forms 4.5 has changed a lot and is more like MVC. Microsoft concatenates EF, strong-type controls, and model binding, and defines a regular method and process for backend binding.

For more details, refer to scottgu's series of articles: web forms model binding.

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.