//load workform for new record
    protected void create_NewMetaContent()
    {
        _MetaContent = new ContentEditorDataHelper();
        _MetaContent.load_File(FileTemplateDataProvider.get_WorkformTemplateFile(LegoWebAdmin.BusLogic.Categories.get_CATEGORY_TEMPLATE_NAME(int.Parse(dropCategories.SelectedValue.ToString()))));
        _MetaContent.MetaContentID    = 0;
        this.txtMetaContentAlias.Text = "";

        this.radioRecordStatus.SelectedValue = _MetaContent.RecordStatus.ToString();

        this.txtLocalCode.Text                 = _MetaContent.LocalCode;
        this.labelEntryDate.Text               = DateTime.Now.ToString("dd-MMM-yyyy");
        this.labelCreator.Text                 = this.Page.User.Identity.Name;
        this.labelModifyDate.Text              = "";
        this.labelModifier.Text                = "";
        this.dropLanguages.SelectedValue       = _MetaContent.LangCode;
        this.dropAccessLevels.SelectedValue    = _MetaContent.AccessLevel.ToString();
        this.dropImportantLevels.SelectedValue = _MetaContent.ImportantLevel.ToString();

        DataTable marcTable = _MetaContent.get_MarcDatafieldTable();
        CRecord   labelRec  = new CRecord();

        labelRec.load_File(FileTemplateDataProvider.get_LabelTemplateFile(LegoWebAdmin.BusLogic.Categories.get_CATEGORY_TEMPLATE_NAME(int.Parse(dropCategories.SelectedValue.ToString()))));
        _MetaContent.set_DataTableLabel(ref marcTable, labelRec);
        repeater_DataBind(marcTable);

        load_listAddTag();
        this.txtAddValue.Attributes.Add("onClick", "changeInputID('" + this.txtAddValue.ClientID.ToString() + "'); return false;");

        Session["METADATA"] = _MetaContent.OuterXml;
    }
    protected void update_MetaContent()
    {
        _MetaContent = new ContentEditorDataHelper();
        _MetaContent.load_Xml(Session["METADATA"].ToString());
        this.txtMetaContentID.Text    = _MetaContent.MetaContentID.ToString();
        this.txtMetaContentAlias.Text = _MetaContent.Alias;
        if (_MetaContent.MetaContentID > 0)
        {
            int iCategoryId = _MetaContent.CategoryID;
            int iSectionId  = (int)LegoWebAdmin.BusLogic.Categories.get_CATEGORY_BY_ID(iCategoryId).Tables[0].Rows[0]["SECTION_ID"];
            load_Sections(iSectionId);
            load_Categories(iSectionId);
            dropCategories.SelectedValue = iCategoryId.ToString();
        }
        DataTable marcTable = _MetaContent.get_MarcDatafieldTable();
        CRecord   labelRec  = new CRecord();

        labelRec.load_File(FileTemplateDataProvider.get_LabelTemplateFile(LegoWebAdmin.BusLogic.Categories.get_CATEGORY_TEMPLATE_NAME(int.Parse(dropCategories.SelectedValue.ToString()))));
        _MetaContent.set_DataTableLabel(ref marcTable, labelRec);
        repeater_DataBind(marcTable);

        this.radioRecordStatus.SelectedValue = _MetaContent.RecordStatus.ToString();
        this.txtLocalCode.Text                 = _MetaContent.LocalCode;
        this.labelEntryDate.Text               = _MetaContent.EntryDate;
        this.labelCreator.Text                 = _MetaContent.Creator;
        this.labelModifyDate.Text              = _MetaContent.ModifyDate;
        this.labelModifier.Text                = _MetaContent.Modifier;
        this.dropLanguages.SelectedValue       = _MetaContent.LangCode;
        this.dropAccessLevels.SelectedValue    = _MetaContent.AccessLevel.ToString();
        this.dropImportantLevels.SelectedValue = _MetaContent.ImportantLevel.ToString();

        if (_MetaContent.AccessLevel >= 2)//special permission
        {
            cblRoles.Visible = true;
            //get roles in formation
            string[] accessRoles = LegoWebAdmin.BusLogic.MetaContents.get_ACCESS_ROLES(_MetaContent.MetaContentID);
            if (accessRoles != null && accessRoles.Length > 0)
            {
                for (int i = 0; i < accessRoles.Length; i++)
                {
                    for (int j = 0; j < cblRoles.Items.Count; j++)
                    {
                        if (cblRoles.Items[j].Value == accessRoles[i])
                        {
                            cblRoles.Items[j].Selected = true;
                        }
                    }
                }
            }
        }
        else
        {
            cblRoles.Visible = false;
        }

        load_listAddTag();
        this.txtAddValue.Attributes.Add("onClick", "changeInputID('" + this.txtAddValue.ClientID.ToString() + "'); return false;");

        Session["METADATA"] = _MetaContent.OuterXml;
    }
    protected void cmdAddTagOrSubfield_Click(object sender, EventArgs e)
    {
        save_MetaContentData();//save data first

        _MetaContent = new ContentEditorDataHelper();
        _MetaContent.load_Xml(Session["METADATA"].ToString());

        CRecord labelRec = new CRecord();

        labelRec.load_File(FileTemplateDataProvider.get_LabelTemplateFile(LegoWebAdmin.BusLogic.Categories.get_CATEGORY_TEMPLATE_NAME(int.Parse(dropCategories.SelectedValue.ToString()))));

        CDatafield Df = labelRec.Datafields.Datafield(listAddTag.SelectedValue.ToString());

        DataTable marcTable = _MetaContent.get_MarcDatafieldTable();
        int       iTagIndex = 0;

        if (marcTable.Rows.Count > 0)
        {
            DataRow[] maxRows = marcTable.Select("TAG_INDEX=Max(TAG_INDEX)");
            iTagIndex = int.Parse("0" + maxRows[0]["TAG_INDEX"].ToString());
        }
        if (listAddSubfieldCode.SelectedValue == "")//add new tag
        {
            for (int i = 0; i < Df.Subfields.Count; i++)
            {
                DataRow nRow = marcTable.NewRow();
                nRow["TAG"]            = Df.Tag;
                nRow["TAG_INDEX"]      = iTagIndex + 1;
                nRow["SUBFIELD_CODE"]  = Df.Subfields.Subfield(i).Code;
                nRow["SUBFIELD_TYPE"]  = Df.Subfields.Subfield(i).Type;
                nRow["SUBFIELD_LABEL"] = Df.Subfields.Subfield(i).Value;
                nRow["SUBFIELD_VALUE"] = txtAddValue.Text;
                marcTable.Rows.Add(nRow);
            }
            marcTable.DefaultView.Sort = "TAG, TAG_INDEX ASC";

            _MetaContent.set_DataTableLabel(ref marcTable, labelRec);
            repeater_DataBind(marcTable);
            _MetaContent.bind_TableDataToMarc(ref marcTable);
            Session["METADATA"] = _MetaContent.OuterXml;
            return;
        }
        else//add subfield to existing tag or new tag only one subfield
        {
            CSubfield Sf = Df.Subfields.Subfield(listAddSubfieldCode.SelectedValue.ToString());

            #region add to subfield to selected tag if have one
            for (int i = 0; i < this.marcTextRepeater.Items.Count; i++)
            {
                CheckBox cb = ((CheckBox)marcTextRepeater.Items[i].FindControl("RowLevelCheckBox"));
                if (cb.Checked)
                {
                    Label labelTag = (Label)marcTextRepeater.Items[i].FindControl("labelTag");
                    if (labelTag.Text == listAddTag.SelectedValue.ToString())
                    {
                        DataRow nRow = marcTable.NewRow();
                        nRow["TAG"] = labelTag.Text;
                        Label labelTagIndex = (Label)marcTextRepeater.Items[i].FindControl("labelTagIndex");
                        nRow["TAG_INDEX"]      = int.Parse(labelTagIndex.Text);
                        nRow["SUBFIELD_CODE"]  = Sf.Code;
                        nRow["SUBFIELD_TYPE"]  = Sf.Type;
                        nRow["SUBFIELD_LABEL"] = Sf.Value;
                        nRow["SUBFIELD_VALUE"] = txtAddValue.Text;
                        marcTable.Rows.Add(nRow);
                        //sort go here
                        marcTable.DefaultView.Sort = "TAG, TAG_INDEX ASC";
                        _MetaContent.set_DataTableLabel(ref marcTable, labelRec);
                        repeater_DataBind(marcTable);
                        _MetaContent.bind_TableDataToMarc(ref marcTable);
                        Session["METADATA"] = _MetaContent.OuterXml;
                        return;
                    }
                }
            }
            #endregion
            //if don't have match selected tag find existing tag
            for (int i = 0; i < marcTable.Rows.Count; i++)
            {
                if (marcTable.Rows[i]["TAG"].ToString() == listAddTag.SelectedValue.ToString())
                {
                    DataRow nRow = marcTable.NewRow();
                    nRow["TAG"]            = marcTable.Rows[i]["TAG"];
                    nRow["TAG_INDEX"]      = marcTable.Rows[i]["TAG_INDEX"];
                    nRow["SUBFIELD_CODE"]  = Sf.Code;
                    nRow["SUBFIELD_TYPE"]  = Sf.Type;
                    nRow["SUBFIELD_LABEL"] = Sf.Value;
                    nRow["SUBFIELD_VALUE"] = txtAddValue.Text;
                    marcTable.Rows.Add(nRow);
                    //sort go here
                    marcTable.DefaultView.Sort = "TAG, TAG_INDEX ASC";
                    _MetaContent.set_DataTableLabel(ref marcTable, labelRec);
                    repeater_DataBind(marcTable);
                    _MetaContent.bind_TableDataToMarc(ref marcTable);
                    Session["METADATA"] = _MetaContent.OuterXml;
                    return;
                }
            }
            //don't have existing match tag create new tag with one subfield
            DataRow addRow = marcTable.NewRow();
            addRow["TAG"]            = Df.Tag;
            addRow["TAG_INDEX"]      = iTagIndex + 1;
            addRow["SUBFIELD_CODE"]  = Sf.Code;
            addRow["SUBFIELD_TYPE"]  = Sf.Type;
            addRow["SUBFIELD_LABEL"] = Sf.Value;
            addRow["SUBFIELD_VALUE"] = txtAddValue.Text;
            marcTable.Rows.Add(addRow);
            //sort go here
            marcTable.DefaultView.Sort = "TAG, TAG_INDEX ASC";
            _MetaContent.set_DataTableLabel(ref marcTable, labelRec);
            repeater_DataBind(marcTable);
            _MetaContent.bind_TableDataToMarc(ref marcTable);
            Session["METADATA"] = _MetaContent.OuterXml;
            return;
        }
    }
    protected void cmdRemoveSelectedRow_Click(object sender, EventArgs e)
    {
        save_MetaContentData();

        _MetaContent = new ContentEditorDataHelper();
        _MetaContent.load_Xml(Session["METADATA"].ToString());

        DataTable marcTable = _MetaContent.get_MarcDatafieldTable();

        for (int i = 0; i < this.marcTextRepeater.Items.Count; i++)
        {
            CheckBox cb = ((CheckBox)marcTextRepeater.Items[i].FindControl("RowLevelCheckBox"));
            if (cb != null && cb.Checked)
            {
                //remove by TAG_INDEX and SUBFIELD_CODE
                Label labelSubfieldID = ((Label)marcTextRepeater.Items[i].FindControl("labelSubfieldID"));

                if (labelSubfieldID != null && !String.IsNullOrEmpty(labelSubfieldID.Text) && labelSubfieldID.Text != "0")
                {
                    LegoWebAdmin.BusLogic.MetaContents.remove_META_CONTENT_SUBFIELD(int.Parse(labelSubfieldID.Text));
                    DataRow[] remRows = marcTable.Select(" SUBFIELD_ID=" + labelSubfieldID.Text);
                    if (remRows != null)
                    {
                        marcTable.Rows.Remove(remRows[0]);
                    }
                }
                else
                {
                    Label labelTagIndex     = ((Label)marcTextRepeater.Items[i].FindControl("labelTagIndex"));
                    Label labelSubfieldCode = ((Label)marcTextRepeater.Items[i].FindControl("labelSubfieldCode"));

                    if (labelTagIndex != null && labelSubfieldCode != null)
                    {
                        DataRow[] remRows = marcTable.Select("TAG_INDEX=" + labelTagIndex.Text + " AND SUBFIELD_CODE='" + labelSubfieldCode.Text + "'");
                        if (remRows != null)
                        {
                            marcTable.Rows.Remove(remRows[0]);
                        }
                    }
                }
            }
        }

        for (int i = 0; i < this.marcNTextRepeater.Items.Count; i++)
        {
            CheckBox cb = ((CheckBox)marcNTextRepeater.Items[i].FindControl("RowLevelCheckBox"));
            if (cb != null && cb.Checked)
            {
                //remove by TAG_INDEX and SUBFIELD_CODE
                Label labelSubfieldID = ((Label)marcNTextRepeater.Items[i].FindControl("labelSubfieldID"));

                if (labelSubfieldID != null && !String.IsNullOrEmpty(labelSubfieldID.Text))
                {
                    LegoWebAdmin.BusLogic.MetaContents.remove_META_CONTENT_SUBFIELD(int.Parse(labelSubfieldID.Text));
                    DataRow[] remRows = marcTable.Select(" SUBFIELD_ID=" + labelSubfieldID.Text);
                    if (remRows != null)
                    {
                        marcTable.Rows.Remove(remRows[0]);
                    }
                }
                else
                {
                    Label labelTagIndex     = ((Label)marcNTextRepeater.Items[i].FindControl("labelTagIndex"));
                    Label labelSubfieldCode = ((Label)marcNTextRepeater.Items[i].FindControl("labelSubfieldCode"));

                    if (labelTagIndex != null && labelSubfieldCode != null)
                    {
                        DataRow[] remRows = marcTable.Select("TAG_INDEX=" + labelTagIndex.Text + " AND SUBFIELD_CODE='" + labelSubfieldCode.Text + "'");
                        if (remRows != null)
                        {
                            marcTable.Rows.Remove(remRows[0]);
                        }
                    }
                }
            }
        }
        CRecord labelRec = new CRecord();

        labelRec.load_File(FileTemplateDataProvider.get_LabelTemplateFile(LegoWebAdmin.BusLogic.Categories.get_CATEGORY_TEMPLATE_NAME(int.Parse(dropCategories.SelectedValue.ToString()))));
        _MetaContent.set_DataTableLabel(ref marcTable, labelRec);
        repeater_DataBind(marcTable);
        _MetaContent.bind_TableDataToMarc(ref marcTable);

        Session["METADATA"] = _MetaContent.OuterXml;
    }