<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Microsoft.aspx.cs" Inherits="EntitySpaces.DataSource.Demo.Microsoft" %><%@ Register Assembly="EntitySpaces.Web" Namespace="EntitySpaces.Web" TagPrefix="cc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>EsDataSource Two Way Binding Sample</title></head><body> <form id="form1" runat="server"> <div> <table> <tr> <td valign="top" style="height: 514px"> <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" AutoGenerateSelectButton="True" DataKeyNames="EmployeeID" DataSourceID="EsDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AllowPaging="True" PageSize="5"> <Columns> <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" /> <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" /> <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" /> <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> <asp:BoundField DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy" SortExpression="TitleOfCourtesy" /> <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" SortExpression="BirthDate" /> <asp:BoundField DataField="HireDate" HeaderText="HireDate" SortExpression="HireDate" /> </Columns> </asp:GridView> </td> <td valign="top" style="height: 514px"> <asp:FormView ID="FormView1" runat="server" DataKeyNames="EmployeeID" DataSourceID="EsDataSource2" EmptyDataText="Select an employee in the grid to view their details" OnItemDeleted="FormView1_ItemDeleted" OnItemInserted="FormView1_ItemInserted" OnItemUpdated="FormView1_ItemUpdated" DefaultMode="Insert"> <EditItemTemplate> EmployeeID: <asp:Label ID="EmployeeIDLabel1" runat="server" Text='<%# Eval("EmployeeID") %>'> </asp:Label><br /> LastName: <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# Bind("LastName") %>'> </asp:TextBox><br /> FirstName: <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstName") %>'> </asp:TextBox><br /> Title: <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>'> </asp:TextBox><br /> TitleOfCourtesy: <asp:TextBox ID="TitleOfCourtesyTextBox" runat="server" Text='<%# Bind("TitleOfCourtesy") %>'> </asp:TextBox><br /> BirthDate: <asp:TextBox ID="BirthDateTextBox" runat="server" Text='<%# Bind("BirthDate") %>'> </asp:TextBox><br /> HireDate: <asp:TextBox ID="HireDateTextBox" runat="server" Text='<%# Bind("HireDate") %>'> </asp:TextBox><br /> Address: <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'> </asp:TextBox><br /> City: <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>'> </asp:TextBox><br /> Region: <asp:TextBox ID="RegionTextBox" runat="server" Text='<%# Bind("Region") %>'> </asp:TextBox><br /> PostalCode: <asp:TextBox ID="PostalCodeTextBox" runat="server" Text='<%# Bind("PostalCode") %>'> </asp:TextBox><br /> Country: <asp:TextBox ID="CountryTextBox" runat="server" Text='<%# Bind("Country") %>'> </asp:TextBox><br /> HomePhone: <asp:TextBox ID="HomePhoneTextBox" runat="server" Text='<%# Bind("HomePhone") %>'> </asp:TextBox><br /> Extension: <asp:TextBox ID="ExtensionTextBox" runat="server" Text='<%# Bind("Extension") %>'> </asp:TextBox><br /> Photo: <asp:TextBox ID="PhotoTextBox" runat="server" Text='<%# Bind("Photo") %>'> </asp:TextBox><br /> Notes: <asp:TextBox ID="NotesTextBox" runat="server" Text='<%# Bind("Notes") %>'> </asp:TextBox><br /> ReportsTo: <asp:TextBox ID="ReportsToTextBox" runat="server" Text='<%# Bind("ReportsTo") %>'> </asp:TextBox><br /> PhotoPath: <asp:TextBox ID="PhotoPathTextBox" runat="server" Text='<%# Bind("PhotoPath") %>'> </asp:TextBox><br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update"> </asp:LinkButton> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"> </asp:LinkButton> </EditItemTemplate> <InsertItemTemplate> LastName: <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# Bind("LastName") %>'> </asp:TextBox><br /> FirstName: <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstName") %>'> </asp:TextBox><br /> Title: <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>'> </asp:TextBox><br /> TitleOfCourtesy: <asp:TextBox ID="TitleOfCourtesyTextBox" runat="server" Text='<%# Bind("TitleOfCourtesy") %>'> </asp:TextBox><br /> BirthDate: <asp:TextBox ID="BirthDateTextBox" runat="server" Text='<%# Bind("BirthDate") %>'> </asp:TextBox><br /> HireDate: <asp:TextBox ID="HireDateTextBox" runat="server" Text='<%# Bind("HireDate") %>'> </asp:TextBox><br /> Address: <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'> </asp:TextBox><br /> City: <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>'> </asp:TextBox><br /> Region: <asp:TextBox ID="RegionTextBox" runat="server" Text='<%# Bind("Region") %>'> </asp:TextBox><br /> PostalCode: <asp:TextBox ID="PostalCodeTextBox" runat="server" Text='<%# Bind("PostalCode") %>'> </asp:TextBox><br /> Country: <asp:TextBox ID="CountryTextBox" runat="server" Text='<%# Bind("Country") %>'> </asp:TextBox><br /> HomePhone: <asp:TextBox ID="HomePhoneTextBox" runat="server" Text='<%# Bind("HomePhone") %>'> </asp:TextBox><br /> Extension: <asp:TextBox ID="ExtensionTextBox" runat="server" Text='<%# Bind("Extension") %>'> </asp:TextBox><br /> Photo: <asp:TextBox ID="PhotoTextBox" runat="server" Text='<%# Bind("Photo") %>'> </asp:TextBox><br /> Notes: <asp:TextBox ID="NotesTextBox" runat="server" Text='<%# Bind("Notes") %>'> </asp:TextBox><br /> ReportsTo: <asp:TextBox ID="ReportsToTextBox" runat="server" Text='<%# Bind("ReportsTo") %>'> </asp:TextBox><br /> PhotoPath: <asp:TextBox ID="PhotoPathTextBox" runat="server" Text='<%# Bind("PhotoPath") %>'> </asp:TextBox><br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert"> </asp:LinkButton> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"> </asp:LinkButton> </InsertItemTemplate> <ItemTemplate> EmployeeID: <asp:Label ID="EmployeeIDLabel" runat="server" Text='<%# Eval("EmployeeID") %>'> </asp:Label><br /> LastName: <asp:Label ID="LastNameLabel" runat="server" Text='<%# Bind("LastName") %>'></asp:Label><br /> FirstName: <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label><br /> Title: <asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>'></asp:Label><br /> TitleOfCourtesy: <asp:Label ID="TitleOfCourtesyLabel" runat="server" Text='<%# Bind("TitleOfCourtesy") %>'> </asp:Label><br /> BirthDate: <asp:Label ID="BirthDateLabel" runat="server" Text='<%# Bind("BirthDate") %>'></asp:Label><br /> HireDate: <asp:Label ID="HireDateLabel" runat="server" Text='<%# Bind("HireDate") %>'></asp:Label><br /> Address: <asp:Label ID="AddressLabel" runat="server" Text='<%# Bind("Address") %>'></asp:Label><br /> City: <asp:Label ID="CityLabel" runat="server" Text='<%# Bind("City") %>'></asp:Label><br /> Region: <asp:Label ID="RegionLabel" runat="server" Text='<%# Bind("Region") %>'></asp:Label><br /> PostalCode: <asp:Label ID="PostalCodeLabel" runat="server" Text='<%# Bind("PostalCode") %>'> </asp:Label><br /> Country: <asp:Label ID="CountryLabel" runat="server" Text='<%# Bind("Country") %>'></asp:Label><br /> HomePhone: <asp:Label ID="HomePhoneLabel" runat="server" Text='<%# Bind("HomePhone") %>'></asp:Label><br /> Extension: <asp:Label ID="ExtensionLabel" runat="server" Text='<%# Bind("Extension") %>'></asp:Label><br /> Photo: <asp:Label ID="PhotoLabel" runat="server" Text='<%# Bind("Photo") %>'></asp:Label><br /> Notes: <asp:Label ID="NotesLabel" runat="server" Text='<%# Bind("Notes") %>'></asp:Label><br /> ReportsTo: <asp:Label ID="ReportsToLabel" runat="server" Text='<%# Bind("ReportsTo") %>'></asp:Label><br /> PhotoPath: <asp:Label ID="PhotoPathLabel" runat="server" Text='<%# Bind("PhotoPath") %>'></asp:Label><br /> <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"> </asp:LinkButton> <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"> </asp:LinkButton> <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New"> </asp:LinkButton> </ItemTemplate> </asp:FormView> </td> </tr> </table> </div> <cc1:esDataSource ID="EsDataSource1" runat="server" OnesSelect="EsDataSource1_esSelect" AutoPaging="true" AutoSorting="true" /> <cc1:esDataSource ID="EsDataSource2" runat="server" OnesCreateEntity="EsDataSource2_esCreateEntity" OnesSelect="EsDataSource2_esSelect" /> </form></body></html>
using System;using System.Data;using System.Drawing;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using BusinessObjects;using EntitySpaces.Interfaces;namespace EntitySpaces.DataSource.Demo{ public partial class Microsoft : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GetEmployeeCount(); } } private void GetEmployeeCount() { Employees emp = new Employees(); emp.Query.es.CountAll = true; emp.Query.es.CountAllAlias = "Count"; if (emp.Query.Load()) { EsDataSource1.TotalRowCount = (int)emp.GetColumn("Count"); } GridView1.Sort(EmployeesMetadata.PropertyNames.LastName, SortDirection.Ascending); } protected void EsDataSource1_esSelect(object sender, EntitySpaces.Web.esDataSourceSelectEventArgs e) { EmployeesCollection employees = new EmployeesCollection(); e.Collection = employees; } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { if (EsDataSource2.State.ContainsKey("employeeid")) EsDataSource2.State["employeeid"] = GridView1.SelectedValue; else EsDataSource2.State.Add("employeeid", GridView1.SelectedValue); FormView1.DataBind(); FormView1.ChangeMode(FormViewMode.Edit); } protected void EsDataSource2_esSelect(object sender, EntitySpaces.Web.esDataSourceSelectEventArgs e) { EmployeesCollection employees = new EmployeesCollection(); if (EsDataSource2.State.ContainsKey("employeeid")) { employees.Query.Where(employees.Query.EmployeeID == (int)EsDataSource2.State["employeeid"]); employees.Query.Load(); e.Collection = employees; } else { e.Collection = employees; } } protected void EsDataSource2_esCreateEntity(object sender, EntitySpaces.Web.esDataSourceCreateEntityEventArgs e) { Employees employee = new Employees(); if (e.PrimaryKeys == null) { employee.AddNew(); } else { employee.LoadByPrimaryKey((int)e.PrimaryKeys[0]); } e.Entity = employee; } protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e) { GridView1.DataBind(); } protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e) { GridView1.DataBind(); } protected void FormView1_ItemDeleted(object sender, FormViewDeletedEventArgs e) { GridView1.DataBind(); } }}
Regards, Scott Schecter EntitySpaces | Blog | Twitter