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;
namespace CIF.Modules.Northwind
{
public partial class OrdersGridFormView : System.Web.UI.UserControl
{
#region Events and Delegates
public delegate void InsertOrdersEventHandler(CIF.Business.Northwind.OrdersItem _OrdersItem);
public event InsertOrdersEventHandler InsertOrders;
protected virtual void OnInsertOrders(CIF.Business.Northwind.OrdersItem _OrdersItem)
{
if (InsertOrders != null)
{
InsertOrders(_OrdersItem);
}
}
public delegate void UpdateOrdersEventHandler(CIF.Business.Northwind.OrdersItem _UpdateItem);
public event UpdateOrdersEventHandler UpdateOrders;
protected virtual void OnUpdateOrders(CIF.Business.Northwind.OrdersItem _OrdersItem)
{
if (UpdateOrders != null)
{
UpdateOrders(_OrdersItem);
}
}
public delegate void DeleteOrdersEventHandler(CIF.Business.Northwind.OrdersItem _OrdersItem);
public event DeleteOrdersEventHandler DeleteOrders;
protected virtual void OnDeleteOrders(CIF.Business.Northwind.OrdersItem _OrdersItem)
{
if (DeleteOrders != null)
{
DeleteOrders(_OrdersItem);
}
}
#endregion
#region Private/Protected Members
protected ViewMode m_ViewMode = ViewMode.GridView;
protected enum ViewMode { Unknown, GridView, FormView }
private const string DATETIME_FORMAT = "MM/dd/yyyy";
#endregion
#region Public Properties
/// <summary>
/// Forces editing to occur in a FormView control instead of the gridview
/// </summary>
public bool ForceEditsInFormView
{
get { return Convert.ToBoolean(ViewState["ForceEditsInFormView"]); }
set { ViewState["ForceEditsInFormView"] = value; }
}
/// <summary>
/// Allows sorting to be enabled in the gridview
/// </summary>
public bool AllowGridViewRecordSorting
{
get { return gvOrders.AllowSorting; }
set { gvOrders.AllowSorting = value; }
}
/// <summary>
/// Allows record selection in the gridview
/// </summary>
public bool AllowGridViewRecordSelection
{
get { return gvOrders.AutoGenerateSelectButton; }
set { gvOrders.AutoGenerateSelectButton = value; }
}
/// <summary>
/// Allows user to insert a record into the gridview
/// </summary>
public bool AllowGridViewRecordInserting
{
get { return Convert.ToBoolean(ViewState["AllowGridViewRecordInserting"]); }
set { ViewState["AllowGridViewRecordInserting"] = value; }
}
/// <summary>
/// Allows user to update a record in the gridview
/// </summary>
public bool AllowGridViewRecordEditing
{
get { return gvOrders.AutoGenerateEditButton; }
set { gvOrders.AutoGenerateEditButton = value; }
}
/// <summary>
/// Allows user to delete a record in the gridview
/// </summary>
public bool AllowGridViewRecordDeleting
{
get { return gvOrders.AutoGenerateDeleteButton; }
set { gvOrders.AutoGenerateDeleteButton = value; }
}
/// <summary>
/// Shows the insert controls in the gridview
/// </summary>
public bool GridViewEditControlsVisible
{
get { return Convert.ToBoolean(ViewState["GridViewEditControlsVisible"]); }
set { ViewState["GridViewEditControlsVisible"] = value; }
}
/// <summary>
/// Allows user to insert a record into the formview
/// </summary>
public bool AllowFormViewRecordInserting
{
get { return Convert.ToBoolean(ViewState["AllowFormViewRecordInserting"]); }
set { ViewState["AllowFormViewRecordInserting"] = value; }
}
/// <summary>
/// Allows user to update a record in the formview
/// </summary>
public bool AllowFormViewRecordEditing
{
get { return Convert.ToBoolean(ViewState["AllowFormViewRecordEditing"]); }
set { ViewState["AllowFormViewRecordEditing"] = value; }
}
/// <summary>
/// Allows user to delete a record in the formview
/// </summary>
public bool AllowFormViewRecordDeleting
{
get { return Convert.ToBoolean(ViewState["AllowFormViewRecordDeleting"]); }
set { ViewState["AllowFormViewRecordDeleting"] = value; }
}
#endregion
#region Public Functions
/// <summary>
/// Initialize the member variables and bind the gridview/FormView
/// </summary>
public void Initialize()
{
AllowGridViewRecordSorting = true;
AllowGridViewRecordDeleting = true;
AllowGridViewRecordEditing = true;
AllowGridViewRecordInserting = true;
AllowGridViewRecordSelection = true;
AllowFormViewRecordDeleting = true;
AllowFormViewRecordEditing = true;
AllowFormViewRecordInserting = true;
ForceEditsInFormView = false;
GridViewEditControlsVisible = false;
ClearDataViews();
BindDataViews();
}
/// <summary>
/// Initiates a filtered search
/// </summary>
/// <param name="_SearchFilter"></param>
public void SearchRecords(string _SearchFilter)
{
SearchFilter = _SearchFilter;
BindDataViews();
}
#endregion
#region Private/Protected Properties
/// <summary>
/// Private variable for storing the latest sorted column expression
/// </summary>
private string SortExpression
{
get
{
if (ViewState["SortExpression"] != null)
return (string)ViewState["SortExpression"];
else
return string.Empty;
}
set
{
if (ViewState["SortExpression"] == null)
{
ViewState.Add("SortExpression", value);
}
else
{
ViewState["SortExpression"] = value;
}
}
}
/// <summary>
/// Private variable for storing the latest sorted column direction
/// </summary>
private string SortDirection
{
get
{
if (ViewState["SortDirection"] != null)
return (string)ViewState["SortDirection"];
else
return "ASC";
}
set
{
if (ViewState["SortDirection"] == null)
{
ViewState.Add("SortDirection", value);
}
else
{
ViewState["SortDirection"] = value;
}
}
}
/// <summary>
/// The search string in which to filter the records on
/// </summary>
private string SearchFilter
{
get
{
if (ViewState["SearchFilter"] != null)
return (string)ViewState["SearchFilter"];
else
return string.Empty;
}
set
{
if (ViewState["SearchFilter"] == null)
{
ViewState.Add("SearchFilter", value);
}
else
{
ViewState["SearchFilter"] = value;
}
}
}
#endregion
#region Private/Protected Functions
/// <summary>
/// Sets the new ViewMode for the control and updates the display accordingly
/// </summary>
/// <param name="vmNewViewMode"></param>
protected void SetBehavior(ViewMode vmNewViewMode)
{
m_ViewMode = vmNewViewMode;
SetBehavior();
}
/// <summary>
/// Handles the display of the gridview/FormView controls according to the ViewMode
/// </summary>
protected void SetBehavior()
{
bool bHasGridRows = (gvOrders.Rows.Count > 0);
if ((bHasGridRows == false) && (AllowFormViewRecordInserting == true))
{
m_ViewMode = ViewMode.FormView;
}
switch (m_ViewMode)
{
case ViewMode.Unknown:
pnlGridView.Visible = bHasGridRows;
pnlFormView.Visible = !bHasGridRows;
break;
case ViewMode.GridView:
pnlGridView.Visible = true;
pnlFormView.Visible = false;
break;
case ViewMode.FormView:
pnlGridView.Visible = false;
pnlFormView.Visible = true;
break;
}
lbtnGridInsertOrders.Visible = GridViewEditControlsVisible;
lbtnGridCancelOrders.Visible = GridViewEditControlsVisible;
gvOrders.ShowFooter = GridViewEditControlsVisible;
lbtnAddNewOrders.Visible = (AllowGridViewRecordInserting && !GridViewEditControlsVisible && !pnlFormView.Visible);
lbtnReturnToOrdersList.Visible = bHasGridRows;
}
/// <summary>
/// Clears out the binding of the old data
/// </summary>
protected void ClearDataViews()
{
gvOrders.DataSource = null;
gvOrders.DataBind();
fvOrders.DataSource = null;
fvOrders.DataBind();
}
/// <summary>
/// Binds the gridview/FormView controls - this overload will display the records in a gridview
/// </summary>
protected void BindDataViews()
{
gvOrders.ShowFooter = (AllowGridViewRecordInserting && GridViewEditControlsVisible);
BindDataViews(0);
}
/// <summary>
/// Binds the gridview/formview controls - if primarykey is specified, the formview is used
/// </summary>
/// <param name="_OrderID"></param>
protected void BindDataViews(int _OrderID)
{
CIF.Business.Northwind.Orders _Orders = new CIF.Business.Northwind.Orders();
DataTable dtOrders = null;
if (_OrderID > 0)
{
dtOrders = _Orders.GetDataTable("OrderID", _OrderID.ToString());
fvOrders.DataSource = dtOrders;
fvOrders.DataBind();
SetBehavior(ViewMode.FormView);
}
else
{
dtOrders = _Orders.GetDataTable();
if (dtOrders.Rows.Count > 0)
{
DataView dv = dtOrders.DefaultView;
if ((SortExpression != string.Empty) && (SortDirection != string.Empty))
dv.Sort = SortExpression + " " + SortDirection;
if (SearchFilter != string.Empty)
dv.RowFilter = SearchFilter;
if (dv.Count <= 0)
{
gvOrders.DataSource = null;
gvOrders.DataBind();
return;
}
gvOrders.DataSource = dv;
gvOrders.DataBind();
}
else
{
if (AllowFormViewRecordInserting == true)
{
fvOrders.DataSource = dtOrders;
fvOrders.DataBind();
}
else
{
gvOrders.DataSource = null;
gvOrders.DataBind();
}
}
SetBehavior();
}
}
/// <summary>
/// Format a date-time field according to the predefined format
/// </summary>
/// <param name="dtvalue"></param>
protected string FormatDateTime(object dtvalue)
{
string sDateTime = Convert.ToString(dtvalue);
if (IsDateTime(sDateTime) == true)
{
System.DateTime dt = System.DateTime.Parse(sDateTime);
if (dt == new DateTime(1900, 1, 1))
sDateTime = string.Empty;
else
sDateTime = dt.ToString(DATETIME_FORMAT);
}
return sDateTime;
}
/// <summary>
/// Returns true if the given string is a valid date string, or false if it's not
/// </summary>
/// <param name="sDateTime"></param>
/// <returns></returns>
public static bool IsDateTime(string sDateTime)
{
bool bIsDateTime = false;
try
{
System.DateTime.Parse(sDateTime);
bIsDateTime = true;
}
catch
{
bIsDateTime = false;
}
return bIsDateTime;
}
/// <summary>
/// Returns the primarykey value for the specified gridrow in the gridview
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
private int GetGridViewRowOrderID(GridViewRow row)
{
int _OrderID = 0;
Label _ctl = (Label)row.FindControl("lblOrderID");
if (_ctl == null)
{
throw new Exception("GetOrderID: could not find OrderID control!");
}
_OrderID = Convert.ToInt32(_ctl.Text);
return _OrderID;
}
/// <summary>
/// Returns the string value for the specified textbox control in the specified gridrow in the gridview
/// </summary>
/// <param name="row"></param>
/// <param name="sControlName"></param>
/// <returns></returns>
private string GetGridViewRowTextValue(GridViewRow row, string sControlName)
{
string sFieldValue = string.Empty;
TextBox _ctl = (TextBox)row.FindControl(sControlName);
if (_ctl == null)
{
throw new Exception("GetGridViewRowTextValue: could not find " + sControlName + " control!");
}
sFieldValue = _ctl.Text.Trim();
return sFieldValue;
}
/// <summary>
/// Returns the string value for the specified textbox control in the specified gridrow in the gridview
/// </summary>
/// <param name="row"></param>
/// <param name="sControlName"></param>
/// <returns></returns>
private string GetGridViewRowDropDownListValue(GridViewRow row, string sControlName)
{
string sFieldValue = string.Empty;
DropDownList _ctl = (DropDownList)row.FindControl(sControlName);
if (_ctl == null)
{
throw new Exception("GetGridViewRowDropDownListValue: could not find " + sControlName + " control!");
}
sFieldValue = _ctl.SelectedValue;
return sFieldValue;
}
/// <summary>
/// Returns the bool value for the specified checkbox control in the specified gridrow in the gridview
/// </summary>
/// <param name="sControlName"></param>
/// <returns></returns>
private bool GetGridViewRowCheckBoxValue(GridViewRow row, string sControlName)
{
bool bFieldValue = false;
CheckBox _ctl = (CheckBox)row.FindControl(sControlName);
if (_ctl == null)
{
throw new Exception("GetGridViewRowCheckBoxValue: could not find " + sControlName + " control!");
}
bFieldValue = _ctl.Checked;
return bFieldValue;
}
/// <summary>
/// Returns the primarykey value for the current record in the FormView
/// </summary>
/// <returns></returns>
private int GetFormViewOrderID()
{
int _OrderID = 0;
Label _ctl = (Label)fvOrders.FindControl("lblOrderID");
if (_ctl == null)
{
throw new Exception("GetOrderID: could not find OrderID control!");
}
_OrderID = Convert.ToInt32(_ctl.Text);
return _OrderID;
}
/// <summary>
/// Returns the string value for the specified textbox control in the FormView
/// </summary>
/// <param name="sControlName"></param>
/// <returns></returns>
private string GetTextFieldValue(string sControlName)
{
string sFieldValue = string.Empty;
TextBox _ctl = (TextBox)fvOrders.FindControl(sControlName);
if (_ctl == null)
{
throw new Exception("GetTextFieldValue: could not find " + sControlName + " control!");
}
sFieldValue = _ctl.Text.Trim();
return sFieldValue;
}
/// <summary>
/// Returns the string value for the specified textbox control in the FormView
/// </summary>
/// <param name="sControlName"></param>
/// <returns></returns>
private string GetDropDownListFieldValue(string sControlName)
{
string sFieldValue = string.Empty;
DropDownList _ctl = (DropDownList)fvOrders.FindControl(sControlName);
if (_ctl == null)
{
throw new Exception("GetDropDownListFieldValue: could not find " + sControlName + " control!");
}
sFieldValue = _ctl.SelectedValue;
return sFieldValue;
}
/// <summary>
/// Returns the bool value for the specified checkbox control in the FormView
/// </summary>
/// <param name="sControlName"></param>
/// <returns></returns>
private bool GetCheckBoxFieldValue(string sControlName)
{
bool bFieldValue = false;
CheckBox _ctl = (CheckBox)fvOrders.FindControl(sControlName);
if (_ctl == null)
{
throw new Exception("GetCheckBoxFieldValue: could not find " + sControlName + " control!");
}
bFieldValue = _ctl.Checked;
return bFieldValue;
}
#endregion
#region General Control Event Handlers / Members
/// <summary>
/// Generic page-load event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
}
}
/// <summary>
/// Switch the controls into insert-record mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnAddNewOrders_Click(object sender, EventArgs e)
{
if (ForceEditsInFormView == true)
{
fvOrders.ChangeMode(FormViewMode.Insert);
fvOrders.DataSource = null;
fvOrders.DataBind();
SetBehavior(ViewMode.FormView);
}
else
{
GridViewEditControlsVisible = true;
BindDataViews();
}
}
/// <summary>
/// Insert a new record into the database table and exit out of insert mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnGridInsertOrders_Click(object sender, EventArgs e)
{
CIF.Business.Northwind.Orders _Orders = new CIF.Business.Northwind.Orders();
CIF.Business.Northwind.OrdersItem _OrdersItem = new CIF.Business.Northwind.OrdersItem();
GridViewRow gvrInsertRow = gvOrders.FooterRow;
_OrdersItem.CustomerID = (GetGridViewRowTextValue(gvrInsertRow, "txtInsertCustomerID") != string.Empty) ? GetGridViewRowTextValue(gvrInsertRow, "txtInsertCustomerID") : "HILAA";
_OrdersItem.EmployeeID = (GetGridViewRowTextValue(gvrInsertRow, "txtInsertEmployeeID") != string.Empty) ? Convert.ToInt32(GetGridViewRowTextValue(gvrInsertRow, "txtInsertEmployeeID")) : 1;
_OrdersItem.OrderDate = (GetGridViewRowTextValue(gvrInsertRow, "txtInsertOrderDate") != string.Empty) ? Convert.ToDateTime(GetGridViewRowTextValue(gvrInsertRow, "txtInsertOrderDate")) : System.DateTime.Now;
_OrdersItem.RequiredDate = (GetGridViewRowTextValue(gvrInsertRow, "txtInsertRequiredDate") != string.Empty) ? Convert.ToDateTime(GetGridViewRowTextValue(gvrInsertRow, "txtInsertRequiredDate")) : System.DateTime.Now;
_OrdersItem.ShippedDate = (GetGridViewRowTextValue(gvrInsertRow, "txtInsertShippedDate") != string.Empty) ? Convert.ToDateTime(GetGridViewRowTextValue(gvrInsertRow, "txtInsertShippedDate")) : System.DateTime.Now;
_OrdersItem.ShipVia = (GetGridViewRowTextValue(gvrInsertRow, "txtInsertShipVia") != string.Empty) ? Convert.ToInt32(GetGridViewRowTextValue(gvrInsertRow, "txtInsertShipVia")) : 1;
_OrdersItem.ShipName = GetGridViewRowTextValue(gvrInsertRow, "txtInsertShipName");
_OrdersItem.ShipAddress = GetGridViewRowTextValue(gvrInsertRow, "txtInsertShipAddress");
_OrdersItem.ShipCity = GetGridViewRowTextValue(gvrInsertRow, "txtInsertShipCity");
_OrdersItem.ShipRegion = GetGridViewRowTextValue(gvrInsertRow, "txtInsertShipRegion");
_OrdersItem.ShipPostalCode = GetGridViewRowTextValue(gvrInsertRow, "txtInsertShipPostalCode");
_OrdersItem.ShipCountry = GetGridViewRowTextValue(gvrInsertRow, "txtInsertShipCountry");
_OrdersItem.IsNew = true;
_Orders.OrdersItem = _OrdersItem;
_Orders.Save();
GridViewEditControlsVisible = false;
BindDataViews();
}
/// <summary>
/// Exit out of insert mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnGridCancelOrders_Click(object sender, EventArgs e)
{
GridViewEditControlsVisible = false;
BindDataViews();
}
/// <summary>
/// Switch
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnReturnToOrdersList_Click(object sender, EventArgs e)
{
BindDataViews();
SetBehavior(ViewMode.GridView);
}
#endregion
#region GridView Event Handlers / Members
/// <summary>
/// Event that occurs when a data record is bound to a row in the gridview
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
// don't uncomment unless needed
//int _OrderID = GetGridViewRowOrderID(e.Row);
}
/// <summary>
/// Event that occurs when a command is issued from a row in the gridview
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvOrders_RowCommand(object sender, GridViewCommandEventArgs e)
{
// don't uncomment unless needed
//GridViewRow _row = (GridViewRow)((Control)e.CommandSource);
//int _OrderID = GetGridViewRowOrderID(_row);
/* // route and handle the command
switch (e.CommandName.ToUpper())
{
}
*/
}
/// <summary>
/// When gridview row is selected, displays the record in the FormView; activates editing if allowed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvOrders_SelectedIndexChanged(object sender, EventArgs e)
{
if (gvOrders.SelectedIndex < 0)
return;
fvOrders.ChangeMode(FormViewMode.ReadOnly);
int _OrderID = GetGridViewRowOrderID(gvOrders.SelectedRow);
BindDataViews(_OrderID);
}
/// <summary>
/// Updates gridview display when user changes to another page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvOrders.PageIndex = e.NewPageIndex;
BindDataViews();
}
/// <summary>
/// Cancel the edits and rebind the gridview when user cancels out of edit mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvOrders_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvOrders.EditIndex = -1;
BindDataViews();
}
/// <summary>
/// Set the gridview row to edit and rebind the gridview into edit mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvOrders_RowEditing(object sender, GridViewEditEventArgs e)
{
gvOrders.EditIndex = e.NewEditIndex;
BindDataViews();
}
/// <summary>
/// Delete the record specified in the gridview and rebind
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvOrders_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
CIF.Business.Northwind.Orders _Orders = new CIF.Business.Northwind.Orders();
GridViewRow gvrEditRow = gvOrders.Rows[e.RowIndex];
int _OrderID = GetGridViewRowOrderID(gvrEditRow);
_Orders.Delete(_OrderID);
BindDataViews();
}
/// <summary>
/// Update the record according to the gridview's edit controls for the currently selected row
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvOrders_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
CIF.Business.Northwind.Orders _Orders = new CIF.Business.Northwind.Orders();
CIF.Business.Northwind.OrdersItem _OrdersItem = new CIF.Business.Northwind.OrdersItem();
GridViewRow gvrEditRow = gvOrders.Rows[e.RowIndex];
_OrdersItem.OrderID = GetGridViewRowOrderID(gvrEditRow);
_OrdersItem.CustomerID = (GetGridViewRowTextValue(gvrEditRow, "txtEditCustomerID") != string.Empty) ? GetGridViewRowTextValue(gvrEditRow, "txtEditCustomerID") : "HILAA";
_OrdersItem.EmployeeID = (GetGridViewRowTextValue(gvrEditRow, "txtEditEmployeeID") != string.Empty) ? Convert.ToInt32(GetGridViewRowTextValue(gvrEditRow, "txtEditEmployeeID")) : 1;
_OrdersItem.OrderDate = (GetGridViewRowTextValue(gvrEditRow, "txtEditOrderDate") != string.Empty) ? Convert.ToDateTime(GetGridViewRowTextValue(gvrEditRow, "txtEditOrderDate")) : System.DateTime.Now;
_OrdersItem.RequiredDate = (GetGridViewRowTextValue(gvrEditRow, "txtEditRequiredDate") != string.Empty) ? Convert.ToDateTime(GetGridViewRowTextValue(gvrEditRow, "txtEditRequiredDate")) : System.DateTime.Now;
_OrdersItem.ShippedDate = (GetGridViewRowTextValue(gvrEditRow, "txtEditShippedDate") != string.Empty) ? Convert.ToDateTime(GetGridViewRowTextValue(gvrEditRow, "txtEditShippedDate")) : System.DateTime.Now;
_OrdersItem.ShipVia = (GetGridViewRowTextValue(gvrEditRow, "txtEditShipVia") != string.Empty) ? Convert.ToInt32(GetGridViewRowTextValue(gvrEditRow, "txtEditShipVia")) : 1;
_OrdersItem.ShipName = GetGridViewRowTextValue(gvrEditRow, "txtEditShipName");
_OrdersItem.ShipAddress = GetGridViewRowTextValue(gvrEditRow, "txtEditShipAddress");
_OrdersItem.ShipCity = GetGridViewRowTextValue(gvrEditRow, "txtEditShipCity");
_OrdersItem.ShipRegion = GetGridViewRowTextValue(gvrEditRow, "txtEditShipRegion");
_OrdersItem.ShipPostalCode = GetGridViewRowTextValue(gvrEditRow, "txtEditShipPostalCode");
_OrdersItem.ShipCountry = GetGridViewRowTextValue(gvrEditRow, "txtEditShipCountry");
_OrdersItem.IsNew = false;
_Orders.OrdersItem = _OrdersItem;
_Orders.Save();
gvOrders.EditIndex = -1;
BindDataViews();
}
/// <summary>
/// Sort / rebind the gridview according to the column of the gridview that was click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvOrders_Sorting(object sender, GridViewSortEventArgs e)
{
if (SortExpression != e.SortExpression)
{
SortExpression = e.SortExpression;
SortDirection = "ASC";
}
else
{
if (SortDirection == "ASC")
{
SortDirection = "DESC";
}
else
{
SortDirection = "ASC";
}
}
gvOrders.PageIndex = 0;
BindDataViews();
}
#endregion
#region FormView Event Handlers / Members
/// <summary>
/// Sets the FormView into Insert mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnFormNewOrders_Click(object sender, EventArgs e)
{
fvOrders.ChangeMode(FormViewMode.Insert);
fvOrders.DataSource = null;
fvOrders.DataBind();
SetBehavior(ViewMode.FormView);
}
/// <summary>
/// Sets the FormView into Edit mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnFormEditOrders_Click(object sender, EventArgs e)
{
fvOrders.ChangeMode(FormViewMode.Edit);
int _OrderID = GetFormViewOrderID();
BindDataViews(_OrderID);
}
/// <summary>
/// Deletes the active FormView item
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnFormDeleteOrders_Click(object sender, EventArgs e)
{
fvOrders.DeleteItem();
}
/// <summary>
/// Insert a new record into the database table and exit out of insert mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnFormInsertOrders_Click(object sender, EventArgs e)
{
fvOrders.InsertItem(false);
}
/// <summary>
/// Exit out of insert mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnFormCancelInsertOrders_Click(object sender, EventArgs e)
{
SetBehavior(ViewMode.GridView);
}
/// <summary>
/// Exit out of update mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnFormCancelUpdateOrders_Click(object sender, EventArgs e)
{
SetBehavior(ViewMode.GridView);
}
/// <summary>
/// Updates the active item in the FormView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnFormUpdateOrders_Click(object sender, EventArgs e)
{
fvOrders.UpdateItem(false);
}
/// <summary>
/// Deletes the record currently in the FormView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void fvOrders_ItemDeleting(object sender, FormViewDeleteEventArgs e)
{
CIF.Business.Northwind.Orders _Orders = new CIF.Business.Northwind.Orders();
int _OrderID = GetFormViewOrderID();
_Orders.Delete(_OrderID);
BindDataViews();
SetBehavior(ViewMode.GridView);
}
/// <summary>
/// Inserts a new record based upon the values in the FormView controls
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void fvOrders_ItemInserting(object sender, FormViewInsertEventArgs e)
{
CIF.Business.Northwind.Orders _Orders = new CIF.Business.Northwind.Orders();
CIF.Business.Northwind.OrdersItem _OrdersItem = new CIF.Business.Northwind.OrdersItem();
_OrdersItem.CustomerID = (GetTextFieldValue("txtInsertCustomerID") != string.Empty) ? GetTextFieldValue("txtInsertCustomerID") : "HILAA";
_OrdersItem.EmployeeID = (GetTextFieldValue("txtInsertEmployeeID") != string.Empty) ? Convert.ToInt32(GetTextFieldValue("txtInsertEmployeeID")) : 1;
_OrdersItem.OrderDate = (GetTextFieldValue("txtInsertOrderDate") != string.Empty) ? Convert.ToDateTime(GetTextFieldValue("txtInsertOrderDate")) : System.DateTime.Now;
_OrdersItem.RequiredDate = (GetTextFieldValue("txtInsertRequiredDate") != string.Empty) ? Convert.ToDateTime(GetTextFieldValue("txtInsertRequiredDate")) : System.DateTime.Now;
_OrdersItem.ShippedDate = (GetTextFieldValue("txtInsertShippedDate") != string.Empty) ? Convert.ToDateTime(GetTextFieldValue("txtInsertShippedDate")) : System.DateTime.Now;
_OrdersItem.ShipVia = (GetTextFieldValue("txtInsertShipVia") != string.Empty) ? Convert.ToInt32(GetTextFieldValue("txtInsertShipVia")) : 1;
_OrdersItem.ShipName = GetTextFieldValue("txtInsertShipName");
_OrdersItem.ShipAddress = GetTextFieldValue("txtInsertShipAddress");
_OrdersItem.ShipCity = GetTextFieldValue("txtInsertShipCity");
_OrdersItem.ShipRegion = GetTextFieldValue("txtInsertShipRegion");
_OrdersItem.ShipPostalCode = GetTextFieldValue("txtInsertShipPostalCode");
_OrdersItem.ShipCountry = GetTextFieldValue("txtInsertShipCountry");
_OrdersItem.IsNew = true;
_Orders.OrdersItem = _OrdersItem;
_Orders.Save();
BindDataViews();
SetBehavior(ViewMode.GridView);
}
/// <summary>
/// Updates an existing record based upon the values in the FormView controls
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void fvOrders_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
CIF.Business.Northwind.Orders _Orders = new CIF.Business.Northwind.Orders();
CIF.Business.Northwind.OrdersItem _OrdersItem = new CIF.Business.Northwind.OrdersItem();
_OrdersItem.OrderID = GetFormViewOrderID();
_OrdersItem.CustomerID = (GetTextFieldValue("txtEditCustomerID") != string.Empty) ? GetTextFieldValue("txtEditCustomerID") : "HILAA";
_OrdersItem.EmployeeID = (GetTextFieldValue("txtEditEmployeeID") != string.Empty) ? Convert.ToInt32(GetTextFieldValue("txtEditEmployeeID")) : 1;
_OrdersItem.OrderDate = (GetTextFieldValue("txtEditOrderDate") != string.Empty) ? Convert.ToDateTime(GetTextFieldValue("txtEditOrderDate")) : System.DateTime.Now;
_OrdersItem.RequiredDate = (GetTextFieldValue("txtEditRequiredDate") != string.Empty) ? Convert.ToDateTime(GetTextFieldValue("txtEditRequiredDate")) : System.DateTime.Now;
_OrdersItem.ShippedDate = (GetTextFieldValue("txtEditShippedDate") != string.Empty) ? Convert.ToDateTime(GetTextFieldValue("txtEditShippedDate")) : System.DateTime.Now;
_OrdersItem.ShipVia = (GetTextFieldValue("txtEditShipVia") != string.Empty) ? Convert.ToInt32(GetTextFieldValue("txtEditShipVia")) : 1;
_OrdersItem.ShipName = GetTextFieldValue("txtEditShipName");
_OrdersItem.ShipAddress = GetTextFieldValue("txtEditShipAddress");
_OrdersItem.ShipCity = GetTextFieldValue("txtEditShipCity");
_OrdersItem.ShipRegion = GetTextFieldValue("txtEditShipRegion");
_OrdersItem.ShipPostalCode = GetTextFieldValue("txtEditShipPostalCode");
_OrdersItem.ShipCountry = GetTextFieldValue("txtEditShipCountry");
_OrdersItem.IsNew = false;
_Orders.OrdersItem = _OrdersItem;
_Orders.Save();
BindDataViews();
SetBehavior(ViewMode.GridView);
}
/// <summary>
/// Toggles the view and binding behavior when the FormView mode changes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void fvOrders_ModeChanging(object sender, FormViewModeEventArgs e)
{
if (e.CancelingEdit == false)
{
fvOrders.ChangeMode(e.NewMode);
int _OrderID = GetGridViewRowOrderID(gvOrders.SelectedRow);
BindDataViews(_OrderID);
}
else
{
SetBehavior(ViewMode.GridView);
}
}
/// <summary>
/// Databind event for the record being bound to the FormView control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void fvOrders_DataBound(object sender, EventArgs e)
{
if (fvOrders.DataItem == null)
return;
}
#endregion
}
}