public FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable GetSort()
        {
            fmTypeViewSort = (FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable)Session["fmTypeViewSortDummy"];
            if (fmTypeViewSort == null)
            {
                fmTypeViewSort = (FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable)Session["fmTypeViewSort"];
            }

            return fmTypeViewSort;
        }
        private void StepSortProcessGrid()
        {
            FmViewTDS dataSet = new FmViewTDS();
            dataSet.LFS_FM_TYPE_VIEW_SORT.Merge(fmTypeViewSort, true);
            FmTypeViewSort model = new FmTypeViewSort(dataSet);

            // Update rows
            if (Session["fmTypeViewSortDummy"] == null)
            {
                foreach (GridViewRow row in grdSort.Rows)
                {
                    int sortId = int.Parse(grdSort.DataKeys[row.RowIndex].Values["SortID"].ToString());
                    bool selected = ((CheckBox)row.FindControl("cbxSelected")).Checked;

                    int order = 0;
                    try
                    {
                        order = int.Parse(((DropDownList)row.FindControl("ddlOrder")).SelectedValue);
                    }
                    catch
                    {
                        order = 0;
                    }

                    model.Update(hdfFmType.Value, int.Parse(hdfCompanyId.Value), sortId, order, selected);
                }

                model.Table.AcceptChanges();

                fmTypeViewSort = (FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable)model.Table;
                Session["fmTypeViewSort"] = fmTypeViewSort;
            }
        }
        protected void SortEmptyFix(GridView grdSort)
        {
            if (grdSort.Rows.Count == 0)
            {
                FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable dt = new FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable();
                dt.AddLFS_FM_TYPE_VIEW_SORTRow("", 0, 0, "", false, true, "", "", false, 0);
                Session["fmTypeViewSortDummy"] = dt;

                grdSort.DataBind();
            }

            // Normally executes at all postbacks
            if (grdSort.Rows.Count == 1)
            {
                FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable dt = (FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable)Session["fmTypeViewSortDummy"];
                if (dt != null)
                {
                    // Hide row
                    grdSort.Rows[0].Visible = false;
                    grdSort.Rows[0].Controls.Clear();
                }
            }
        }
        private bool StepLogicNext()
        {
            Page.Validate("Logic");
            if (Page.IsValid)
            {
                // Load
                FmViewTDS dataSet = new FmViewTDS();
                dataSet.LFS_FM_TYPE_VIEW_SORT.Merge(fmTypeViewSort, true);

                FmTypeViewSort model = new FmTypeViewSort(dataSet);

                if (dataSet.LFS_FM_TYPE_VIEW_SORT.Rows.Count <= 0)
                {
                    model.LoadByFmTypeInView(hdfFmType.Value, int.Parse(hdfCompanyId.Value), true);
                }

                // Store tables
                fmTypeViewSort = (FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable)model.Table;
                Session["fmTypeViewSort"] = fmTypeViewSort;
                return true;
            }
            else
            {
                return false;
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["fm_type"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in view_add.aspx");
                }

                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_VIEWS_ADD"])))
                {
                    if ((string)Request.QueryString["fm_type"] == "Services")
                    {
                        if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_VIEW"])))
                        {
                            Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                        }
                    }

                    if ((string)Request.QueryString["fm_type"] == "Units")
                    {
                        if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_UNITS_VIEW"])))
                        {
                            Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                        }
                    }
                }

                // Tag page
                hdfFmType.Value = (string)Request.QueryString["fm_type"];
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfUpdate.Value = "no";

                ViewState["columnsToDisplayPageIndex"] = 0;
                ViewState["sortPageIndex"] = 0;

                Session.Remove("fmTypeViewDisplay");
                Session.Remove("fmTypeViewDisplayDummy");
                Session.Remove("fmTypeViewSort");
                Session.Remove("fmTypeViewSortDummy");
                Session.Remove("fmViewDisplayTemp");
                Session.Remove("fmViewSortTemp");
                Session.Remove("fmViewConditionNew");
                Session.Remove("fmViewConditionNewDummy");
                Session.Remove("categoriesTDSForAddView");

                FmViewGateway fmViewGateway = new FmViewGateway();
                hdfViewId.Value = (fmViewGateway.GetLastViewId() + 1).ToString().Trim();

                // Prepare initial data
                // ... For view type
                FmViewTypeList fmViewTypeList = new FmViewTypeList(new DataSet());
                fmViewTypeList.LoadAndAddItem("(Select a type)", Int32.Parse(hdfCompanyId.Value));
                ddlType.DataSource = fmViewTypeList.Table;
                ddlType.DataValueField = "Type";
                ddlType.DataTextField = "Type";
                ddlType.DataBind();
                ddlType.SelectedIndex = 0;

                // ... Global Views check
                if (!Convert.ToBoolean(Session["sgLFS_GLOBALVIEWS_ADD"]))
                {
                    ddlType.Items.Remove("Global");
                }

                // If coming from
                // ... services_navigator.aspx, services_navigator2.aspx, units_navigator.aspx or units_navigator2.aspx
                if ((Request.QueryString["source_page"] == "services_navigator.aspx") || (Request.QueryString["source_page"] == "services_navigator2.aspx") || (Request.QueryString["source_page"] == "units_navigator.aspx") || (Request.QueryString["source_page"] == "unit_navigator2.aspx"))
                {
                    // ... For Grids
                    fmViewTDS = new FmViewTDS();

                    fmTypeViewDisplay = new FmViewTDS.LFS_FM_TYPE_VIEW_DISPLAYDataTable();
                    fmTypeViewSort = new FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable();
                    fmViewDisplayTemp = new FmViewTDS.FmViewDisplayTempDataTable();
                    fmViewSortTemp = new FmViewTDS.FmViewSortTempDataTable();
                    fmViewConditionNew = new FmViewTDS.FmViewConditionNewDataTable();

                    // ... for Categories
                    categoriesTDS = new CategoriesTDS();

                    // ... Store datasets
                    Session["fmViewTDS"] = fmViewTDS;

                    Session["fmTypeViewDisplay"] = fmTypeViewDisplay;
                    Session["fmTypeViewSort"] = fmTypeViewSort;
                    Session["fmViewDisplayTemp"] = fmViewDisplayTemp;
                    Session["fmViewSortTemp"] = fmViewSortTemp;
                    Session["fmViewConditionNew"] = fmViewConditionNew;

                    Session["categoriesTDSForAddView"] = categoriesTDS;
                }

                // StepSection1In
                wzViews.ActiveStepIndex = 0;
                StepBeginIn();
            }
            else
            {
                // Restore datasets
                fmViewTDS = (FmViewTDS)Session["fmViewTDS"];

                fmTypeViewDisplay = (FmViewTDS.LFS_FM_TYPE_VIEW_DISPLAYDataTable)Session["fmTypeViewDisplay"];
                fmTypeViewSort = (FmViewTDS.LFS_FM_TYPE_VIEW_SORTDataTable)Session["fmTypeViewSort"];
                fmViewDisplayTemp = (FmViewTDS.FmViewDisplayTempDataTable)Session["fmViewDisplayTemp"];
                fmViewSortTemp = (FmViewTDS.FmViewSortTempDataTable)Session["fmViewSortTemp"];
                fmViewConditionNew = (FmViewTDS.FmViewConditionNewDataTable)Session["fmViewConditionNew"];

                categoriesTDS = (CategoriesTDS)Session["categoriesTDSForAddView"];
            }

            // Control for postback
            hdfTag.Value = DateTime.Now.ToLongTimeString();
        }