using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebStructure
{
    // Defines a single address.
    public struct Addresses
    {
        // Make it easy to add new names by providing a special
        // constructor.
        public Addresses(
            String NewName, String NewAddress, String NewCity,
            String NewState, String NewZIP)
        {
            // Add each input value to the value inside the structure.
            Name = NewName;
            Address = NewAddress;
            City = NewCity;
            State = NewState;
            ZIPCode = NewZIP;
        }

        public String Name;     // Person's name.
        public String Address;  // Person's street address.
        public String City;     // City in which the person lives.
        public String State;    // State in which the person lives.
        public String ZIPCode;  // Location's ZIP code.
    }

    public partial class Default : System.Web.UI.Page
    {
        // Declare an array a List of addresses.
        List<Addresses> MyAddresses;

        // Keep track of the current entry.
        Int32 CurrentEntry;

        protected void Page_Load(object sender, EventArgs e)
        {
            // Initialize the array.
            MyAddresses = new List<Addresses>();

            // Add entries to the array.
            MyAddresses.Add(
                new Addresses(
                    "Mark Hanson", "123 Anywhere Street", "Somewhere", "UT", "99999"));
            MyAddresses.Add(
                new Addresses(
                    "Kim Abercrombie", "456 5th Avenue", "Outback", "AK", "99998"));
            MyAddresses.Add(
                new Addresses(
                    "Armando Pinto", "9925 Galaxy Drive", "Nowhere", "IN", "99997"));

            // Set the initial entry.
            if (Page.IsPostBack)
                CurrentEntry = Int32.Parse(CurrentRecord.Value);
            else
                CurrentEntry = 0;

            // Display the first element on screen.
            ShowEntry();
        }

        public void ShowEntry()
        {
            // Display each of the address elements.
            txtName.Text = MyAddresses[CurrentEntry].Name;
            txtAddress.Text = MyAddresses[CurrentEntry].Address;
            txtCity.Text = MyAddresses[CurrentEntry].City;
            txtState.Text = MyAddresses[CurrentEntry].State;
            txtZIPCode.Text = MyAddresses[CurrentEntry].ZIPCode;
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            // Turn off the status message.
            lblStatus.Visible = false;

            // Determine if this is the last entry.
            if (CurrentEntry + 1 == MyAddresses.Count)
            {

                // If so, disable the Next button.
                btnNext.Enabled = false;

                // Tell the user that this is the last entry.
                lblStatus.Text = "Last Entry!";
                lblStatus.Visible = true;
            }

            else
            {

                // Update the current entry.
                CurrentEntry++;

                // Store the value for future use.
                CurrentRecord.Value = CurrentEntry.ToString();
            }

            // Determine if it's acceptable to enable the Previous button.
            if (MyAddresses.Count > 1)

                btnPrevious.Enabled = true;

            // Display the current entry.
            ShowEntry();
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            // Turn off the status message.
            lblStatus.Visible = false;

            // Determine if this is the first entry.
            if (CurrentEntry == 0)
            {

                // If so, disable the Previous button.
                btnPrevious.Enabled = false;

                // Tell the user that this is the last entry.
                lblStatus.Text = "First Entry!";
                lblStatus.Visible = true;
            }

            else
            {
                // Update the current entry.
                CurrentEntry--;

                // Store the value for future use.
                CurrentRecord.Value = CurrentEntry.ToString();
            }

            // Determine if it's acceptable to enable the Next button.
            if (CurrentEntry + +1 < MyAddresses.Count)

                btnNext.Enabled = true;

            // Display the current entry.
            ShowEntry();
        }
    }
}