All of what I've posted so far goes in the so-called "business layer". I put this in a separate .NET "class library" project. This way, I can use this same code for any sort of visual display - be it on in desktop windows forms application or a simple console app. I stuck all the code in its own namespace as well to differentiate it from any sort of "presentation layer" code that I would happen to write.

Now that I've given you the run-down of all the code that makes this program tick, let me give you a quick example of the presentation-layer code. At the end of the article you can download all the code as one .NET solution.

BoardControl.ascx 

The Home of Jon © 2007
  1. <%@ Control Language="C#" AutoEventWireup="true" Inherits="BoardControl" %>
  2.  
  3. <div class="boardControl">
  4.    
  5.     <asp:ImageButton ID="btnPosition1" runat="server" ImageUrl="~/App_Themes/<%= Page.Theme %>/Images/empty.jpg" CssClass="button" OnClick="btnPosition_Click" />
  6.     <asp:ImageButton ID="btnPosition2" runat="server" ImageUrl="~/App_Themes/<%= Page.Theme %>/Images/empty.jpg" CssClass="button" OnClick="btnPosition_Click"  />
  7.     <asp:ImageButton ID="btnPosition3" runat="server" ImageUrl="~/App_Themes/<%= Page.Theme %>/Images/empty.jpg" CssClass="button" OnClick="btnPosition_Click"  /><br />
  8.     <asp:ImageButton ID="btnPosition4" runat="server" ImageUrl="~/App_Themes/<%= Page.Theme %>/Images/empty.jpg" CssClass="button" OnClick="btnPosition_Click"  />
  9.     <asp:ImageButton ID="btnPosition5" runat="server" ImageUrl="~/App_Themes/<%= Page.Theme %>/Images/empty.jpg" CssClass="button" OnClick="btnPosition_Click"  />
  10.     <asp:ImageButton ID="btnPosition6" runat="server" ImageUrl="~/App_Themes/<%= Page.Theme %>/Images/empty.jpg" CssClass="button" OnClick="btnPosition_Click"  /><br />
  11.     <asp:ImageButton ID="btnPosition7" runat="server" ImageUrl="~/App_Themes/<%= Page.Theme %>/Images/empty.jpg" CssClass="button" OnClick="btnPosition_Click"  />
  12.     <asp:ImageButton ID="btnPosition8" runat="server" ImageUrl="~/App_Themes/<%= Page.Theme %>/Images/empty.jpg" CssClass="button" OnClick="btnPosition_Click"  />
  13.     <asp:ImageButton ID="btnPosition9" runat="server" ImageUrl="~/App_Themes/<%= Page.Theme %>/Images/empty.jpg" CssClass="button" OnClick="btnPosition_Click"  />
  14. </div>
Parsed in 0.156 seconds

This is the markup for the user control. All we have is basically a bunch of buttons (using CSS they float just right so that there are only 3 buttons in one row). 

In the code-behind we expose a very important public property called Board. In it we store the current Board object in the ViewState. We can do this because in our Board.cs file I marked the Board class with the [Serializable()] attribute. This is a great boon to us, because we can use the same Board object across page requests.

The Home of Jon © 2007
  1.     /// <summary>
  2.     /// Internal Board object to represent state
  3.     /// </summary>
  4.     public Board Board
  5.     {
  6.         get
  7.         {
  8.             object board = ViewState["_Board"];
  9.             // if board doesn't exist, create new one
  10.             if (board == null)
  11.             {
  12.                 Board b = new Board();
  13.                 ViewState["_Board"] = b;
  14.                 return b;
  15.             }
  16.             else
  17.             {
  18.                 return (Board)board;
  19.             }
  20.         }
  21.         private set
  22.         {
  23.             ViewState["_Board"] = value;
  24.         }
  25.     }
  26.  
  27.     /// <summary>
  28.     /// Restore this control from a Board object
  29.     /// </summary>
  30.     /// <param name="board">Board to represent internal state.</param>
  31.     private void RestoreControlFromBoard(Board board)
  32.     {
  33.         int i = 1;
  34.         foreach (BoardPlayer player in board)
  35.         {
  36.             ImageButton btn = (ImageButton) FindControl(string.Format("btnPosition{0}", i));
  37.             btn.ImageUrl = GetImageUrlForPlayer(player);
  38.             // if a player has made a move on this button, disable it, so
  39.             // we can't still click on it
  40.             if (player != BoardPlayer.NoPlayer)
  41.                 btn.Enabled = false;
  42.             i++;
  43.         }
  44.     }
  45.  
  46.     protected void Page_Load(object sender, EventArgs e)
  47.     {
  48.         if (!IsPostBack)
  49.         {
  50.             // Restore the controls on form from the Board object
  51.             RestoreControlFromBoard(Board);
  52.             if (Board.IsEmpty())
  53.                 MakeComputerMove(true);
  54.         }
  55.     }
Parsed in 0.069 seconds

That is all the major portions of the code to review. The rest is straightforward ASP.NET form code for displaying the UserControl we make in our page form. You can see in the solution I provide that I've added some sugar and put our User Control in Microsoft's new UpdatePanel control. That lets us do some fancy AJAX without doing any work at all. 

I wish I could provide a working demo, but the way my hosting is set up, I cannot run .NET code without buying a whole new hosting account. Oh well! Hope that helps for now! You can download the full source for this project below. Thanks.

(Notes: Realize that I coded this project for clean-ness and readability. Also modularization and easy modification. That said, the code could be more compact if you wanted it to be, and you could accomplish the same thing with fewer lines of code. However, the price you would pay would be readability and maintenance. I figured it was worth it to Keep It Simple Stupid.)

Downloads

FilenameFilesizeDateLink 
Filetype image tictactoe_asp_net.zip 398K 2007-11-18 Download

Previous Previous    Page Page 2 of 2

Trackback

Trackback URL for Entry: http://www.thehomeofjon.net/trackback/receive/45.html

Leave a Comment

Name (required)
E-mail (required, will not be shared)
Website
Visual CAPTCHA Enter security code shown (required)

Receive notifications for new comments