public void Take_LinkRelatedContents()
    {
        try
        {
            if (Session["METADATA"] == null)
            {
                Response.Redirect("MetaContentEditor.aspx");
            }
            LegoWebAdmin.DataProvider.ContentEditorDataHelper _MetaContentObject = new ContentEditorDataHelper();
            _MetaContentObject.load_Xml(Session["METADATA"].ToString());

            DataTable  marcTable = _MetaContentObject.get_MarcDatafieldTable();
            CDatafield Df        = new CDatafield();
            for (int i = 0; i < this.metaContentManagerRepeater.Items.Count; i++)
            {
                CheckBox cbRow = ((CheckBox)metaContentManagerRepeater.Items[i].FindControl("chkSelect"));
                if (cbRow.Checked == true)
                {
                    TextBox txtMetaContentId = (TextBox)metaContentManagerRepeater.Items[i].FindControl("txtMetaContentId");
                    if (txtMetaContentId != null)
                    {
                        int   iTagIndex      = marcTable.Rows.Count;
                        Int32 iMetaContentId = Int32.Parse(txtMetaContentId.Text);

                        Label labelMetaContentTitle = (Label)metaContentManagerRepeater.Items[i].FindControl("labelMetaContentTitle");
                        if (labelMetaContentTitle != null)
                        {
                            DataRow addRow = marcTable.NewRow();
                            addRow["TAG"]            = 780;
                            addRow["TAG_INDEX"]      = iTagIndex;
                            addRow["SUBFIELD_CODE"]  = "t";
                            addRow["SUBFIELD_LABEL"] = " ";
                            addRow["SUBFIELD_TYPE"]  = "TEXT";
                            addRow["SUBFIELD_VALUE"] = labelMetaContentTitle.Text;
                            marcTable.Rows.Add(addRow);

                            addRow                   = marcTable.NewRow();
                            addRow["TAG"]            = 780;
                            addRow["TAG_INDEX"]      = iTagIndex;
                            addRow["SUBFIELD_CODE"]  = "w";
                            addRow["SUBFIELD_LABEL"] = " ";
                            addRow["SUBFIELD_TYPE"]  = "NUMBER";
                            addRow["SUBFIELD_VALUE"] = iMetaContentId;
                            marcTable.Rows.Add(addRow);
                        }
                    }
                }
            }
            _MetaContentObject.bind_TableDataToMarc(ref marcTable);
            Session["METADATA"] = _MetaContentObject.OuterXml;
            Response.Redirect("MetaContentEditor.aspx");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    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;
    }
    protected void save_MetaContentData()
    {
        try
        {
            _MetaContent = new ContentEditorDataHelper();
            _MetaContent.load_Xml(Session["METADATA"].ToString());

            _MetaContent.Alias = txtMetaContentAlias.Text;
            if (!String.IsNullOrEmpty(this.dropCategories.SelectedValue))
            {
                _MetaContent.CategoryID = Convert.ToInt16(this.dropCategories.SelectedValue.ToString());
            }
            _MetaContent.LocalCode = txtLocalCode.Text;
            //check duplicate in local code go here
            if (!String.IsNullOrEmpty(txtLocalCode.Text))
            {
                if (LegoWebAdmin.BusLogic.MetaContents.is_LocalCode_Exist(txtLocalCode.Text, _MetaContent.CategoryID, _MetaContent.MetaContentID))
                {
                    throw new Exception("Local Code is already existed!");
                }
            }
            _MetaContent.RecordStatus   = int.Parse(this.radioRecordStatus.SelectedValue);
            _MetaContent.LangCode       = this.dropLanguages.SelectedValue.ToString();
            _MetaContent.AccessLevel    = int.Parse(this.dropAccessLevels.SelectedValue);
            _MetaContent.ImportantLevel = int.Parse(this.dropImportantLevels.SelectedValue);

            DataTable marcTable = _MetaContent.create_MarcDataTable();

            for (int i = 0; i < marcTextRepeater.Items.Count; i++)
            {
                DataRow nRow     = marcTable.NewRow();
                Label   labelTag = (Label)marcTextRepeater.Items[i].FindControl("labelTag");
                if (labelTag != null)
                {
                    nRow["TAG"] = labelTag.Text;
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelTag==null");
                }

                Label labelTagIndex = (Label)marcTextRepeater.Items[i].FindControl("labelTagIndex");
                if (labelTagIndex != null)
                {
                    nRow["TAG_INDEX"] = int.Parse(labelTagIndex.Text);
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelTagIndex==null");
                }

                Label labelSubfieldID = (Label)marcTextRepeater.Items[i].FindControl("labelSubfieldID");
                if (labelSubfieldID != null && !String.IsNullOrEmpty(labelSubfieldID.Text))
                {
                    nRow["SUBFIELD_ID"] = Int32.Parse(labelSubfieldID.Text);
                }

                Label labelSubfieldCode = (Label)marcTextRepeater.Items[i].FindControl("labelSubfieldCode");
                if (labelSubfieldCode != null)
                {
                    nRow["SUBFIELD_CODE"] = labelSubfieldCode.Text;
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelSubfieldCode==null");
                }

                Label labelSubfieldLabel = (Label)marcTextRepeater.Items[i].FindControl("labelSubfieldLabel");

                if (labelSubfieldLabel != null)
                {
                    nRow["SUBFIELD_LABEL"] = labelSubfieldLabel.Text;
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelSubfieldLabel==null");
                }

                Label labelSubfieldType = (Label)marcTextRepeater.Items[i].FindControl("labelSubfieldType");
                if (labelSubfieldType != null)
                {
                    nRow["SUBFIELD_TYPE"] = labelSubfieldType.Text;
                    TextBox Subfield_Value = (TextBox)marcTextRepeater.Items[i].FindControl("Subfield_Value");
                    if (Subfield_Value != null)
                    {
                        nRow["SUBFIELD_VALUE"] = Subfield_Value.Text;
                    }
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelSubfieldType==null");
                }

                marcTable.Rows.Add(nRow);
            }

            /*marcNTextRepeater*/
            #region marcNTextRepeater
            for (int i = 0; i < marcNTextRepeater.Items.Count; i++)
            {
                DataRow nRow     = marcTable.NewRow();
                Label   labelTag = (Label)marcNTextRepeater.Items[i].FindControl("labelTag");
                if (labelTag != null)
                {
                    nRow["TAG"] = labelTag.Text;
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelTag==null");
                }

                Label labelTagIndex = (Label)marcNTextRepeater.Items[i].FindControl("labelTagIndex");
                if (labelTagIndex != null)
                {
                    nRow["TAG_INDEX"] = int.Parse(labelTagIndex.Text);
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelTagIndex==null");
                }

                Label labelSubfieldID = (Label)marcNTextRepeater.Items[i].FindControl("labelSubfieldID");
                if (labelSubfieldID != null && !String.IsNullOrEmpty(labelSubfieldID.Text))
                {
                    nRow["SUBFIELD_ID"] = labelSubfieldID.Text;
                }

                Label labelSubfieldCode = (Label)marcNTextRepeater.Items[i].FindControl("labelSubfieldCode");
                if (labelSubfieldCode != null)
                {
                    nRow["SUBFIELD_CODE"] = labelSubfieldCode.Text;
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelSubfieldCode==null");
                }

                Label labelSubfieldLabel = (Label)marcNTextRepeater.Items[i].FindControl("labelSubfieldLabel");

                if (labelSubfieldLabel != null)
                {
                    nRow["SUBFIELD_LABEL"] = labelSubfieldLabel.Text;
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelSubfieldLabel==null");
                }

                Label labelSubfieldType = (Label)marcNTextRepeater.Items[i].FindControl("labelSubfieldType");
                if (labelSubfieldType != null)
                {
                    nRow["SUBFIELD_TYPE"] = labelSubfieldType.Text;
                    FCKeditor fckValue = (FCKeditor)marcNTextRepeater.Items[i].FindControl("NTEXT_Value");
                    if (fckValue != null)
                    {
                        nRow["SUBFIELD_VALUE"] = HttpUtility.HtmlDecode(fckValue.Value);
                    }
                }
                else
                {
                    throw new Exception("Error: save_MetaContentData labelSubfieldType==null");
                }

                marcTable.Rows.Add(nRow);
            }
            #endregion marcNTextRepeater
            _MetaContent.bind_TableDataToMarc(ref marcTable);
            Session["METADATA"] = _MetaContent.OuterXml;
        }
        catch (Exception ex)
        {
            throw new Exception("Data error:" + ex.Message + "\n" + ex.InnerException);
        }
    }