protected override void OnUnload(EventArgs e)
 {
     base.OnUnload(e);
     try
     {
         SessionStore csSession = SessionManager.GetSession();
         if (csSession != null &&
             csSession.GetCurrentDocument() != null)
         {
             //csSession.CurrentDocument.Close();
         }
     }
     catch (UserError uerr)
     {
     }
 }
        private void LoadVariables()
        {
            VariablesLB.Items.Clear();
            SessionStore csSession = SessionManager.GetSession();
            CSLabel      csobj     = csSession.GetCurrentLabel();

            if (csobj != null)
            {
                if (csobj.CSVariables.Count == 0)
                {
                    using (var doc = csSession.GetCurrentDocument())
                    {
                        foreach (CSVariable aVariable in doc.CSVariables.OrderBy(p => p.Name))
                        {
                            //CSVariable aVariable = doc.CSVariables[i];
                            CodeSoftDTO.Variable csVar = new CodeSoftDTO.Variable();

                            csVar.Name           = aVariable.Name;
                            csVar.Value          = ""; //aVariable.Value;
                            csVar.ArrayBaseShift = 0;
                            csVar.IsHidden       = !aVariable.IsUserInput;

                            if (aVariable.Name.ToLower().Contains("x_"))
                            {
                                var    regex  = new Regex("[^a-zA-Z0-9]");
                                var    regex2 = new Regex(@"\d+");
                                string root   = aVariable.Name.Replace("X_", "").Replace("x_", "");
                                string index  = regex2.Match(root).Value;
                                root = root.Remove(regex.Match(root).Index);
                                if (!root.ToLower().Contains("mod"))
                                {
                                    csVar.ArrayBaseShift = -1;
                                }
                                csVar.ArrayIndex = Int32.Parse(index);
                                csVar.GroupName  = root;
                                csVar.IsXBox     = true;
                                csVar.IsHidden   = true;
                            }

                            csobj.AddVariable(csVar);
                        }

                        /*CodeSoftDTO.Variable csVarQty = new CodeSoftDTO.Variable();
                         * csVarQty.Name = "LABEL_QUANTITY";
                         * csVarQty.Value = "1";
                         * csVarQty.ArrayBaseShift = 0;
                         * csobj.AddVariable(csVarQty);*/
                    }
                }


                List <HFValue> HFValues = new List <HFValue>();
                foreach (CodeSoftDTO.Variable v in csobj.CSVariables.Where(v => string.IsNullOrEmpty(v.Name) == false))
                {
                    ListItem item = new ListItem(v.Name);
                    item.Attributes.Add("tag", v.Value);

                    if (v.IsXBox)
                    {
                        HFValues.Add(new HFValue()
                        {
                            Name = v.Name, Value = v.Value
                        });
                    }

                    /*if (v.IsHidden)
                     * {
                     *  item.Attributes.Add("style", "display:none;");
                     *  item.Attributes.Add("disabled", "true");
                     * }*/
                    if (!v.IsHidden)
                    {
                        VariablesLB.Items.Add(item);
                    }
                }

                VariableValuesHf.Value = JsonConvert.SerializeObject(HFValues);
                //load the groups in the browser.
                string jscript = "";
                var    groups  = csobj.CSVariables.Where(l => l.GroupName != null).GroupBy(g => g.GroupName);
                foreach (var group in groups)
                {
                    CodeSoftDTO.VariableGroup vgroup = new CodeSoftDTO.VariableGroup();
                    vgroup.Name = group.Key;
                    jscript    += "groups.push('" + vgroup.Name + "');";
                    vgroup.Variables.AddRange(csobj.CSVariables.Where(v => v.GroupName == vgroup.Name).Cast <CodeSoftDTO.Variable>());
                    foreach (CodeSoftDTO.Variable v in vgroup.Variables.OrderBy(o => o.ArrayIndex))
                    {
                        jscript += "groupItems.push({ group: \"" + vgroup.Name + "\", item: \"<div class='x_item' ><label for='" + v.Name + "' class='x_item_label' >" + (v.ArrayIndex + v.ArrayBaseShift) + "</label><input type='checkbox' id='" + v.Name + "' class='x_item_chk' /></div>\" });";
                    }
                }
                ClientScriptManager cm = ClientScript;
                cm.RegisterClientScriptBlock(GetType(), "LoadXGroupData", jscript, true);

                if (HFValues.Count > 0)
                {
                    cm       = ClientScript;
                    jscript  = "$(document).ready(function () {";
                    jscript += "$('#x_dialogIcon').show();";
                    jscript += "});";
                    cm.RegisterClientScriptBlock(GetType(), "HideXDialog", jscript, true);
                }
            }
        }