Zdrojové kódy pro vývojáře.
Přeskočit odkazy pro navigaci Top 10 přispěvatelů
UživatelČlánky
codeshare45
sochor1
stoupa1
tomas.oplt15
Článek: Framework pro dynamické načítání/ukládání dat v prostředí ASP .NET
Špatný Super
Autor:
Vytvořeno:
Popularita:

Tento framework je určen převážně pro databázové ASP .NET aplikace, které vyžadují mnoho databázových operací.
V těchto případech je pak vhodné některé operace automatizovat a nepsat pro každou stránku logiku pro načítání a ukládání dat.
Tento framework zajistí automatické načtení dat do gridu pomocí definovaného SQL příkazu a zároveň je schopný změny zapsat zpět do databáze.

Co je nutné definovat?
SQL příkaz pro naplnění gridu
U každého prvku na formuláři je nutné specifikovat vlastnosti DataGrid a DataField. 
Pokud nastane události RowChanged všechny ovládací prvky na formuláři automaticky načtou data z databáze.

    public class ControlLoader
    {
        // Private variables
        private System.Web.UI.Page m_Page = null;
        private DbHelper m_db = null;
        private System.Collections.Generic.List m_lstControls = null;
        private System.Collections.Generic.List m_lstDataGrids = null;
        private int m_intPKValue = -1;
        private StateBag m_vwState = null;
        private ConfigurationManager m_cfg;


        ///


        /// Constructor
        ///
        /// Web page
        /// Connection name which is defined in Web.Config file
        /// ViewState of page - because ViewState is protected it is not possible to ask for it from external class
        public ControlLoader(System.Web.UI.Page page, string strConnectionStringName, StateBag vwState)
        {
            string strConnextionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[strConnectionStringName].ConnectionString;
            m_Page = page;
            m_vwState = vwState;
            m_db = new DbHelper(DatabaseProvider.SQLSERVER, strConnextionString);
            CfgMgr = new ConfigurationManager(GetFormNameFromRequst(HttpContext.Current.Request.CurrentExecutionFilePath.ToString()));
        }

        ///


        /// Constructor
        ///
        /// Connection name which is defined in Web.Config file
        public ControlLoader(string strConnectionStringName)
        {
            string strConnextionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[strConnectionStringName].ConnectionString;
            m_db = new DbHelper(DatabaseProvider.SQLSERVER, strConnextionString);
        }
   

        ///


        /// Returns only the Page name without path and extension
        ///
        /// URL path of asp page
        ///
        private string GetFormNameFromRequst(string strRequestFilePath)
        {
            int iPos = strRequestFilePath.LastIndexOf("/");
            int iExtensionLen = 1;
            if (iPos == -1) iPos = 0;
            if (strRequestFilePath.IndexOf("aspx") > 1)
                iExtensionLen = 6;
           
            string strFormName = strRequestFilePath.Substring(iPos+1,strRequestFilePath.Length - iPos - iExtensionLen);
            return strFormName;
        }
        [System.ComponentModel.Browsable(false)]
        public ConfigurationManager CfgMgr
        {
            get
            {
                return m_cfg;
            }
            set
            {
                m_cfg = value;
            }
        }

        ///


        /// Page for which belogngs this instance of controlLoader
        ///
        public System.Web.UI.Page PAGE
        {
            get
            {
                return m_Page;
            }
            set
            {
                m_Page = value;
            }
        }

        ///


        /// Page for which belogngs this instance of controlLoader
        ///
        public StateBag VIEWSTATE
        {
            get
            {
                return m_vwState;
            }
            set
            {
                m_vwState = value;
            }
        }

        ///


        /// Instance of DBHelper class
        ///
        public DbHelper DBHelper
        {
            get
            {
                return m_db;
            }
            set
            {
                m_db = value;
            }
        }

        ///


        /// Get saved value from ViewState
        ///
        ///
        ///
        ///
        ///
        public string GetViewStateValue(string strControlName, PageValueNameEnum enmValueName, string strDefaultValue)
        {
            if (HttpContext.Current.Session[strControlName + "|" + Convert.ToString((int)enmValueName)] == null)
                return strDefaultValue;
            else
                return HttpContext.Current.Session[strControlName + "|" + Convert.ToString((int)enmValueName)].ToString();
        }

        ///


        /// Get saved value from ViewState
        ///
        ///
        ///
        ///
        public object GetViewStateValue(string strControlName, PageValueNameEnum enmValueName)
        {
            return HttpContext.Current.Session[strControlName + "|" + Convert.ToString((int)enmValueName)];
        }

        ///


        /// Save value - viewState sometimes not functionali we are using Session
        ///
        ///
        ///
        ///
        public void SetViewStateValue(string strControlName, PageValueNameEnum enmValueName, string strValue)
        {
            HttpContext.Current.Session[strControlName + "|" + Convert.ToString((int)enmValueName)] = strValue;
        }

        ///


        /// Save value - viewState sometimes not functionali we are using Session
        ///
        ///
        ///
        ///
        public void SetViewStateValue(string strControlName, PageValueNameEnum enmValueName, object objValue)
        {
            HttpContext.Current.Session[strControlName + "|" + Convert.ToString((int)enmValueName)] = objValue;
        }

     
        ///


        /// In method WebPage.Load - new instance of ControlHelper should be created and this method should be called
        ///
        public void LoadForm()
        {
            try
            {
                m_lstControls = new System.Collections.Generic.List();
                m_lstDataGrids = new System.Collections.Generic.List();

                foreach (System.Web.UI.Control ctrl in this.m_Page.Controls)
                {
                    ProcessControls(ctrl, false);
                }
                if (this.m_Page.IsPostBack == false)
                {
                    foreach (BL.BLGrid grd in this.m_lstDataGrids)
                    {
                        grd._Init(this);
                        if (grd.DisplayLayout.ActiveRow != null)
                        {
                            this.OnGrdRowChange(grd, grd.DisplayLayout.ActiveRow);
                        }
                    }
                }
                else
                {
                  
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.ShowException(ex, "LoadForm");
            }
        }

        private void ProcessControlsAfter()
        {
            try
            {
                // Necessary to have here!
                foreach (System.Web.UI.Control ctrlGrd in this.m_lstControls)
                {
                    if (ctrlGrd is BL.BLGrid)
                    {
                        BL.BLGrid grd = ctrlGrd as BL.BLGrid;
                        if (this.m_lstDataGrids.Contains(grd) == false)
                        {
                            this.m_lstDataGrids.Add(grd);
                            grd.CtrlLoader = this;
                            grd._Init(this);

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.ShowException(ex, "LoadForm");
            }
        }

        private void ProcessControls(System.Web.UI.Control ctrl, bool bIsPostBack)
        {
            foreach (System.Web.UI.Control ctrl2 in ctrl.Controls)
            {
                ProcessControls(ctrl2, bIsPostBack);

                if (ctrl2 is BLGrid)
                {
                    m_lstControls.Add(ctrl2);
                    BLGrid grd = ctrl2 as BLGrid;
                    m_lstDataGrids.Add(grd);
                    grd.CtrlLoader = this;
                    if (this.m_Page.IsPostBack == false || grd.DataSource == null)
                    {
                        grd.InitializeVariables();
                    }
                    else
                    {
                        grd._OnPostback(this);
                    }
                    if (grd.HandleRowChange == true)
                        grd.ActiveRowChange += new Infragistics.WebUI.UltraWebGrid.ActiveRowChangeEventHandler(OnDataGrid_ActiveRowChange);
                }
                else if (ctrl2 is BLGridNet)
                {
                    BLGridNet grdNet = ctrl2 as BLGridNet;
                    m_lstControls.Add(grdNet);
                    if (this.m_Page.IsPostBack == false)
                        grdNet._Init(this);
                    else
                        grdNet.CtrlLoader = this;
                }
                else if (ctrl2 is BLTree)
                {
                    m_lstControls.Add(ctrl2);
                    BLTree tree = ctrl2 as BLTree;
                    tree.NodeClicked += new Infragistics.WebUI.UltraWebNavigator.NodeClickedEventHandler(OnTree_NodeClicked);
                    if (this.m_Page.IsPostBack == false)
                        tree._Init(this);
                    else
                        tree.CtrlLoader = this;
                }
                else if (ctrl2 is BLComboBox )
                {
                    m_lstControls.Add(ctrl2);
                    BLComboBox cbo = ctrl2 as BLComboBox;
                    if (this.m_Page.IsPostBack == false )
                        cbo._Init(this);
                    else
                        cbo.CtrlLoader = this;
                }
                else if (ctrl2 is BLCheckBox)
                {
                    m_lstControls.Add(ctrl2);
                }
                else if (ctrl2 is BLComboBoxFilter)
                {
                    m_lstControls.Add(ctrl2);
                    BLComboBoxFilter cbo = ctrl2 as BLComboBoxFilter;
                    if (this.m_Page.IsPostBack == false || ( cbo.SelectedIndex < 1 && cbo.Items.Count < 1 ) )
                        cbo._Init(this);
                    else
                        cbo.CtrlLoader = this;

                    cbo.SelectedIndexChanged += new EventHandler(OnCboFilterSelectedIndexChanged);
                }
                else if (ctrl2 is BLComboBoxInfra)
                {
                    m_lstControls.Add(ctrl2);
                    BLComboBoxInfra cbo = ctrl2 as BLComboBoxInfra;
                    if (this.m_Page.IsPostBack == false || (cbo.SelectedIndex < 1 && cbo.Rows.Count < 1))
                        cbo._Init(this);
                    else
                        cbo.CtrlLoader = this;
                }
                else if (ctrl2 is BLTextBox)
                {
                    m_lstControls.Add(ctrl2);
                }
                else if (ctrl2 is BLTextEditor)
                {
                    m_lstControls.Add(ctrl2);
                }
                else if (ctrl2 is BLMaskEdit)
                {
                    m_lstControls.Add(ctrl2);
                }
                else if (ctrl2 is BLButton)
                {
                    BLButton btn = ctrl2 as BLButton;
                    if (this.m_Page.IsPostBack == false )
                    {
                        btn._Init(this);
                    }
                    else
                    {
                        btn._OnPostBack(this);
                    }
                    if (btn.ButtonFunction != ButtonFunctionEnum.OpenForm)
                    {
                        btn.Click += new EventHandler(OnBtnClick);
                    }
                    m_lstControls.Add(ctrl2);
                }
                else if (ctrl2 is BLButtonInfra)
                {
                    BLButtonInfra btn = ctrl2 as BLButtonInfra;
                    if (this.m_Page.IsPostBack == false)
                    {
                        btn._Init(this);
                    }
                    else
                    {
                        btn.CtrlLoader = this;
                    }
                    if (btn.ButtonFunction != ButtonFunctionEnum.OpenForm)
                    {
                        btn.Click += new  Infragistics.WebUI.WebDataInput.ClickHandler(OnBtnClickInfra);
                    }
                    m_lstControls.Add(ctrl2);
                }
                else if (ctrl2 is BLSingleSearch)
                {
                    BLSingleSearch search = ctrl2 as BLSingleSearch;
                    search.Searching += new BLSingleSearch.SearchingDelegate(OnSingleSearchControlSearching);
                    m_lstControls.Add(ctrl2);
                }
                else if (ctrl2 is BLMultiSearch)
                {
                    BLMultiSearch search = ctrl2 as BLMultiSearch;
                    search.Searching += new BLMultiSearch.SearchingDelegate(OnMultiSearchControlSearching);
                    m_lstControls.Add(ctrl2);
                }
                else if (ctrl2 is BLPositionControl)
                {
                    BLPositionControl pos = ctrl2 as BLPositionControl;
                    pos.Moving += new BLPositionControl.MovingDelegate(OnPositionControlMoving);
                    m_lstControls.Add(ctrl2);
                }
            }
        }

        void OnCboFilterSelectedIndexChanged(object sender, EventArgs e)
        {
            BLComboBoxFilter cboF = sender as BLComboBoxFilter;
            BLGrid grd = this.GetDataGrid(cboF.DataGrid);
            if (grd == null)
                ExceptionManager.ShowException("DataGrid not found. DataGrid: " + cboF.DataGrid);
            string strVal = "";
            if (cboF.SelectedIndex == 0 || cboF.SelectedIndex == -1)
            {
                strVal = grd.RecordsetSQL;
            }
            else
            {
                strVal = cboF.SelectedValue;
            }
           
            DataGridReloadEventArgs ee = new DataGridReloadEventArgs();
            ee.SQL = strVal;
            grd.ReloadData(ee);

        }

        void OnPositionControlMoving(object sender, MovingEventArgs e)
        {
            BLPositionControl pos = sender as BLPositionControl;
            BL.BLGrid grd = this.GetDataGrid(pos.DataGrid);
            if (grd == null) return;
            switch (e.enmMovingDirection)
            {
                case MovingDirectionEnum.First:
                    grd.MoveFirst();
                    break;
                case MovingDirectionEnum.Last:
                    grd.MoveLast();
                    break;
                case MovingDirectionEnum.Next:
                    grd.MoveNext();
                    break;
                case MovingDirectionEnum.Prev:
                    grd.MovePrev();
                    break;
            }
        }

       
        private void OnSingleSearchControlSearching(object sender, EventArgs e)
        {
            BLSingleSearch search = sender as BLSingleSearch;
            if (search.DataField == "")
                return;

            BLGrid grd = this.GetDataGrid(search.DataGrid);
            if (grd == null)
                throw new Exception("DataGrid not found: " + search.DataGrid);

            string strSearchText = search.GetSearchText();
            string strMask = search.GetSearchMask();

            bool bIsString = true;

            DBHelper.Connect();
            try
            {
                string strSQLNew = "";

                if (strSearchText == "")
                {
                    strSQLNew = grd.RecordsetSQL;
                }
                else
                {
                    string strSQL = grd.GetCorrectRecordset();
                    strSQL = BL.SQLHelper.GetCondition(strSQL, search.DataField, "-1", "=");
                    DataTable dt = DBHelper.GetDataTable(strSQL);
                    if (dt.Columns[0].DataType != System.Type.GetType("System.String"))
                    {
                        bIsString = false;
                    }

                    if (strMask == "*.*")
                    {
                        strMask = "LIKE";
                    }
                    else if (strMask == "*")
                    {
                        strMask = "BEGINSLIKE";
                    }
                    if ((bIsString == true && strMask != "LIKE") || (bIsString == true && strMask != "BEGINSLIKE"))
                    {
                        //strSearchText = "'" + strSearchText + "'";
                    }
                    strSQLNew = SQLHelper.GetCondition(grd.RecordsetSQL, search.DataField, strSearchText, strMask);
                }
                grd.FilterData(DBHelper, strSQLNew);
                this.OnGrdRowChange(grd, grd.DisplayLayout.ActiveRow,true);
            }
            catch (Exception ex)
            {
                ExceptionManager.ShowException(ex, "search_Searching");
                return;
            }
            finally
            {
                DBHelper.DisConnect();
            }

        }
        private void OnMultiSearchControlSearching(object sender, EventArgs e)
        {
            BLMultiSearch search = sender as BLMultiSearch;
            if (search.GetDataField() == "")
                return;

            BLGrid grd = this.GetDataGrid(search.DataGrid);
            if (grd == null)
                return;

            string strSearchText = search.GetSearchText();
            string strMask = search.GetSearchMask();

            bool bIsString = true;

            DBHelper.Connect();
            try
            {

                if (grd.UpdateTableName != "")
                {
                    string strSQLColType = "SELECT " + search.GetDataField() + " FROM " + grd.UpdateTableName + " WHERE 1=2";
                    DataTable dt = DBHelper.GetDataTable(strSQLColType);
                    if (dt.Columns[0].DataType != System.Type.GetType("System.String"))
                    {
                        bIsString = false;
                    }
                }

                if ( strMask == "*.*" )
                {
                    strMask = "LIKE";
                    strSearchText = "%" + strSearchText + "%";
                }

                if (bIsString == true)
                {
                    strSearchText = "'" + strSearchText + "'";
                }
                string strSQLNew = SQLHelper.GetCondition(grd.RecordsetSQL, search.GetDataField(), strSearchText, strMask);
                grd.FilterData(DBHelper, strSQLNew);
            }
            catch (Exception ex)
            {
                ExceptionManager.ShowException(ex, "search_Searching");
                return;
            }
            finally
            {
                DBHelper.DisConnect();
            }

        }

    
        private void OnDataGrid_ActiveRowChange(object sender, Infragistics.WebUI.UltraWebGrid.RowEventArgs e)
        {
            OnGrdRowChange((BLGrid)sender, e.Row);
        }

       ///


        /// Raises when row is changed on the grid
        ///
        /// Grid which fired an event
        /// activated row
        private void OnGrdRowChange(BLGrid grdSender, Infragistics.WebUI.UltraWebGrid.UltraGridRow gridRrow, bool bSaveViewState)
        {
            m_intPKValue = Const.NULL;


            string strPkValue = "";
            string strDataGridName = grdSender.ID.ToUpper();

            if ( bSaveViewState == true )
                this.SetViewStateValue(grdSender.ID, PageValueNameEnum.GridState, FormState.FormStateNew.ToString());

            if (gridRrow == null)
            {
                this.ClearControls(strDataGridName);
                return;
            }

            try
            {
                strPkValue = gridRrow.Cells[gridRrow.Cells.FromKey(grdSender.PrimaryKey).Column.Index].Text;
            }
            catch (Exception ex)
            {
                ExceptionManager.ShowException(ex, "OnGrdRowChange - GetPrimaryKeyValue from DataGrid: " + strDataGridName);
                return;
            }

            if (Fnc.IsNumeric(strPkValue) == false)
                return;
            m_intPKValue = Fnc.Nvl(strPkValue, Const.NULL);

            if (m_intPKValue == Const.NULL)
            {
                return;
            }

            string strSQL = SQLHelper.GetCondition(grdSender.GetCorrectRecordset(), grdSender.PrimaryKey, m_intPKValue, "=");

            System.Data.DataTable dt = DBHelper.GetDataTable(strSQL);
            if (dt.Rows.Count < 1)
            {
                //throw new Exception("Record not found SQL: " + strSQL);
                this.ClearControls(strDataGridName);
                grdSender.ReloadData(true);
                grdSender.DisplayLayout.SelectedRows.Clear();
                this.SetViewStateValue(grdSender.ID, PageValueNameEnum.GridState, FormState.FormStateNew.ToString());
                return;
            }
            System.Data.DataRow row = dt.Rows[0];

            if (bSaveViewState == true)
                this.SetViewStateValue(grdSender.ID, PageValueNameEnum.GridState, FormState.FormStateEdit.ToString());

            try
            {

                foreach (System.Web.UI.Control ctrl in m_lstControls)
                {
                    if (ctrl is BLButton)
                    {
                        BLButton btn = ctrl as BLButton;
                        if (btn.DataGrid.ToUpper() == strDataGridName)
                        {
                            if (btn.OpenFormAction == OpenFormActionEnum.Edit)
                            {
                                if (btn.OpenFormName.Length > 0)
                                {
                                    btn.OpenFormActionWithPrimaryKeyValue = btn.OpenFormName +"?Action="+btn.OpenFormAction.ToString() + "?PK=" + strPkValue;
                                }
                            }
                        }
                    }
                    else if (ctrl is BLTextBox)
                    {
                        BLTextBox txt = ctrl as BLTextBox;
                        if (txt.DataField != "" && txt.DataGrid.ToUpper() == strDataGridName)
                        {
                            txt.SetDBValue(row[txt.DataField]);
                        }
                    }
                    else if (ctrl is BLTextEditor)
                    {
                        BLTextEditor txt = ctrl as BLTextEditor;
                        if (txt.DataField != "" && txt.DataGrid.ToUpper() == strDataGridName)
                        {
                            txt.SetDBValue(row[txt.DataField]);
                        }
                    }
                    else if (ctrl is BLMaskEdit)
                    {
                        BLMaskEdit mask = ctrl as BLMaskEdit;
                        if (mask.DataField != "" && mask.DataGrid.ToUpper() == strDataGridName)
                        {
                            mask.SetDBValue(row[mask.DataField]);
                        }
                    }

                    else if (ctrl is BLComboBox)
                    {
                        BLComboBox cbo = ctrl as BLComboBox;
                        if (cbo.DataField != "" && cbo.DataGrid.ToUpper() == strDataGridName)
                        {
                            cbo.SetDBValue(row[cbo.DataField]);
                        }
                    }

                    else if (ctrl is BLComboBoxInfra)
                    {
                        BLComboBoxInfra cbo = ctrl as BLComboBoxInfra;
                        if (cbo.DataField != "" && cbo.DataGrid.ToUpper() == strDataGridName)
                        {
                            cbo.SetDBValue(row[cbo.DataField]);
                        }
                    }

                    else if (ctrl is BLCheckBox)
                    {
                        BLCheckBox chk = ctrl as BLCheckBox;
                        if (chk.DataField != "" && chk.DataGrid.ToUpper() == strDataGridName)
                        {
                            chk.SetDBValue(row[chk.DataField]);
                        }
                    }

                    else if (ctrl is BLGrid)
                    {
                        BLGrid grdChild = ctrl as BLGrid;
                        if (grdChild.DataGrid.ToUpper() == strDataGridName)
                        {
                            string strParentGridValue = "";
                            if (grdChild.PrimaryKey.ToUpper() == grdChild.ParentFieldName.ToUpper())
                                strParentGridValue = strPkValue;
                            else
                                strParentGridValue = gridRrow.Cells[gridRrow.Cells.FromKey(grdChild.ParentFieldName).Column.Index].Text;

                            grdChild.FilterData(DBHelper, grdChild.ChildFieldName, strParentGridValue);
                            // if (this.PAGE.IsPostBack == false) 20070812
                            this.OnGrdRowChange(grdChild, grdChild.GetActiveRow());
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                ExceptionManager.ShowException(ex, "OnGrdRowChange");
            }
        }

        ///


        /// Raises when row is changed on the grid
        ///
        /// Grid which fired an event
        /// activated row
        public void OnGrdRowChange(BLGrid grdSender, Infragistics.WebUI.UltraWebGrid.UltraGridRow gridRrow)
        {
            OnGrdRowChange(grdSender, gridRrow, true);
        }

       
        ///


        /// Raised by node click inside Tree
        ///
        ///
        ///
        private void OnTree_NodeClicked(object sender, Infragistics.WebUI.UltraWebNavigator.WebTreeNodeEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
                return;
            BLTree tree = sender as BLTree;
            if (tree.DataGridFilterColumn == "" || tree.DataGrid == "")
                return;

            BLGrid grd = m_Page.FindControl(tree.DataGrid) as BLGrid;
            grd.FilterData(tree.DataGridFilterColumn, Fnc.Nvl(e.Node.DataKey, ""));
        }


       

 

        ///


        /// Raised by button click on the web form
        ///
        ///
        ///
        private void OnBtnClick(object sender, System.EventArgs e)
        {
            BLButton btn = sender as BLButton;
            OnButtonClick(btn.ButtonFunction, btn.DataGrid);
            if (btn.ButtonFunction == ButtonFunctionEnum.New || btn.ButtonFunction == ButtonFunctionEnum.Edit)
            {
                if (btn.ControlNameForFocus != "")
                {
                    System.Web.UI.Control ctrl = this.GetControl(btn.ControlNameForFocus);
                    ctrl.Focus();
                }
            }

            if (btn.ButtonFunction == ButtonFunctionEnum.OpenFormSubmitResult )
            {
                // btn.GenerateSubmitCall();
                if (btn.OpenControlNamesToSave != "")
                {
                    char c = char.Parse(";");
                    string [] strControlsArr = btn.OpenControlNamesToSave.Split(c);
                    foreach (string strControl in strControlsArr)
                    {
                        System.Web.UI.Control ctrl = this.GetControl(strControl);
                        if ( ctrl != null && ctrl is BLTextBox )
                        {
                            BLTextBox txt = ctrl as BLTextBox;
                            this.PAGE.Session[strControl] = txt.Text;
                        }
                        else if (ctrl != null && ctrl is BLMaskEdit)
                        {
                            BLMaskEdit mask = ctrl as BLMaskEdit;
                            this.PAGE.Session[strControl] = mask.Text.Trim();
                        }

                    }
                }
            }
        }


         ///


        /// Raised by button click on the web form
        ///
        ///
        ///
        private void OnBtnClickInfra(object sender, Infragistics.WebUI.WebDataInput.ButtonEventArgs  e)
        {
            BLButtonInfra btn = sender as BLButtonInfra;
            OnButtonClick(btn.ButtonFunction, btn.DataGrid);
            if (btn.ButtonFunction == ButtonFunctionEnum.New || btn.ButtonFunction == ButtonFunctionEnum.Edit)
            {
                if (btn.ControlNameForFocus != "")
                {
                    System.Web.UI.Control ctrl = this.GetControl(btn.ControlNameForFocus);
                    ctrl.Focus();
                }
            }

            if (btn.ButtonFunction == ButtonFunctionEnum.OpenFormSubmitResult )
            {
                btn.GenerateSubmitCall();
            }
        }
        ///


        /// Returns control's collection
        ///
        public System.Collections.Generic.List Controls
        {
            get
            {
                return this.m_lstControls;
            }
        }

        ///


        /// Returns control with specified name
        ///
        ///
        ///
        public System.Web.UI.Control GetControl(string strControlName)
        {
            strControlName = strControlName.ToUpper();
            foreach (System.Web.UI.Control ctrl in this.m_lstControls)
            {
                if (ctrl.ID.ToUpper() == strControlName)
                {
                    return ctrl;
                }
            }
            return null;
        }
        ///
        /// Returns DataGrid with specified name
        ///
        ///
        ///
        public BL.BLGrid GetDataGrid(string strDataGridName)
        {
            strDataGridName = strDataGridName.ToUpper();
            foreach (BL.BLGrid grd in this.m_lstDataGrids)
            {
                if (grd.ID.ToUpper() == strDataGridName)
                {
                    return grd;
                }
            }
            return null;
        }

        ///


        /// Set controls to the default state
        ///
        ///
        public void ClearControls(string strDataGridName)
        {
            strDataGridName = strDataGridName.ToUpper();
            foreach (System.Web.UI.Control ctrl in this.Controls)
            {
                if (ctrl is BLTextBox)
                {
                    BLTextBox txt = ctrl as BLTextBox;
                    if (txt.DataGrid.ToUpper() == strDataGridName)
                        txt.ClearValue();
                }
                if (ctrl is BLTextEditor)
                {
                    BLTextEditor txt = ctrl as BLTextEditor;
                    if (txt.DataGrid.ToUpper() == strDataGridName)
                        txt.ClearValue();
                }
                if (ctrl is BLMaskEdit)
                {
                    BLMaskEdit mask = ctrl as BLMaskEdit;
                    if (mask.DataGrid.ToUpper() == strDataGridName)
                        mask.ClearValue();
                }
                if (ctrl is BLComboBox)
                {
                    BLComboBox cbo = ctrl as BLComboBox;
                    if (cbo.DataGrid.ToUpper() == strDataGridName)
                        cbo.ClearValue();
                }
                if (ctrl is BLComboBoxInfra)
                {
                    BLComboBoxInfra cbo = ctrl as BLComboBoxInfra;
                    if (cbo.DataGrid.ToUpper() == strDataGridName)
                        cbo.ClearValue();
                }
                if (ctrl is BLCheckBox)
                {
                    BLCheckBox chk = ctrl as BLCheckBox;
                    if (chk.DataGrid.ToUpper() == strDataGridName)
                        chk.ClearValue();
                }
                if (ctrl is BLSingleSearch)
                {
                    BLSingleSearch ss = ctrl as BLSingleSearch;
                    //if (ss.DataGrid.ToUpper() == strDataGridName)
                    //    ss.ClearValue();
                }
                if (ctrl is BLGrid)
                {
                    BLGrid grdChild = ctrl as BLGrid;

                    // In case of EnableViewState == false - datagrid lost data by click on button
                    // so it is necessary to refresh it by reloading data
                    Control ctrlVw = grdChild;
                    bool bForceRefresh = false;
                    while (ctrlVw.Parent != null)
                    {
                        if (ctrlVw.EnableViewState == false)
                        {
                            bForceRefresh = true;
                            break;
                        }
                        ctrlVw = ctrlVw.Parent;
                    }
                    if (grdChild.DataGrid.ToUpper() == strDataGridName)
                    {
                        string strSQL = grdChild.RecordsetSQL;
                        strSQL = SQLHelper.GetCondition(strSQL, grdChild.ChildFieldName, "-1", "=");
                        DataGridReloadEventArgs re = new DataGridReloadEventArgs();
                        re.SQL = strSQL;
                        re.DisableRowChange = true;
                        re.ClearDataGrid = true;
                        grdChild.ReloadData(re);
                    }
                    else if (bForceRefresh == true)
                    {
                        DataGridReloadEventArgs re = new DataGridReloadEventArgs();
                        re.DisableRowChange = true;
                        re.SQL = grdChild.LastSQLString;
                        grdChild.ReloadData(re);
                    }
                 
                }
            }
        }

                ///


        /// Process button click (SAVE, ADD)
        ///
        /// Button function
        /// DataGrid name
        public void SendButtonFunction(ButtonFunctionEnum enmButtonFunction, string strDataGrid)
        {
            OnButtonClick(enmButtonFunction, strDataGrid);
        }

        ///


        /// Process button click (SAVE, ADD)
        ///
        /// Button function
        /// DataGrid name
        private void OnButtonClick(ButtonFunctionEnum enmButtonFunction, string strDataGrid)
        {
         

            strDataGrid       = strDataGrid.ToUpper();
            BLGrid grdSender  = this.GetDataGrid(strDataGrid) as BLGrid;
            DataRow row       = null;
            DataTable dt      = null;
            string strPKValue = "";
            try
            {
                if (enmButtonFunction == ButtonFunctionEnum.Cancel)
                {
                    if (grdSender.Rows.Count > 0)
                    {
                        if (grdSender.DisplayLayout.ActiveRow != null)
                        {
                            this.OnGrdRowChange(grdSender, grdSender.DisplayLayout.ActiveRow);
                        }
                        else
                        {
                            grdSender.MoveFirst();
                        }
                    }
                }
                else if (enmButtonFunction == ButtonFunctionEnum.NotSpecified)
                {
                    return;
                }
                else if (enmButtonFunction == ButtonFunctionEnum.RowChange)
                {
                    this.OnGrdRowChange(grdSender, grdSender.GetActiveRow());
                }
                else if (enmButtonFunction == ButtonFunctionEnum.Refresh)
                {
                    grdSender.ReloadData(true);
                }
                else if (enmButtonFunction == ButtonFunctionEnum.Save)
                {

                    if (this.GetViewStateValue(grdSender.ID, PageValueNameEnum.GridState, FormState.FormStateEdit.ToString()) == FormState.FormStateEdit.ToString())
                    {
                        strPKValue = grdSender.GetActiveRowPrimaryKey();
                    }
                    else
                    {
                        strPKValue = "-1";
                    }

                    string strSQL = SQLHelper.GetCondition(grdSender.GetCorrectRecordset(), grdSender.PrimaryKey, strPKValue, "=");
                    dt = DBHelper.GetDataTable(strSQL);

                    // Look for page state ( ADD EDIT )
                    if (this.GetViewStateValue(grdSender.ID, PageValueNameEnum.GridState, FormState.FormStateEdit.ToString()) == FormState.FormStateEdit.ToString())
                    {
                        if (dt.Rows.Count < 1)
                            return;
                        row = dt.Rows[0];
                    }
                    else
                    {
                        row = dt.NewRow();
                    }

                    if (grdSender.DataGrid != "" && grdSender.InsertForeignKey == true)
                    {
                        if (grdSender.ParentFieldName != "")
                        {
                            BLGrid grdParent = this.GetDataGrid(grdSender.DataGrid);
                            if (grdParent != null)
                            {
                                string strVal = "";
                                strVal = grdParent.GetActiveRowPrimaryKey();
                                if (strVal != "")
                                {
                                    strSQL = SQLHelper.GetCondition(grdParent.GetCorrectRecordset(), grdParent.PrimaryKey, strVal, "=");
                                    try
                                    {
                                        IDataReader reader = DBHelper.GetReader(strSQL);
                                        while (reader.Read())
                                        {
                                            row[grdSender.ChildFieldName] = reader[grdSender.ParentFieldName];
                                        }
                                        reader.Close();
                                    }
                                    catch (Exception ex)
                                    {
                                        ExceptionManager.ShowException(ex, "OnButtonClick");
                                        return;
                                    }

                                }
                            }
                        }
                    }
                }
                else if (enmButtonFunction == ButtonFunctionEnum.New)
                {
                    // no, by cancel is good to have active row !!! grdSender.DisplayLayout.ActiveRow = null;
                    SetViewStateValue(grdSender.ID, PageValueNameEnum.GridState, FormState.FormStateNew.ToString());
                    this.ClearControls(strDataGrid);
                }
                else if (enmButtonFunction == ButtonFunctionEnum.Delete)
                {
                    if (this.GetViewStateValue(grdSender.ID, PageValueNameEnum.GridState, FormState.FormStateEdit.ToString()) != FormState.FormStateEdit.ToString())
                        return;

                    strPKValue = grdSender.GetActiveRowPrimaryKey();
                    string strSQL = SQLHelper.GetCondition(grdSender.GetCorrectRecordset(), grdSender.PrimaryKey, strPKValue, "=");

                    dt = DBHelper.GetDataTable(strSQL);

                    if (dt.Rows.Count == 1)
                    {
                        row = dt.Rows[0];
                        row.Delete();
                    }
                }

                if (enmButtonFunction == ButtonFunctionEnum.Save)
                    CollectFormData(strDataGrid, row);


                if (enmButtonFunction == ButtonFunctionEnum.Save || enmButtonFunction == ButtonFunctionEnum.Delete)
                {
                    bool bAddNewRow = false;

                    if (row.RowState == DataRowState.Detached)
                    {
                        bAddNewRow = true;
                        dt.Rows.Add(row);
                    }

                    // by delete we must remove also child records
                    if (enmButtonFunction == ButtonFunctionEnum.Delete)
                    {
                        this.DBHelper.Connect();
                        this.DBHelper.BeginTransaction();
                        this.DeleteChildRecords(grdSender.ID, strPKValue);
                    }

                   
                    UpdateEventArgs e = new UpdateEventArgs(dt, grdSender.UpdateTableName, grdSender.PrimaryKey);
                    grdSender.SaveHelperInst.Save(e);

                    if (enmButtonFunction == ButtonFunctionEnum.Delete)
                    {
                        if (this.DBHelper.IsTransaction)
                            this.DBHelper.CommitTransaction();
                    }

                    string strPrimaryKeyValue = "";

                    if (bAddNewRow == true)
                        strPrimaryKeyValue = grdSender.SaveHelperInst.LastIdentityValue;
                    else
                        strPrimaryKeyValue = grdSender.GetActiveRowPrimaryKey();

                    grdSender.ReloadData(true);
                    grdSender.SelectRowByValue(grdSender.PrimaryKey, strPrimaryKeyValue);
                   
                    if (bAddNewRow == true)
                    {
                        this.OnGrdRowChange(grdSender, grdSender.GetActiveRow());
                    }
                    if (grdSender.GetActiveRow() != null)
                    {
                        SetViewStateValue(grdSender.ID, PageValueNameEnum.GridState, FormState.FormStateEdit.ToString());
                    }
                }

            }
            catch (Exception ex)
            {
                if (this.DBHelper.IsTransaction)
                    this.DBHelper.RollBackTransaction();

                ExceptionManager.ShowException(ex, "OnButtonClick");
            }
            finally
            {
                if (enmButtonFunction == ButtonFunctionEnum.Delete)
                    this.DBHelper.DisConnect();
            }

        }

        public void CollectFormData(string strDataGrid, DataRow row)
        {
            strDataGrid = strDataGrid.ToUpper();
            foreach (System.Web.UI.Control ctrl in m_lstControls)
            {
                if (ctrl is BLTextBox)
                {
                    BLTextBox txt = ctrl as BLTextBox;
                    if (txt.DataGrid.ToUpper() == strDataGrid && txt.DataField != "" && txt.SaveToDB == true)
                    {
                        try
                        {
                            row[txt.DataField] = txt.GetDBValue();
                        }
                        catch (Exception exCtrl)
                        {
                            ExceptionManager.ShowException(exCtrl, "ControlName datafield is wrong. Control = TEXTBOX, Name = " + txt.ID + " DataField = " + txt.DataField);
                        }
                    }
                }
                if (ctrl is BLTextEditor)
                {
                    BLTextEditor txt = ctrl as BLTextEditor;
                    if (txt.DataGrid.ToUpper() == strDataGrid && txt.DataField != "" && txt.SaveToDB == true)
                    {
                        try
                        {
                            row[txt.DataField] = txt.GetDBValue();
                        }
                        catch (Exception exCtrl)
                        {
                            ExceptionManager.ShowException(exCtrl, "ControlName datafield is wrong. Control = TEXTEDITOR, Name = " + txt.ID + " DataField = " + txt.DataField);
                        }
                    }
                }
                if (ctrl is BLMaskEdit)
                {
                    BLMaskEdit mask = ctrl as BLMaskEdit;
                    if (mask.DataGrid.ToUpper() == strDataGrid && mask.DataField != "" && mask.SaveToDB == true)
                    {
                        try
                        {
                            row[mask.DataField] = mask.GetDBValue();
                        }
                        catch (Exception exCtrl)
                        {
                            ExceptionManager.ShowException(exCtrl, "ControlName datafield is wrong. Control = MASKEDIT, Name = " + mask.ID + " DataField = " + mask.DataField);
                        }
                    }
                }
                if (ctrl is BLComboBox)
                {
                    BLComboBox cbo = ctrl as BLComboBox;
                    if (cbo.DataGrid.ToUpper() == strDataGrid && cbo.DataField != "" && cbo.SaveToDB == true)
                    {
                        try
                        {
                            row[cbo.DataField] = cbo.GetDBValue();
                        }
                        catch (Exception exCtrl)
                        {
                            ExceptionManager.ShowException(exCtrl, "ControlName datafield is wrong. Control = COMBOBOX, Name = " + cbo.ID + " DataField = " + cbo.DataField);
                        }
                    }
                }
                if (ctrl is BLComboBoxInfra)
                {
                    BLComboBoxInfra cbo = ctrl as BLComboBoxInfra;
                    if (cbo.DataGrid.ToUpper() == strDataGrid && cbo.DataField != "" && cbo.SaveToDB == true)
                    {
                        try
                        {
                            row[cbo.DataField] = cbo.GetDBValue();
                        }
                        catch (Exception exCtrl)
                        {
                            ExceptionManager.ShowException(exCtrl, "ControlName datafield is wrong. Control = INFRA COMBO, Name = " + cbo.ID + " DataField = " + cbo.DataField);
                        }
                    }
                }
                if (ctrl is BLCheckBox)
                {
                    BLCheckBox chk = ctrl as BLCheckBox;
                    if (chk.DataGrid.ToUpper() == strDataGrid && chk.DataField != "" && chk.SaveToDB == true)
                    {
                        try
                        {
                            row[chk.DataField] = chk.GetDBValue();
                        }
                        catch (Exception exCtrl)
                        {
                            ExceptionManager.ShowException(exCtrl, "ControlName datafield is wrong. Control = CHECKBOX, Name = " + chk.ID + " DataField = " + chk.DataField);
                        }
                    }
                }
            }
        }

        public void DeleteChildRecords(string strParentDataGridName, string strParentDataGridGridValue)
        {
            foreach (BLGrid grd in this.m_lstDataGrids)
            {
                grd.DeleteChildRecords(strParentDataGridName, strParentDataGridGridValue);
            }
        }

   
        public System.Web.UI.Control GetPostBackControl()
        {
            System.Web.UI.Control control = null;

            string ctrlname = PAGE.Request.Params.Get("__EVENTTARGET");
            if (ctrlname != null && ctrlname != String.Empty )
            {
                control = PAGE.FindControl(ctrlname);
            }
            else
            {
                foreach (string ctl in PAGE.Request.Form)
                {
                    System.Web.UI.Control c = PAGE.FindControl(ctl);
                    if (c is System.Web.UI.WebControls.Button )
                    {
                        control = c;
                        break;
                    }
                }
            }
            return control;
        }


        public void RegisterEvents(System.Web.UI.Control ctrl)
        {
            ProcessControls(ctrl,false);
            ProcessControlsAfter();
        }


        private void UnregisterEventsProcess(System.Web.UI.Control ctrl)
        {
            m_arrToRemove.Add(ctrl);

            foreach (System.Web.UI.Control ctrl2 in ctrl.Controls)
            {
                if (ctrl2 is System.Web.UI.Control)
                {
                    UnregisterEventsProcess(ctrl2 as System.Web.UI.Control);
                }
                if (ctrl2 is BL.BLGrid)
                {
                    BL.BLGrid grd = ctrl2 as BL.BLGrid;
                    grd.ActiveRowChange -= new Infragistics.WebUI.UltraWebGrid.ActiveRowChangeEventHandler(OnDataGrid_ActiveRowChange);
                }
                else if (ctrl2 is BLTree)
                {
                    BLTree m_Tree = ctrl2 as BLTree;
                    m_Tree.NodeClicked -= new Infragistics.WebUI.UltraWebNavigator.NodeClickedEventHandler(OnTree_NodeClicked);
                }
                else if (ctrl2 is BLButton)
                {
                    BLButton btn = ctrl2 as BLButton;
                    if (btn.ButtonFunction != ButtonFunctionEnum.OpenForm)
                    {
                        btn.Click -= new EventHandler(OnBtnClick);
                    }
                }
                else if (ctrl2 is BLComboBoxFilter)
                {
                    BLComboBoxFilter flt = ctrl2 as BLComboBoxFilter;
                    flt.SelectedIndexChanged -= new EventHandler(OnCboFilterSelectedIndexChanged);
                }
                else if (ctrl2 is BLButtonInfra)
                {
                    BLButtonInfra btn = ctrl2 as BLButtonInfra;
                    if (btn.ButtonFunction != ButtonFunctionEnum.OpenForm)
                    {
                        btn.Click -= new  Infragistics.WebUI.WebDataInput.ClickHandler(OnBtnClickInfra);
                    }
                }
                else if (ctrl2 is BLSingleSearch)
                {
                    BLSingleSearch search = ctrl2 as BLSingleSearch;
                    search.Searching -= new BLSingleSearch.SearchingDelegate(OnSingleSearchControlSearching);
                }
                else if (ctrl2 is BLMultiSearch)
                {
                    BLMultiSearch search = ctrl2 as BLMultiSearch;
                    search.Searching -= new BLMultiSearch.SearchingDelegate(OnMultiSearchControlSearching);
                }
                 else if (ctrl2 is BLPositionControl)
                {
                    BLPositionControl pos = ctrl2 as BLPositionControl;
                    pos.Moving += new BLPositionControl.MovingDelegate(OnPositionControlMoving);
                }
            }
        }
          

        private System.Collections.ArrayList m_arrToRemove;
        public void UnregisterEvents(System.Web.UI.Control ctrl)
        {
            m_arrToRemove = new System.Collections.ArrayList(20);
            foreach (System.Web.UI.Control ctrl2 in ctrl.Controls)
            {
                this.UnregisterEventsProcess(ctrl2);
            }
            foreach (System.Web.UI.Control ctrl2 in m_arrToRemove)
            {
                this.m_lstControls.Remove(ctrl2);
                if (ctrl2 is BL.BLGrid)
                {
                    BL.BLGrid grd2 = ctrl2 as BL.BLGrid;
                    if (this.m_lstDataGrids.Contains(grd2) == true)
                    {
                        this.m_lstDataGrids.Remove(grd2);
                    }
                }

            }
            m_arrToRemove = null;
        }

    }

  Na stránku 
screen  Nový příspěvek
Název  Uživatel  Datum 
Poslední návštěva: 17:38:33, 21. září 2017 První  Předchozí  0 Záznamů  Další  Poslední  

Autor článku
Jméno
Pracovní pozice
Informace
Foto

   

Počet návštěvníků:2
 
  Kontakt