using System;
using System.Data;
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;

public partial class CustomerDataList : System.Web.UI.UserControl
{
   public delegate void EditRecordHandler(object sender, ChangedRecordEventArgs e);
   public event EditRecordHandler EditRecord;

   public delegate void FinishedEditRecordHandler(object sender, EventArgs e);
   public event FinishedEditRecordHandler FinishedEditRecord;

   private int howManyColumns = 3;
   private RepeatDirection whichDirection = RepeatDirection.Horizontal;

   public int HowManyColumns
   {
      get { return howManyColumns; }
      set { howManyColumns = value; }
   }

   public RepeatDirection WhichDirection
   {
      get { return whichDirection; }
      set { whichDirection = value; }
   }

   protected void Page_Load(object sender, EventArgs e)
   {
   }

   protected void Page_PreRender(object sender, EventArgs e)
   {
      this.DataList1.RepeatColumns = this.howManyColumns;
      this.DataList1.RepeatDirection = this.whichDirection;
   }

   protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
   {
      DataList1.EditItemIndex = e.Item.ItemIndex;
      DataBind();

      Label lbl = (Label)e.Item.FindControl("CompanyNameLabel");
      string companyName = lbl.Text;
      ChangedRecordEventArgs cre =
         new ChangedRecordEventArgs(companyName);
      OnEditRecord(cre);
   }

   protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
   {
      DataList1.EditItemIndex = -1;
      DataBind();
      OnFinishedEditRecord(new EventArgs());
   }

   protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
   {
      OnFinishedEditRecord(new EventArgs());
   }

   protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
   {
      // (1) Pobieramy recordID z zaznaczonego elementu (cig znakowy).
      string recordID = (DataList1.DataKeys[e.Item.ItemIndex]).ToString();

      // (2) Pobieramy odniesienie do parametru customerID.
      System.Web.UI.WebControls.Parameter param =
         DataListCustomerDeleteDataSource.DeleteParameters["CustomerID"];

      // (3) Ustawiamy warto domyln parametru do wartoci
      // rekordu przeznaczonego do usunicia.
      param.DefaultValue = recordID

      // (4) Usuwamy rekord.
      DataListCustomerDeleteDataSource.Delete();

      // (5) Ponownie doczamy list.
      DataBind();
   }

   public class ChangedRecordEventArgs : EventArgs
   {
      private string companyName;

      public string CompanyName
      {
         get { return companyName; }
      }

      public ChangedRecordEventArgs(string companyName)
      {
         this.companyName = companyName;
      }
   }

   protected virtual void OnEditRecord(ChangedRecordEventArgs e)
   {
      if (EditRecord != null)
      {
         EditRecord(this, e);
       }
   }

   protected virtual void OnFinishedEditRecord(EventArgs e)
   {
      if (FinishedEditRecord != null)
      {
         FinishedEditRecord(this, e);
      }
   }
}
