Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
MM example asp.net app?
Message
From
07/03/2013 08:26:41
 
 
To
07/03/2013 07:26:30
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
01567635
Message ID:
01567704
Views:
44
This message has been marked as the solution to the initial question of the thread.
This was done a few years ago and I'm not sure if it is exactly what you are asking for, but I hope it helps:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using OakLeaf.MM.Main.Web.UI;
using OakLeaf.MM.Main.Web.UI.WebControls;
using OakLeaf.MM.Main.Business;

using SamaanSystems.IAS.Business;
using System.Text;
using SamaanSystems.IAS.Web.UserControls;


namespace SamaanSystems.IAS.Web
{
    /// <summary>
    /// Client Edit Form
    /// </summary>
    public partial class ClientEdit : mmBusinessWebPage
    {
        #region Field References

        protected Guid ClientPK;
        protected DataSet dsClient;

        //Business Objects
        protected Client oClient;
        protected Phone oPhone;
        protected Address oAddress;
        protected Email oEmail;
        protected PolicyHeader oPolicyHeader;

        // lookups
        protected PhoneType oPhoneType;
        protected AddressType oAddressType;
        protected EmailType oEmailType;
        protected Occupation oOccupation;
        //protected DataSet dsSpouse;
        //protected DataSet dsSpouseList;
        protected Currency oCurrency;
        protected Country oCountry;
        protected User oUser;

        protected String selectType;

        #endregion Field References

        /// <summary>
        /// OnInit Event Handler
        /// </summary>
        /// <param name="e">Event Arguments</param>
        protected override void OnInit(EventArgs e)
        {
            this.RequiresSecurity = true;
            base.OnInit(e);
        }

        /// <summary>
        /// Page Load Event Handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["SelectType"] != null)
            {
                this.selectType = Request.QueryString["SelectType"];
            }

            // If no clientPK was passed, redirect to Client Search
            if (Request.QueryString["clientPK"] == null)
                Response.Redirect("~/ClientSearch.aspx");

            this.RegisterBusinessObjects();

            if (IsPostBack)
                this.SetCurrentClient();
            else
            {
                // Retrieve the Client ID passed by the Clients page
                this.ClientPK = new Guid(Request.QueryString["clientPK"]);
                this.GetCurrentClient();
                this.SetFocus(this.cboClientType);
            }
                
            this.FillLookups();
            this.calDateOfBirth.Visible = false;
        }

        #region Helper Methods

        /// <summary>
        /// Registers the Business Objects with the form
        /// </summary>
        private void RegisterBusinessObjects()
        {
            this.oClient = (Client)this.RegisterPrimaryBizObj(new Client());
            this.oOccupation = (Occupation)this.RegisterBizObj(new Occupation());

            this.oPhone = (Phone)this.RegisterBizObj(new Phone());
            this.oClient.RegisterChildBizObj(this.oPhone);

            this.oAddress = (Address)this.RegisterBizObj(new Address());
            this.oClient.RegisterChildBizObj(this.oAddress);

            this.oEmail = (Email)this.RegisterBizObj(new Email());
            this.oClient.RegisterChildBizObj(this.oEmail);

            this.oPolicyHeader = (PolicyHeader)this.RegisterBizObj(new PolicyHeader());
            this.oClient.RegisterChildBizObj(this.oPolicyHeader);

            // Type Lookups
            this.oPhoneType = (PhoneType)this.RegisterBizObj(new PhoneType());
            this.oEmailType = (EmailType)this.RegisterBizObj(new EmailType());
            this.oCurrency = (Currency)this.RegisterBizObj(new Currency());
            this.oAddressType = (AddressType)this.RegisterBizObj(new AddressType());
            this.oCountry = (Country)this.RegisterBizObj(new Country());

        }


        /// <summary>
        /// Retrieves all the lookup data
        /// </summary>
        private void FillLookups()
        {
            //this.oLookup.GetPhoneTypes();
            //retrieve the Lookup data
            this.oOccupation.GetAllEntities();
            this.oAddressType.GetAllEntities();
            this.oPhoneType.GetAllEntities();
            this.oEmailType.GetAllEntities();
            this.oCurrency.GetAllEntities();
            this.oCountry.GetAllEntities();

        }


        /// <summary>
        /// Sets the Display for the specified Type
        /// </summary>
        /// <param name="clientType">Client Type</param>
        private void SetClientTypeDisplay(string clientType)
        {
            if (clientType == "Individual")
            {
                this.pnlCompany.Visible = false;
                this.pnlIndividual.Visible = true;
            }
            else
            {
                this.pnlCompany.Visible = true;
                this.pnlIndividual.Visible = false;
            }
        }

        /// <summary>
        /// Set the Display for the Specified Marital Status
        /// </summary>
        /// <param name="maritalStatus">Marital Status</param>
        private void SetMaritalStatusDisplay(string maritalStatus)
        {
            if (maritalStatus == "Married" || maritalStatus == "Common Law" || maritalStatus == "Separated")
            {
                this.pnlSpouse.Visible = true;
            }
            else
            {
                this.pnlSpouse.Visible = false;
            }
        }

        /// <summary>
        /// Sets the Employment Display Mode
        /// </summary>
        /// <param name="employed"></param>
        private void SetEmploymentDisplay(bool? employed)
        {
            if (employed != null && (bool)employed)
                this.pnlEmployment.Visible = true;
            else
                this.pnlEmployment.Visible = false;
        }

        /// <summary>
        /// Sets the Maiden Name Display Mode
        /// </summary>
        /// <param name="sex"></param>
        /// <param name="maritalstatus"></param>
        private void SetMaidenNameDisplay(string sex, string maritalStatus )
        {
            if (maritalStatus == "Single" || sex == "M")
            {
                this.pnlMaidenName.Visible = false;
            }
            else
            {
                this.pnlMaidenName.Visible = true;
            }
        }

        #endregion Helper Methods

        #region Client Gets and Saves

        /// <summary>
        /// Get the current Client Data
        /// </summary>
        private void GetCurrentClient()
        {
            // Get the current client or start new
            if (this.ClientPK == Guid.Empty)
            {
                this.oClient.GetEmptyDataSet();
                this.oClient.NewEntity();
                this.ClientPK = this.oClient.Entity.ClientPK;
            }
            else
            {
                
                this.oClient.GetClientByClientPK(this.ClientPK);
            }

            if (this.oClient.Entity.HasValues)
            {
                Session["ClientPK"] = this.oClient.Entity.ClientPK;

                // If an individual, Retrieve the list of possible spouses
                if (this.oClient.Entity.ClientType == "Individual")
                {
                    this.oClient.GetPossibleSpouses(this.oClient.Entity.Sex);

                    // Retrieve the spouse
                    if (this.oClient.Entity.SpouseFK != null)
                    {
                        // Adds it to the existing Client DataSet
                        this.oClient.GetClientSpouse(this.oClient.Entity.SpouseFK);
                        DataTable dtSpouse = this.oClient.DataSet.Tables["Spouse"];
                        if (dtSpouse.Rows.Count > 0)
                        {
                            this.txtSpouse.Text = Utility.FormatNameLFM(dtSpouse.Rows[0]["FirstName"].ToString(),
                                dtSpouse.Rows[0]["MiddleName"].ToString(),
                                dtSpouse.Rows[0]["LastName"].ToString());
                        }
                    }
                }
                
                // DataSet should have Client, Spouse and SpouseList tables
                this.dsClient = this.oClient.DataSet;
                if (this.dsClient != null)
                    Session["dsClient"] = this.dsClient;

                // Get the related Phones
                this.oPhone.GetPhonesByEntityFK(this.ClientPK);
                Session["dsPhone"] = this.oPhone.DataSet;

                // Get the related Addresses
                this.oAddress.GetAddressesByEntityFK(this.ClientPK);
                Session["dsAddress"] = this.oAddress.DataSet;

                // Get the related Emails
                this.oEmail.GetEmailsByEntityFK(this.ClientPK);
                Session["dsEmail"] = this.oEmail.DataSet;


                // Get the related Policies
                this.oPolicyHeader.GetPoliciesByClientFK(this.ClientPK);
                Session["dsCientPolicyHeader"] = this.oPolicyHeader.DataSet;

                // Align the Combos with the proper Values
                this.cboClientType.SelectedValue = this.oClient.Entity.ClientType;
                this.cboSex.SelectedValue = this.oClient.Entity.Sex;
                this.cboSalaryFrequency.SelectedValue = this.oClient.Entity.SalaryFrequency;
                this.cboMaritalStatus.SelectedValue = this.oClient.Entity.MaritalStatus;
                this.SetClientTypeDisplay(this.oClient.Entity.ClientType);
                this.SetMaritalStatusDisplay(this.oClient.Entity.MaritalStatus);
                this.SetEmploymentDisplay(this.oClient.Entity.Employed);
                this.SetMaidenNameDisplay(this.oClient.Entity.Sex, this.oClient.Entity.MaritalStatus);

                // Get the related User
                if (this.oClient.Entity.UserFK != 0)
                {
                    this.oUser = new User();
                    this.oUser.GetUserByPK(this.oClient.Entity.UserFK);
                    Session["dsUser"] = this.oUser.DataSet;
                    this.txtUserName.Text = Utility.FormatNameFML((string)this.oUser.DataSet.Tables[0].Rows[0]["FirstName"], "", (string)this.oUser.DataSet.Tables[0].Rows[0]["LastName"]);
                }

                this.txtLastChanged.Text = this.oClient.Entity.LastChanged.ToString();

            }
        }

        /// <summary>
        /// Retrieve the Current Client DataSets
        /// </summary>
        private void SetCurrentClient()
        {
            if (Session["dsClient"] != null)
            {
                DataSet dsClient = (DataSet)Session["dsClient"];
                this.oClient.SetCurrentDataSet(dsClient);

                if (Session["dsPhone"] != null)
                {
                    DataSet dsPhone = (DataSet)Session["dsPhone"];
                    this.oPhone.SetCurrentDataSet(dsPhone);
                }
                if (Session["dsAddress"] != null)
                {
                    DataSet dsAddress = (DataSet)Session["dsAddress"];
                    this.oAddress.SetCurrentDataSet(dsAddress);
                }
                if (Session["dsEmail"] != null)
                {
                    DataSet dsEmail = (DataSet)Session["dsEmail"];
                    this.oEmail.SetCurrentDataSet(dsEmail);
                }
                if (Session["dsClientPolicyHeader"] != null)
                {
                    DataSet dsPolicyHeader = (DataSet)Session["dsClientPolicyHeader"];
                    this.oPolicyHeader.SetCurrentDataSet(dsPolicyHeader);
                }

            }
        }

        /// <summary>
        /// Loads the Current Client from the Session
        /// </summary>
        private void RetrieveClientData()
        {
            if (Session["dsClient"] != null)
                this.dsClient = (DataSet)Session["dsClient"];
        }

        /// <summary>
        /// Stores the Current client into the Session
        /// </summary>
        private void StoreClientData()
        {
            if (this.dsClient != null)
                Session["dsClient"] = this.dsClient;
        }

        /// <summary>
        /// Selects the Spouse
        /// </summary>
        private void SelectSpouse()
        {
            // Do everything else you need in this method also
            Guid spousePK = new
            Guid(this.grdSpouseList.SelectedDataKey.Value.ToString());
            if (spousePK != null)
            {
                this.RetrieveClientData();

                this.dsClient.Tables[this.oClient.TableName].Rows[0]["SpouseFK"] = spousePK;

                // The question now is do you want this saved to the backend also or just refreshed in the textbox?
                // It will get saved to the backend later when the client is saved if you store it in the row of the dataset
                // and then later when you save you retrieve it from the session again and pass it in to be saved.

                // pull the name details from the selected SpouseList record

                int intCurrentRow = grdSpouseList.SelectedIndex;
                GridViewRow gvrCurrent = grdSpouseList.Rows[intCurrentRow];
                //string fName = gvrCurrent.DataItem;

                string lName = gvrCurrent.Cells[1].Text;
                string fName = gvrCurrent.Cells[2].Text;
                string mName = gvrCurrent.Cells[3].Text;

                this.txtSpouse.Text = Utility.FormatNameLFM(fName, mName, lName);
                this.StoreClientData();
            }
        }

        #endregion Client Gets and Saves

        #region Client Control Event Handlers

        /// <summary>
        /// Cancel Button Event Handler
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Session["dsClient"] = null;
            Session["dsPhone"] = null;
            Session["dsAddress"] = null;
            Session["dsEmail"] = null;
            Session["dsClientPolicyHeader"] = null;
            Response.Redirect("~/ClientSearch.aspx");
        }

        /// <summary>
        /// Save Button Event Handler
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.RetrieveClientData();

            if (this.dsClient != null)
            {
                dsClient.Tables[this.oClient.TableName].Rows[0]["ClientType"] = this.cboClientType.SelectedValue;
                dsClient.Tables[this.oClient.TableName].Rows[0]["Sex"] = this.cboSex.SelectedValue;
                dsClient.Tables[this.oClient.TableName].Rows[0]["SalaryFrequency"] = this.cboSalaryFrequency.SelectedValue;
                dsClient.Tables[this.oClient.TableName].Rows[0]["MaritalStatus"] = this.cboMaritalStatus.SelectedValue;

                // loop through Address grid's rows and delete the selected items
                foreach (GridViewRow row in this.grdAddresses.Rows)
                {
                    CheckBox cb = (CheckBox)(row.FindControl("chkSelector"));
                    if (cb != null && cb.Checked)
                    {
                        // delete the row
                        Guid addressPK = new Guid(this.grdAddresses.DataKeys[row.RowIndex].Value.ToString());

                        this.oAddress.Delete(addressPK);
                    }
                }

                // loop through Phone grid's rows and delete the selected items
                foreach (GridViewRow row in this.grdPhones.Rows)
                {
                    CheckBox cb = (CheckBox)(row.FindControl("chkSelector"));
                    if (cb != null && cb.Checked)
                    {
                        // delete the row
                        Guid phonePK = new Guid(this.grdPhones.DataKeys[row.RowIndex].Value.ToString());

                        this.oPhone.Delete(phonePK);
                    }
                }

                // loop through Email grid's rows and delete the selected items
                foreach (GridViewRow row in this.grdEmails.Rows)
                {
                    CheckBox cb = (CheckBox)(row.FindControl("chkSelector"));
                    if (cb != null && cb.Checked)
                    {
                        // delete the row
                        Guid emailPK = new Guid(this.grdEmails.DataKeys[row.RowIndex].Value.ToString());

                        this.oEmail.Delete(emailPK);
                    }
                }

                // store the user id on the record
                this.dsClient.Tables[0].Rows[0]["UserfK"] = Session["mmUserSecurity_UserPk"];

                mmSaveDataResult result = this.Save(this.oClient, this.dsClient, this.oClient.TableName);
                if (result == mmSaveDataResult.RulesPassed)
                {
                    if (Session["PolicyPK"] != null)
                    {
                        if (this.selectType != null)
                        {
                            //string selectType = Request.QueryString["SelectType"];
                            switch (this.selectType)
                            {
                                case "Insured":
                                    Session["ClientFK"] = this.oClient.Entity.ClientPK;
                                    break;
                                case "Owner":
                                    Session["OwnerFK"] = this.oClient.Entity.ClientPK;
                                    break;
                                case "Payor":
                                    Session["PayorFK"] = this.oClient.Entity.ClientPK;
                                    break;
                            }
                        }
                        Response.Redirect("~/PolicyEdit.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/ClientSearch.aspx");
                    }
                    
                }
                else
                {
                    string rules = this.oClient.Rules.GetAllBrokenRules();
                }
            }
        }

        /// <summary>
        /// SaveAddPolicy Button Event Handler
        /// </summary>
        protected void btnNewPolicy_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/PolicyEdit.aspx?PolicyPK=" + Guid.Empty);
        }

        /// <summary>
        /// ClientType Selection Event Handler
        /// </summary>
        protected void cboClientType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetClientTypeDisplay(this.cboClientType.SelectedValue);
            if (this.cboClientType.SelectedValue.ToString() == "Company")
                this.SetFocus(this.txtCompanyName);
            else
                this.SetFocus(this.txtFirstName);
        }

        /// <summary>
        /// Pick Spouse Button Event Handler
        /// </summary>
        protected void btnPickSpouse_Click(object sender, EventArgs e)
        {

            this.RetrieveClientData();
            this.oClient.GetPossibleSpouses(this.oClient.Entity.Sex);
            this.StoreClientData();

            this.grdSpouseList.Visible = true;

            this.grdSpouseList.DataSource = this.dsClient;
            this.grdSpouseList.DataMember = "SpouseList";
            this.grdSpouseList.DataBind();
        }

        /// <summary>
        /// Marital Status Selection Event Handler
        /// </summary>
        protected void cboMaritalStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetMaidenNameDisplay(this.cboSex.SelectedValue, this.cboMaritalStatus.SelectedValue);
            this.SetMaritalStatusDisplay(this.cboMaritalStatus.SelectedValue);
            if (this.cboMaritalStatus.SelectedValue.ToString() == "Single")
                this.SetFocus(this.chkSmoker);
            else
                this.SetFocus(this.btnPickSpouse);

        }

        /// <summary>
        /// Sex Selection Event Handler
        /// </summary>
        protected void cboSex_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetMaidenNameDisplay(this.cboSex.SelectedValue, this.cboMaritalStatus.SelectedValue);
            this.SetFocus(this.txtNumberOfChildren);
        }

        /// <summary>
        /// Calendar Selection Event Handler
        /// </summary>
        protected void calDateOfBirth_SelectionChanged(object sender, EventArgs e)
        {
            DateTime dob = this.calDateOfBirth.SelectedDate;
            this.txtDateOfBirth.Text = dob.ToShortDateString();
            this.calDateOfBirth.Visible = false;
        }

        /// <summary>
        /// DisplayCalendar Button Event Handler
        /// </summary>
        protected void btnDisplayCalendar_Click(object sender, ImageClickEventArgs e)
        {
            string dateOfBirth = this.txtDateOfBirth.Text;
            if (dateOfBirth == "")
            {
                dateOfBirth = DateTime.Now.ToString();
            }
            this.calDateOfBirth.SelectedDate = Convert.ToDateTime(dateOfBirth);
            this.calDateOfBirth.VisibleDate = this.calDateOfBirth.SelectedDate;
            this.calDateOfBirth.Visible = this.calDateOfBirth.Visible != true;
        }



        /// <summary>
        /// SpouseList Grid Selection Handler
        /// </summary>
        protected void grdSpouseList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SelectSpouse();

            this.grdSpouseList.Visible = false;
        }

        /// <summary>
        /// Employed Checkbox Check Changed Handler
        /// </summary>
        protected void chkEmployed_CheckedChanged(object sender, EventArgs e)
        {
            this.SetEmploymentDisplay(this.chkEmployed.Checked);
            if (this.chkEmployed.Checked)
                this.SetFocus(this.cboOccupation);
            else
                this.SetFocus(this.txtNotes);
        }


        #endregion Client Control Event Handlers

        #region Phone Operations

        /// <summary>
        /// Phones Row Canceling Edit Event Handler
        /// </summary>
        protected void grdPhones_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            // Check to see if the cancel is a newly added row.
            DataSet dsPhone = (DataSet)Session["dsPhone"];
            if (dsPhone.Tables[0].Rows[e.RowIndex].RowState == DataRowState.Added)
            {
                dsPhone.Tables[0].Rows[e.RowIndex].Delete();
            }

            this.grdPhones.EditIndex = -1;
            this.BindControl(this.grdPhones);
        }

        /// <summary>
        /// Phones Row Deleting Event Handler
        /// </summary>
        protected void grdPhones_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid phonePK = new Guid(this.grdPhones.DataKeys[e.RowIndex].Value.ToString());
            this.oPhone.Delete(phonePK);
            this.BindControl(this.grdPhones);
        }

        /// <summary>
        /// Phones Row Editing Event Handler
        /// </summary>
        protected void grdPhones_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.grdPhones.EditIndex = e.NewEditIndex;
            this.BindControl(this.grdPhones);
            GridViewRow gvRow = this.grdPhones.Rows[this.grdPhones.EditIndex];
            this.SetFocus(gvRow.FindControl("cboPhoneType"));

        }

        /// <summary>
        /// Phones Row Updating Event Handler
        /// </summary>
        protected void grdPhones_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            this.grdPhones.EditIndex = e.RowIndex;
            DataSet dsPhone = (DataSet)Session["dsPhone"];

            // populate the description field in the dataset
            GridViewRow gvRow = this.grdPhones.Rows[e.RowIndex];
            Guid PhoneType = new Guid(((DropDownList)(gvRow.FindControl("cboPhoneType"))).SelectedValue.ToString());
            string phoneTypeDescription = ((DropDownList)(gvRow.FindControl("cboPhoneType"))).SelectedItem.ToString();
            DataSet dsPhoneType = this.oPhoneType.DataSet;
            DataRow row = (DataRow)dsPhoneType.Tables[0].Rows.Find(PhoneType);

            //find the correct row in the dataset based on the PhonePK
            Guid phonePK = new Guid(this.grdPhones.DataKeys[e.RowIndex].Values[0].ToString());
            DataRow drPhone = (DataRow)dsPhone.Tables[0].Rows.Find(phonePK);
            drPhone["PhoneTypeFK"] = PhoneType;
            drPhone["Description"] = phoneTypeDescription;
            drPhone["PhoneNumber"] = ((TextBox)(gvRow.Cells[2].Controls[0])).Text;
            drPhone["EntityFK"] = new Guid(Session["ClientPK"].ToString());

            this.grdPhones.EditIndex = -1;
            this.BindControl(this.grdPhones);
        }

        /// <summary>
        /// Add Phone Link Button to Add a new Phone
        /// </summary>
        protected void btnAddPhone_Click(object sender, EventArgs e)
        {
            DataSet dsPhone = (DataSet)Session["dsPhone"];

            this.oPhone.NewRow(dsPhone, this.oPhone.TableName);
            Guid newPK = (Guid)dsPhone.Tables[0].Rows[dsPhone.Tables[0].Rows.Count - 1]["PhonePK"];

            // default the PhoneTypeFK to the first item in the PhoneTypes data session
            dsPhone.Tables[0].Rows[dsPhone.Tables[0].Rows.Count - 1]["PhoneTypeFK"] = this.oPhoneType.DataSet.Tables[0].Rows[0]["PhoneTypePK"];
            
            this.BindControl(this.grdPhones);

            // Code to put grid in edit mode on the new record.
            int totalrows = this.grdPhones.Rows.Count;
            for (int r = 0; r < totalrows; r++)
            {
                if ((Guid)(this.grdPhones.DataKeys[r].Values[0]) == newPK)
                {
                    this.grdPhones.EditIndex = r;
                    break;
                }
            }
            this.BindControl(this.grdPhones);
            GridViewRow gvRow = this.grdPhones.Rows[this.grdPhones.EditIndex];
            this.SetFocus(gvRow.FindControl("cboPhoneType"));

        }


        #endregion Phone Operations

        #region Address Operations

        /// <summary>
        /// Addresses Row Canceling Edit Event Handler
        /// </summary>
        protected void grdAddresses_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            // Check to see if the cancel is a newly added row.
            DataSet dsAddress = (DataSet)Session["dsAddress"];
            if (dsAddress.Tables[0].Rows[e.RowIndex].RowState == DataRowState.Added)
            {
                dsAddress.Tables[0].Rows[e.RowIndex].Delete();
            }

            this.grdAddresses.EditIndex = -1;
            this.BindControl(this.grdAddresses);
        }

        /// <summary>
        /// Addresses Row Deleting Event Handler
        /// </summary>
        protected void grdAddresses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid addressPK = new Guid(this.grdAddresses.DataKeys[e.RowIndex].Value.ToString());
            this.oAddress.Delete(addressPK);
            this.BindControl(this.grdAddresses);
        }

        /// <summary>
        /// Address Row Editing Event Handler
        /// </summary>
        protected void grdAddresses_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.grdAddresses.EditIndex = e.NewEditIndex;
            this.BindControl(this.grdAddresses);
            GridViewRow gvRow = this.grdAddresses.Rows[this.grdAddresses.EditIndex];
            this.SetFocus(gvRow.FindControl("cboAddressType"));
        }

        /// <summary>
        /// Address Row Updating Event Handler
        /// </summary>
        protected void grdAddresses_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            this.grdAddresses.EditIndex = e.RowIndex;
            DataSet dsAddress = (DataSet)Session["dsAddress"];

            // populate the description field in the dataset
            GridViewRow gvRow = this.grdAddresses.Rows[e.RowIndex];
            
            //AddressType
            Guid AddressType = new Guid(((DropDownList)(gvRow.FindControl("cboAddressType"))).SelectedValue.ToString());
            string addressTypeDescription = ((DropDownList)(gvRow.FindControl("cboAddressType"))).SelectedItem.ToString();
            DataSet dsAddressType = this.oAddressType.DataSet;
            DataRow row = (DataRow)dsAddressType.Tables[0].Rows.Find(AddressType);

            //Country
            Guid Country = new Guid(((DropDownList)(gvRow.FindControl("cboCountry"))).SelectedValue.ToString());
            string countryDescription = ((DropDownList)(gvRow.FindControl("cboCountry"))).SelectedItem.ToString();
            DataSet dsCountry = this.oCountry.DataSet;
            DataRow countryRow = (DataRow)dsCountry.Tables[0].Rows.Find(Country);
            
            //find the correct row in the dataset based on the AddressPK
            Guid addressPK = new Guid(this.grdAddresses.DataKeys[e.RowIndex].Values[0].ToString());
            DataRow drAddress = (DataRow)dsAddress.Tables[0].Rows.Find(addressPK);
            drAddress["AddressTypeFK"] = AddressType;
            drAddress["Description"] = addressTypeDescription;
            drAddress["Address1"] = ((TextBox)(gvRow.FindControl("txtAddress1"))).Text;
            drAddress["Address2"] = ((TextBox)(gvRow.FindControl("txtAddress2"))).Text;
            drAddress["Address3"] = ((TextBox)(gvRow.FindControl("txtAddress3"))).Text;
            drAddress["Address4"] = ((TextBox)(gvRow.FindControl("txtAddress4"))).Text;
            drAddress["Country"] = countryDescription;
            drAddress["CountryFK"] = Country;
            drAddress["EntityFK"] = new Guid(Session["ClientPK"].ToString());

            this.grdAddresses.EditIndex = -1;
            this.BindControl(this.grdAddresses);
        }

        /// <summary>
        /// Add Address Link Button to Add a new Address
        /// </summary>
        protected void btnAddAddress_Click(object sender, EventArgs e)
        {
            DataSet dsAddress = (DataSet)Session["dsAddress"];

            this.oAddress.NewRow(dsAddress, this.oAddress.TableName);
            Guid newPK = (Guid)dsAddress.Tables[0].Rows[dsAddress.Tables[0].Rows.Count - 1]["AddressPK"];

            // default the AddressTypeFK to the first item in the AddressTypes data session
            dsAddress.Tables[0].Rows[dsAddress.Tables[0].Rows.Count - 1]["AddressTypeFK"] = this.oAddressType.DataSet.Tables[0].Rows[0]["AddressTypePK"];

            // default the CountryFK to the first item in the Country data session
            dsAddress.Tables[0].Rows[dsAddress.Tables[0].Rows.Count - 1]["CountryFK"] = this.oCountry.DataSet.Tables[0].Rows[0]["CountryPK"];

            this.BindControl(this.grdAddresses);

            // Code to put grid in edit mode on the new record.
            int totalrows = this.grdAddresses.Rows.Count;
            for (int r = 0; r < totalrows; r++)
            {
                if ((Guid)(this.grdAddresses.DataKeys[r].Values[0]) == newPK)
                {
                    this.grdAddresses.EditIndex = r;
                    break;
                }
            }
            this.BindControl(this.grdAddresses);
            GridViewRow gvRow = this.grdAddresses.Rows[this.grdAddresses.EditIndex];
            this.SetFocus(gvRow.FindControl("cboAddressType"));

        }


        #endregion Address Operations

        #region Email Operations

        /// <summary>
        /// Emails Row Canceling Edit Event Handler
        /// </summary>
        protected void grdEmails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            // Check to see if the cancel is a newly added row.
            DataSet dsEmail = (DataSet)Session["dsEmail"];
            if (dsEmail.Tables[0].Rows[e.RowIndex].RowState == DataRowState.Added)
            {
                dsEmail.Tables[0].Rows[e.RowIndex].Delete();
            }

            this.grdEmails.EditIndex = -1;
            this.BindControl(this.grdEmails);
        }

        /// <summary>
        /// Emails Row Deleting Event Handler
        /// </summary>
        protected void grdEmails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid emailPK = new Guid(this.grdEmails.DataKeys[e.RowIndex].Value.ToString());
            this.oEmail.Delete(emailPK);
            this.BindControl(this.grdEmails);
        }

        /// <summary>
        /// Emails Row Editing Event Handler
        /// </summary>
        protected void grdEmails_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.grdEmails.EditIndex = e.NewEditIndex;
            this.BindControl(this.grdEmails);
            GridViewRow gvRow = this.grdEmails.Rows[this.grdEmails.EditIndex];
            this.SetFocus(gvRow.FindControl("cboEmailType"));

        }

        /// <summary>
        /// Emails Row Updating Event Handler
        /// </summary>
        protected void grdEmails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            this.grdEmails.EditIndex = e.RowIndex;
            DataSet dsEmail = (DataSet)Session["dsEmail"];

            // populate the description field in the dataset
            GridViewRow gvRow = this.grdEmails.Rows[e.RowIndex];
            Guid EmailType = new Guid(((DropDownList)(gvRow.FindControl("cboEmailType"))).SelectedValue.ToString());
            string emailTypeDescription = ((DropDownList)(gvRow.FindControl("cboEmailType"))).SelectedItem.ToString();
            DataSet dsEmailType = this.oEmailType.DataSet;
            DataRow row = (DataRow)dsEmailType.Tables[0].Rows.Find(EmailType);

            //find the correct row in the dataset based on the EmailPK
            Guid emailPK = new Guid(this.grdEmails.DataKeys[e.RowIndex].Values[0].ToString());
            DataRow drEmail = (DataRow)dsEmail.Tables[0].Rows.Find(emailPK);
            drEmail["EmailTypeFK"] = EmailType;
            drEmail["Description"] = emailTypeDescription;
            drEmail["Email"] = ((TextBox)(gvRow.Cells[2].Controls[0])).Text;
            drEmail["EntityFK"] = new Guid(Session["ClientPK"].ToString());

            this.grdEmails.EditIndex = -1;
            this.BindControl(this.grdEmails);
        }

        /// <summary>
        /// Add Email Link Button to Add a new Email
        /// </summary>
        protected void btnAddEmail_Click(object sender, EventArgs e)
        {
            DataSet dsEmail = (DataSet)Session["dsEmail"];

            this.oEmail.NewRow(dsEmail, this.oEmail.TableName);
            Guid newPK = (Guid)dsEmail.Tables[0].Rows[dsEmail.Tables[0].Rows.Count - 1]["EmailPK"];

            // default the EmailTypeFK to the first item in the EmailTypes data session
            dsEmail.Tables[0].Rows[dsEmail.Tables[0].Rows.Count - 1]["EmailTypeFK"] = this.oEmailType.DataSet.Tables[0].Rows[0]["EmailTypePK"];

            this.BindControl(this.grdEmails);

            // Code to put grid in edit mode on the new record.
            int totalrows = this.grdEmails.Rows.Count;
            for (int r = 0; r < totalrows; r++)
            {
                if ((Guid)(this.grdEmails.DataKeys[r].Values[0]) == newPK)
                {
                    this.grdEmails.EditIndex = r;
                    break;
                }
            }
            this.BindControl(this.grdEmails);
            GridViewRow gvRow = this.grdEmails.Rows[this.grdEmails.EditIndex];
            this.SetFocus(gvRow.FindControl("cboEmailType"));

        }

        #endregion Email Operations

    }
}
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform