Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
Demo
Homework
imgs
README.md
index.html

README.md

ASP.NET Data Source Controls

Building Data-Driven ASP.NET Web Forms Apps

Table of Contents

  • ASP.NET Data Source Controls
    • SqlDataSource
    • EntityDataSource
    • ObjectDataSource
  • Accessing ADO.NET Entity Frameworkwith EntityDataSource
    • Working with Editable Controls
    • Master-Details Navigation
  • Model Binding in Web Forms

ASP.NET Data Source Controls

ASP.NET Data Source Controls

  • ASP.NET provides server controls that take care of data binding details
    • Known as data source controls
    • SqlDataSource, EntityDataSource, ObjectDataSource, XmlDataSource,
  • They are an abstraction over the data source
  • Data-bound server controls can be associated to a data source control
    • Through the DataSourceID property

ASP.NET Data Source Controls

  • ASP.NET data source controls connect the data-bound controls with the database / ORM:
  • Typical scenario:

  • SqlDataSource

    • SqlDataSource provides connection to a relational DB (SQL Server, Oracle, OLE DB, …)
    • Data is manipulated by using commands
      • Select, Update, Insert and Delete commands
      • Commands can be either SQL queries or names of stored procedures
    • Data is processed with a DataSet (by default)
      • The DataSourceMode property specifies whether to use DataSet or DataReader
    • Old-fasioned, better use EntityDataSource

    SqlDataSource – Example

    <asp:SqlDataSource ID="DSCategories" runat="server" 
      ConnectionString="<%$ ConnectionStrings:Northwind %>" 
      SelectCommand="SELECT x FROM [Categories]" />
    <asp:GridView ID="GridViewCategories" runat="server" 
      DataSourceID="DSCategories" DataKeyNames="CategoryID"
      AutoGenerateColumns="True" AllowPaging="True"
      AllowSorting="True">
    </asp:GridView>

    Demo

    EntityDataSource

    • The EntityDataSource
      • Provides data binding in Web applications that use the ADO.NET Entity Framework (EF)
      • Implements CRUD (create, read, update, and delete) operations
        • Against a database mapped through EF
        • On behalf of data-bound controls
      • Supports complex queries, sorting and paging
    • The Entity Data Model Designer in VS is used to create the EF mappings (.edmx)

    EntityDataSource – Example

    • Define the Entity Data Model (.edmx)
    • Put a list control in the form (e.g. ListBox)
    <asp:ListBox ID="ListBoxCustomers" runat="server">
    </asp:ListBox>

    EntityDataSource – Example (2)

    • Bind the ListBox to the data model
    • Select the new "Entity" option in the dialog box

    EntityDataSource – Example (3)

    • The VS Designer will then display the available Entity Containers

    EntityDataSource – Example (4)

    • ListBox control bound an EntityDataSource:
    <asp:EntityDataSource
      ID="EntityDataSourceCustomers" runat="server" 
      ConnectionString="name=NorthwindEntities" 
      DefaultContainerName="NorthwindEntities" 
      EntitySetName="Customers" />
    
    <asp:ListBox ID="ListBoxCustomers" runat="server"
      DataSourceID="EntityDataSourceCustomers" 
      DataTextField="CompanyName" Rows="10"
      DataValueField="CustomerID" />

    Demo

    Master-Detail Navigation

    • Master-details navigation could be implemented through filtering:
    <asp:EntityDataSource ID="DSCustomers" runat="server" 
      EntitySetName="Customers"  />
    <asp:ListBox ID="ListBoxCustomers" runat="server" 
      DataSourceID="EntityDataSourceCustomers"  />
    
    <asp:EntityDataSource ID="DSOrders" runat="server"
      EntitySetName="Orders" Where="it.CustomerID=@CustID">
        <WhereParameters>
          <asp:ControlParameter Name="CustID" Type="String"
            ControlID="ListBoxCustomers" />
        </WhereParameters>
    </asp:EntityDataSource>

    EntityDataSource: Hints

    • To access navigational properties in EntityDataSource use "Include"
    &#60;asp:EntityDataSource ID="DataSourceOrderDetails"
      ConnectionString="name=NorthwindEntities" 
      DefaultContainerName="NorthwindEntities" 
      EntitySetName="Order_Details" Include="Product"
      runat="server" … /&#62;
    • To use strongly-typed binding (ItemType) use EnableFlattening="false"
    &#60;asp:EntityDataSource ID="DataSourceOrderDetails"
     runat="server" EnableFlattening="false" … /&#62;

    Demo

    Editable EntityDataSource

    <asp:EntityDataSource ID="EntityDataSourceCustomers"
      runat="server" ConnectionString="name=…"
      DefaultContainerName="NorthwindEntities"
      EnableInsert="True" EnableUpdate="True"
      EnableDelete="True" EntitySetName="Customers">
    </asp:EntityDataSource>
    
    <asp:ListView ID="ListViewCustomers" runat="server"
      DataKeyNames="CustomerID"
      DataSourceID="EntityDataSourceCustomers">
        <LayoutTemplate>…</LayoutTemplate>
        <ItemTemplate>…</ItemTemplate>
        <EditTemplate>…</EditItemTemplate>
        <InsertTemplate>…</InsertItemTemplate>
    </asp:ListView>

    BindItem and Bind(…)

    • Editable controls require a two-way binding (data field ←→ UI control)
      • Use BindItem.Field for strongly-typed binding instead of Item.Field
      • Use Bind("PropertyPath") for standard binding instead of Eval("PropertyPath")
    <EditItemTemplate>
      Company Name: <asp:TextBox ID="TextBoxCompanyName" 
      runat="server" Text='<%# BindItem.CompanyName %>' />
      …
    </EditItemTemplate>

    Demo

    ObjectDataSource

    • ObjectDataSource enables data-binding of UI control to collection of objects
      • Instead of directly binding to a database
    • Needs a middle-tier business object class
      • A class providing the Select, Update, Insert, Delete methods (CRUD operations)
    • ObjectDataSource properties
      • TypeName – name of the business object class
      • SelectMethod, UpdateMethod,

    ObjectDataSource – Example

    <asp:ObjectDataSource ID="dsProducts"
      runat="server" TypeName="ObjectDataSourceProducts"
      SelectMethod="GetAll" InsertMethod="Insert"
      UpdateMethod="Update" DeleteMethod="Delete">
    </asp:ObjectDataSource>
    
    <asp:GridView ID="GridViewProducts" runat="server"
      DataSourceID="dsProducts" DataKeyNames="ProductID">
    </asp:GridView>
    public class ObjectDataSourceProducts
    {
      public IEnumerable<Product> GetAll() { … }
      public int UpdateProduct(Product p) { … }
      …
    }

    Building File System Explorerwith ObjectDataSource

    • Define a class to get all files from given folder
    • Add GetAllFiles() method to return a collection of FileInfo objects
    public class FileSystemManager
    {
      public static IEnumerable<FileInfo> 
        GetAllFiles(string folder)
      {
        var files = Directory.GetFiles(folder);
        return files;
      }
    }

    Building File System Explorerwith ObjectDataSource (2)

    • Add a ListView control in the form
    • Bind the ListView to the ObjectDataSource

    Building File System Explorerwith ObjectDataSource (3)

    • Next choose your custom class FilesManager
    • And choose the GetAllFiles(…) method as "SELECT Method"

    Building File System Explorerwith ObjectDataSource (4)

    • Click to configure the ListView control
    • Optionally choose layout and style

    Building File System Explorerwith ObjectDataSource (5)

    • Customize the ListView to show only rows you like
    • Add a parameterfor the SELECT method
    <SelectParameters>
      <asp:Parameter DefaultValue="C:\WINDOWS"
        Name="rootFolder" Type="String" />
    </SelectParameters>

    Building File System Explorerwith ObjectDataSource (6)

    • The result is:

    Demo

    Other Data Sources

    • LinqDataSource
      • For LINQ-to-SQL ORM mappings (legacy)
    • Hierarchical
      • XmlDataSource
        • Establishes a connection to an XML source of data (files, documents)
        • DataFile, TranformFile, XPath
      • SiteMapDataSource
    • MS Access – AccessDataSource
      • Derives from SqlDataSource

    Model Binding

    CRUD Operations Made Easy

    What is Model Binding?

    • What is "model"?
      • In data-driven applications "models" are classes that hold the data behind the UI controls
    • What is "model binding" in ASP.NET?
      • Bind data controls directly to methods that provide CRUD functionality
      • SelectMethod → returns IQueryable<T>
      • InsertMethod, UpdateMethod, DeleteMethod → edit the model (entity)

    Demo

    ASP.NET Data Source Controls

    • Questions?
    You can’t perform that action at this time.