Saturday, April 12, 2014

my sophomoric DevExpress effort

Building on my first effort, I have refactored the markup of default.aspx as defined here to be as such:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master"
      AutoEventWireup="true" CodeBehind="Default.aspx.cs"
      Inherits="DummyDevExpressApplication._Default" %>
<%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.9.0, Culture=neutral,
      PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxEditors"
      TagPrefix="dx" %>
<%@ Register TagPrefix="dx" Namespace="DevExpress.Web.ASPxGridView"
      Assembly="DevExpress.Web.v13.1, Version=13.1.9.0, Culture=neutral,
      PublicKeyToken=b88d1754d700e49a" %>
<%@ Register TagPrefix="dx" Namespace="DevExpress.Web.ASPxTabControl"
      Assembly="DevExpress.Web.v13.1, Version=13.1.9.0, Culture=neutral,
      PublicKeyToken=b88d1754d700e49a" %>
<%@ Register TagPrefix="dx" Namespace="DevExpress.Web.ASPxClasses"
      Assembly="DevExpress.Web.v13.1, Version=13.1.9.0, Culture=neutral,
      PublicKeyToken=b88d1754d700e49a" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
   <dx:ASPxGridView ID="SolarSystem" ClientInstanceName="SolarSystem"
         runat="server" Width="100%" KeyFieldName="Name"
         AutoGenerateColumns="False" >
      <SettingsBehavior AutoExpandAllGroups="True" />
      <SettingsPager PageSize="15">
      </SettingsPager>
      <Settings ShowFilterRow="True" ShowGroupPanel="True" />
      <Styles AlternatingRow-Enabled="True">
         <AlternatingRow Enabled="True">
         </AlternatingRow>
      </Styles>
      <Columns>
         <dx:GridViewCommandColumn VisibleIndex="0" ButtonType="Link"
               Caption="Edit">
            <EditButton Visible="True">
            </EditButton>
            <DeleteButton Visible="True">
            </DeleteButton>
            <HeaderCaptionTemplate>
               <dx:ASPxHyperLink ID="btnNew" runat="server" Text="New">
                  <ClientSideEvents Click="function (s, e) { SolarSystem.AddNewRow();}" />
               </dx:ASPxHyperLink>
            </HeaderCaptionTemplate>
         </dx:GridViewCommandColumn>
         <dx:GridViewDataTextColumn FieldName="Name" VisibleIndex="1">
         </dx:GridViewDataTextColumn>
         <dx:GridViewDataTextColumn
               FieldName="ClosestAstronomicalUnitDistanceFromSun" VisibleIndex="2">
         </dx:GridViewDataTextColumn>
         <dx:GridViewDataComboBoxColumn FieldName="PlanetType" VisibleIndex="3">
            <PropertiesComboBox DataSourceID="PlanetTypeSource" ValueField="key"
                  TextField="value" ValueType="System.String"
                  DropDownStyle="DropDown" />
         </dx:GridViewDataComboBoxColumn>
         <dx:GridViewDataTextColumn FieldName="MilesInDiameter" VisibleIndex="4">
         </dx:GridViewDataTextColumn>
         <dx:GridViewDataColumn FieldName="DiscoveryDate" VisibleIndex="5">
         </dx:GridViewDataColumn>
      </Columns>
      <SettingsPopup>
         <EditForm Width="500" />
      </SettingsPopup>
      <SettingsPager Mode="ShowPager" PageSize="10" />
      <Settings ShowTitlePanel="true" />
      <SettingsText Title="Popup Edit Form Editing" />
      <Templates>
         <EditForm>
            <div style="padding: 4px 4px 3px 4px">
               <dx:ASPxPageControl runat="server" ID="PageControl" Width="100%">
                  <TabPages>
                     <dx:TabPage Text="Planet" Visible="true">
                        <ContentCollection>
                           <dx:ContentControl runat="server">
                              <dx:ASPxGridViewTemplateReplacement ID="Editors"
                                    ReplacementType="EditFormEditors" runat="server">
                              </dx:ASPxGridViewTemplateReplacement>
                           </dx:ContentControl>
                        </ContentCollection>
                     </dx:TabPage>
                  </TabPages>
               </dx:ASPxPageControl>
            </div>
            <div style="text-align: right; padding: 2px 2px 2px 2px">
               <dx:ASPxGridViewTemplateReplacement ID="UpdateButton"
                     ReplacementType="EditFormUpdateButton" runat="server">
               </dx:ASPxGridViewTemplateReplacement>
               <dx:ASPxGridViewTemplateReplacement ID="CancelButton"
                     ReplacementType="EditFormCancelButton" runat="server">
               </dx:ASPxGridViewTemplateReplacement>
            </div>
         </EditForm>
      </Templates>
   </dx:ASPxGridView>
   <asp:ObjectDataSource ID="PlanetTypeSource" runat="server"
         SelectMethod="SelectPlanetTypes"
         TypeName="DummyDevExpressApplication.Utilities.CommonSource">
   </asp:ObjectDataSource>
</asp:Content>

 
 

Things now look a smidge different:

 
 

My code behind has become...

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.Web.UI;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxGridView.Rendering;
using DevExpress.Web.ASPxTabControl;
using DevExpress.Web.Data;
using DummyDevExpressApplication.Models;
using DummyDevExpressApplication.Utilities;
using System.Linq;
namespace DummyDevExpressApplication
{
   public partial class _Default : Page
   {
      private List<Planet> planets;
      
      protected void Page_Load(object sender, EventArgs e)
      {
         List<Planet> nullCheck = Session["planets"] as List<Planet>;
         if (nullCheck == null)
         {
            planets = PlanetFactory.GetPlanets();
            Session["planets"] = planets;
         }
         planets = Session["planets"] as List<Planet>;
         SetSolarSystem();
      }
      
      private void SetSolarSystem()
      {
         SolarSystem.HtmlDataCellPrepared += SolarSystem_HtmlDataCellPrepared;
         SolarSystem.HtmlEditFormCreated += SolarSystem_HtmlEditFormCreated;
         SolarSystem.RowDeleting += SolarSystem_RowDeleting;
         SolarSystem.RowInserting += SolarSystem_RowInserting;
         SolarSystem.RowUpdating += SolarSystem_RowUpdating;
         SolarSystem.RowValidating += SolarSystem_RowValidating;
         SolarSystem.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
         SolarSystem.DataSource = Session["planets"];
         SolarSystem.DataBind();
      }
      
      private void SolarSystem_HtmlDataCellPrepared(object sender,
            ASPxGridViewTableDataCellEventArgs e)
      {
         if (e.DataColumn.FieldName == "DiscoveryDate" && e.CellValue == null)
         {
            e.Cell.Text = "prehistory";
            e.Cell.ForeColor = Color.Firebrick;
            e.Cell.Font.Size = 8;
         }
      }
      
      private void SolarSystem_HtmlEditFormCreated(object sender,
            ASPxGridViewEditFormEventArgs e)
      {
         ASPxPageControl outerWrapper =
               (ASPxPageControl)SolarSystem.FindEditFormTemplateControl("PageControl");
         GridViewEditFormTable innerWrapper =
               (GridViewEditFormTable)outerWrapper.ActiveTabPage.FindControl("DXEFT");
         LiteralControl literal =
               (LiteralControl)innerWrapper.Rows[0].Cells[2].Controls[0].Controls[0];
         literal.Text = "AU Distance";
         ASPxTextBox firstTextBox =
               (ASPxTextBox)outerWrapper.ActiveTabPage.FindControl("DXEditor1");
         outerWrapper.ActiveTabPage.Text = firstTextBox.Text;
      }
      
      private void SolarSystem_RowDeleting(object sender, ASPxDataDeletingEventArgs e)
      {
         UseRevampedPlanets(RevampPlanets(e.Values, new OrderedDictionary()));
         e.Cancel = true;
      }
      
      private void SolarSystem_RowInserting(object sender,
            ASPxDataInsertingEventArgs e)
      {
         UseRevampedPlanets(RevampPlanets(new OrderedDictionary(), e.NewValues));
         e.Cancel = true;
      }
      
      private void SolarSystem_RowUpdating(object sender,
            ASPxDataUpdatingEventArgs e)
      {
         UseRevampedPlanets(RevampPlanets(e.OldValues, e.NewValues));
         e.Cancel = true;
      }
      
      private void SolarSystem_RowValidating(object sender,
            ASPxDataValidationEventArgs e)
      {
         List<Planet> revamp = RevampPlanets(e.OldValues, e.NewValues);
         if (IsNamingCollision(revamp)) e.RowError = "You may not have a duplicate name.";
      }
      
      private bool IsNamingCollision(List<Planet> revamp)
      {
         return revamp.Select(x => revamp.Count(y => x.Name == y.Name))
               .Any(z => z > 1);
      }
      
      private Planet GiveNewPlanet(OrderedDictionary newValues)
      {
         return new Planet()
         {
            Name = newValues[0] as string,
            ClosestAstronomicalUnitDistanceFromSun = Convert.ToDecimal(newValues[1]),
            PlanetType = (PlanetType)Convert.ToInt32(newValues[2]),
            MilesInDiameter = Convert.ToInt32(newValues[3]),
            DiscoveryDate = newValues[4] as DateTime?
         };
      }
      
      private List<Planet> RevampPlanets(OrderedDictionary oldValues,
            OrderedDictionary newValues)
      {
         List<Planet> revamp = new List<Planet>();
         if (oldValues.Count == 0) revamp.Add(GiveNewPlanet(newValues));
         foreach (Planet planet in planets)
         {
            if (oldValues.Count != 0 && planet.Name == oldValues[0] as string)
            {
               if (newValues.Count != 0) revamp.Add(GiveNewPlanet(newValues));
            } else {
               revamp.Add(planet);
            }
         }
         return revamp;
      }
      
      private void UseRevampedPlanets(List<Planet> revamp)
      {
         if (!IsNamingCollision(revamp)) planets = revamp;
         Session["planets"] = planets;
         SolarSystem.DataSource = Session["planets"];
         SolarSystem.DataBind();
         SolarSystem.CancelEdit();
      }
   }
}

 
 

I've made the list of planets a little longer too in the name of experimenting with pagination. I have included four dwarf planets I was not savvy to. The SettingsPager XML node in the markup breaks the content up into pages of ten. The three dwarf planets discovered since the turn of the millennium thus end up on a second page.

using System;
using System.Collections.Generic;
using DummyDevExpressApplication.Models;
namespace DummyDevExpressApplication.Utilities
{
   public static class PlanetFactory
   {
      public static List<Planet> GetPlanets()
      {
         return new List<Planet>()
         {
            new Planet()
            {
               Name = "Mercury",
               ClosestAstronomicalUnitDistanceFromSun = 0.307m,
               PlanetType = PlanetType.Inner,
               MilesInDiameter = 3032
            },
            new Planet()
            {
               Name = "Venus",
               ClosestAstronomicalUnitDistanceFromSun = 0.718m,
               PlanetType = PlanetType.Inner,
               MilesInDiameter = 7521
            },
            new Planet()
            {
               Name = "Earth",
               ClosestAstronomicalUnitDistanceFromSun = 0.98m,
               PlanetType = PlanetType.Inner,
               MilesInDiameter = 7926
            },
            new Planet()
            {
               Name = "Mars",
               ClosestAstronomicalUnitDistanceFromSun = 1.38m,
               PlanetType = PlanetType.Inner,
               MilesInDiameter = 4222
            },
            new Planet()
            {
               Name = "Ceres",
               ClosestAstronomicalUnitDistanceFromSun = 2.547m,
               PlanetType = PlanetType.Dwarf,
               MilesInDiameter = 590,
               DiscoveryDate = new DateTime(1801,1,1)
            },
            new Planet()
            {
               Name = "Jupiter",
               ClosestAstronomicalUnitDistanceFromSun = 4.95m,
               PlanetType = PlanetType.Gas,
               MilesInDiameter = 88846
            },
            new Planet()
            {
               Name = "Saturn",
               ClosestAstronomicalUnitDistanceFromSun = 9.05m,
               PlanetType = PlanetType.Gas,
               MilesInDiameter = 74898
            },
            new Planet()
            {
               Name = "Uranus",
               ClosestAstronomicalUnitDistanceFromSun = 18.4m,
               PlanetType = PlanetType.Gas,
               MilesInDiameter = 31763,
               DiscoveryDate = new DateTime(1781,3,13)
            },
            new Planet()
            {
               Name = "Neptune",
               ClosestAstronomicalUnitDistanceFromSun = 29.8m,
               PlanetType = PlanetType.Gas,
               MilesInDiameter = 30778,
               DiscoveryDate = new DateTime(1846,9,23)
            },
            new Planet()
            {
               Name = "Pluto",
               ClosestAstronomicalUnitDistanceFromSun = 29.7m,
               PlanetType = PlanetType.Dwarf,
               MilesInDiameter = 1430,
               DiscoveryDate = new DateTime(1930,2,18)
            },
            new Planet()
            {
               Name = "Haumea",
               ClosestAstronomicalUnitDistanceFromSun = 34.48m,
               PlanetType = PlanetType.Dwarf,
               MilesInDiameter = 217,
               DiscoveryDate = new DateTime(2003,3,7)
            },
            new Planet()
            {
               Name = "Makemake",
               ClosestAstronomicalUnitDistanceFromSun = 37.91m,
               PlanetType = PlanetType.Dwarf,
               MilesInDiameter = 882,
               DiscoveryDate = new DateTime(2005,3,31)
            },
            new Planet()
            {
               Name = "Eris",
               ClosestAstronomicalUnitDistanceFromSun = 38.54m,
               PlanetType = PlanetType.Dwarf,
               MilesInDiameter = 1445,
               DiscoveryDate = new DateTime(2003,10,21)
            }
         };
      }
   }
}

No comments:

Post a Comment