ARTICLE

Select Data Using Model Binders in .NET 4.5

Posted by Vijay Prativadi Articles | .NET 4.5 October 07, 2012
Today, in this article let’s play around with one of the interesting and most useful concepts in .NET 4.5.
Reader Level:

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in .NET 4.5.

Question: What are model binders?

In simple terms "It is a data binding system which is used to perform some common operations based on the data retrieved from a model class with strongly typed invocations".

Step 1: Create a new "ASP.Net Web Forms Application", as in:

New-project-in-asp.net.jpg

Step 2: The complete code of webform1.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ModelBindersSelectApp.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <center>

        <div>

            <asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" ItemType="ModelBindersSelectApp.Employee"

                BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"

                AutoGenerateColumns="false" SelectMethod="GetData">

                <AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle>

                <FooterStyle BackColor="Tan"></FooterStyle>

                <HeaderStyle BackColor="Tan" Font-Bold="True"></HeaderStyle>

                <PagerStyle HorizontalAlign="Center" BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue">

                </PagerStyle>

                <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite"></SelectedRowStyle>

                <SortedAscendingCellStyle BackColor="#FAFAE7"></SortedAscendingCellStyle>

                <SortedAscendingHeaderStyle BackColor="#DAC09E"></SortedAscendingHeaderStyle>

                <SortedDescendingCellStyle BackColor="#E1DB9C"></SortedDescendingCellStyle>

                <SortedDescendingHeaderStyle BackColor="#C2A47B"></SortedDescendingHeaderStyle>

                <Columns>

                    <asp:TemplateField HeaderText="Id">

                        <ItemTemplate>

                            <asp:Label ID="lblId" runat="server" Text='<%# Item.Id %>'></asp:Label></ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="First Name">

                        <ItemTemplate>

                            <asp:Label ID="lblFirstName" runat="server" Text='<%# Item.FirstName %>'></asp:Label></ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Last Name">

                        <ItemTemplate>

                            <asp:Label ID="lblLastName" runat="server" Text='<%# Item.LastName %>'></asp:Label></ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Age">

                        <ItemTemplate>

                            <asp:Label ID="lblAge" runat="server" Text='<%# Item.Age %>'></asp:Label></ItemTemplate>

                    </asp:TemplateField>

                </Columns>

            </asp:GridView>

        </div>

    </center>

    </form>

</body>

</html>

Step 3: The complete code of webform1.aspx.cs looks like this:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace ModelBindersSelectApp

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        public IQueryable GetData()

        {return objEntities.Employee;

        }

        #region Instance VariablesCompanyEntities objEntities = new CompanyEntities();

        #endregion

    }

}

Step 4: The output of the application looks like this:


Output-of-the-application-in-asp.net.jpg

Login to add your contents and source code to this article
post comment
     

What is ItemType property. Is it related to Model Binders?

Posted by Gaurav Gupta Oct 10, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter