private void BindCustomFields()
        {
            MetaObject obj = null;

            if (ObjectId > 0)
            {
                obj = MetaDataWrapper.LoadMetaObject(ObjectId, MetaClassName);
            }
            if (obj == null)
            {
                obj = MetaDataWrapper.NewMetaObject(ObjectId, MetaClassName);
            }

            MetaClass mc = obj.MetaClass;

            SortedList <int, MetaField> userMetaFields = new SortedList <int, MetaField>();

            foreach (MetaField field in mc.UserMetaFields)
            {
                if (ContainsMetaField(field.Name))
                {
                    int cur_weight = _mflist.IndexOf(field.Name);
                    userMetaFields.Add(cur_weight, field);
                }
            }

            foreach (MetaField field in userMetaFields.Values)
            {
                HtmlTableRow  row       = new HtmlTableRow();
                HtmlTableCell cellTitle = new HtmlTableCell();
                HtmlTableCell cellValue = new HtmlTableCell();

                cellTitle.VAlign    = "middle";
                cellTitle.InnerHtml = String.Format("<b>{0}</b>:", field.FriendlyName);
                object fieldValue = obj[field.Name];
                System.Web.UI.UserControl control = null;

                switch (field.DataType)
                {
                case MetaDataType.Binary:
                    cellValue.InnerText = "[BinaryData]";
                    break;

                case MetaDataType.File:
                    cellTitle.VAlign = "top";
                    control          = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/FileValue.ascx");
                    break;

                case MetaDataType.ImageFile:
                    cellTitle.VAlign = "top";
                    control          = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/ImageFileValue.ascx");
                    break;

                case MetaDataType.DateTime:
                    //control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/DateTimeValue.ascx");
                    Mediachase.UI.Web.Modules.EditControls.DateTimeValue control_datetime = (Mediachase.UI.Web.Modules.EditControls.DateTimeValue)Page.LoadControl("~/Modules/EditControls/DateTimeValue.ascx");
                    control_datetime.Path_JS = "../../Scripts/";
                    control = (System.Web.UI.UserControl)control_datetime;
                    break;

                case MetaDataType.Money:
                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/MoneyValue.ascx");
                    break;

                case MetaDataType.Float:
                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/FloatValue.ascx");
                    break;

                case MetaDataType.Integer:
                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/IntValue.ascx");
                    break;

                case MetaDataType.Boolean:
                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/BooleanValue.ascx");
                    break;

                case MetaDataType.Date:
                    //control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/DateValue.ascx");
                    Mediachase.UI.Web.Modules.EditControls.DateValue control_date = (Mediachase.UI.Web.Modules.EditControls.DateValue)Page.LoadControl("~/Modules/EditControls/DateValue.ascx");
                    control_date.Path_JS = "../../Scripts/";
                    control = (System.Web.UI.UserControl)control_date;
                    break;

                case MetaDataType.Email:
                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/EmailValue.ascx");
                    break;

                case MetaDataType.Url:
                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/URLValue.ascx");
                    break;

                case MetaDataType.ShortString:
                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/ShortStringValue.ascx");
                    break;

                case MetaDataType.LongString:
                    cellTitle.VAlign = "top";
                    control          = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/LongStringValue.ascx");
                    break;

                case MetaDataType.LongHtmlString:
                    cellTitle.VAlign = "top";
                    control          = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/LongHTMLStringValue.ascx");
                    break;

                case MetaDataType.DictionarySingleValue:
                case MetaDataType.EnumSingleValue:
                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/DictionarySingleValue.ascx");
                    ((DictionarySingleValue)control).InitControl(field.Id, (field.AllowNulls ? !field.IsRequired : field.AllowNulls));
                    break;

                case MetaDataType.DictionaryMultivalue:
                case MetaDataType.EnumMultivalue:
                    cellTitle.VAlign = "top";
                    control          = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/DictionaryMultivalue.ascx");
                    ((DictionaryMultivalue)control).InitControl(field.Id);
                    break;

                default:
                    if (fieldValue != null)
                    {
                        cellValue.InnerText = fieldValue.ToString();
                    }
                    break;
                }

                if (control != null)
                {
                    cellValue.Controls.Add(control);
                }

                row.Cells.Add(cellTitle);
                row.Cells.Add(cellValue);

                tblCustomFields.Rows.Add(row);

                if (control != null)
                {
                    ICustomField iCustomField = ((ICustomField)control);
                    iCustomField.FieldName = field.Name;
                    if (fieldValue != null)
                    {
                        iCustomField.Value = fieldValue;
                    }
                    iCustomField.AllowEmptyValues = !mc.GetFieldIsRequired(field);
                }
            }
        }
        private void LoadBlocksToCell(TableCell cell, ControlData[] blocks, string prefix, bool needToBind)
        {
            for (int i = 0; i < blocks.Length; i++)
            {
                if (!String.IsNullOrEmpty(blocks[i].Settings))
                {
                    XmlDocument settings = new XmlDocument();
                    settings.LoadXml(blocks[i].Settings);
                    XmlNode     nameNode   = settings.SelectSingleNode("MetaDataBlockViewControl/Name");
                    XmlNodeList mfNodeList = settings.SelectNodes("MetaDataBlockViewControl/MetaField");

                    // BlockHeader
                    BlockHeaderLightWithMenu blockHeader = (BlockHeaderLightWithMenu)LoadControl("~/Modules/BlockHeaderLightWithMenu.ascx");
                    blockHeader.ID = String.Concat(prefix, i.ToString());
                    cell.Controls.Add(blockHeader);

                    // Collapsible Table
                    Table collapsibleTable = new Table();
                    collapsibleTable.CssClass    = "ibn-stylebox-light text";
                    collapsibleTable.CellSpacing = 0;
                    collapsibleTable.CellPadding = 5;
                    collapsibleTable.Width       = Unit.Percentage(100);
                    collapsibleTable.ID          = String.Concat("tbl", prefix, i.ToString());
                    cell.Controls.Add(collapsibleTable);

                    blockHeader.CollapsibleControlId = collapsibleTable.ID;
                    blockHeader.AddText(nameNode.InnerText);

                    foreach (XmlNode mfNode in mfNodeList)
                    {
                        foreach (MetaField field in mc.UserMetaFields)
                        {
                            if (field.Name == mfNode.InnerText)
                            {
                                TableRow row = new TableRow();
                                collapsibleTable.Rows.Add(row);

                                TableCell cellTitle = new TableCell();
                                cellTitle.VerticalAlign = VerticalAlign.Middle;
                                cellTitle.CssClass      = "ibn-label";
                                cellTitle.Width         = Unit.Pixel(220);
                                cellTitle.Text          = String.Concat(field.FriendlyName, ":");
                                row.Cells.Add(cellTitle);

                                TableCell cellValue = new TableCell();
                                row.Cells.Add(cellValue);

                                object fieldValue = obj[field.Name];
                                System.Web.UI.UserControl control = null;

                                switch (field.DataType)
                                {
                                case MetaDataType.Binary:
                                    cellValue.Text = "[BinaryData]";
                                    break;

                                case MetaDataType.File:
                                    cellTitle.VerticalAlign = VerticalAlign.Top;
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/FileValue.ascx");
                                    break;

                                case MetaDataType.ImageFile:
                                    cellTitle.VerticalAlign = VerticalAlign.Top;
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/ImageFileValue.ascx");
                                    break;

                                case MetaDataType.DateTime:
                                    Mediachase.UI.Web.Modules.EditControls.DateTimeValue control_datetime = (Mediachase.UI.Web.Modules.EditControls.DateTimeValue)Page.LoadControl("~/Modules/EditControls/DateTimeValue.ascx");
                                    control_datetime.Path_JS = "../../Scripts/";
                                    control = (System.Web.UI.UserControl)control_datetime;
                                    break;

                                case MetaDataType.Money:
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/MoneyValue.ascx");
                                    break;

                                case MetaDataType.Float:
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/FloatValue.ascx");
                                    break;

                                case MetaDataType.Integer:
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/IntValue.ascx");
                                    break;

                                case MetaDataType.Boolean:
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/BooleanValue.ascx");
                                    break;

                                case MetaDataType.Date:
                                    Mediachase.UI.Web.Modules.EditControls.DateValue control_date = (Mediachase.UI.Web.Modules.EditControls.DateValue)Page.LoadControl("~/Modules/EditControls/DateValue.ascx");
                                    control_date.Path_JS = "../../Scripts/";
                                    control = (System.Web.UI.UserControl)control_date;
                                    break;

                                case MetaDataType.Email:
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/EmailValue.ascx");
                                    break;

                                case MetaDataType.Url:
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/URLValue.ascx");
                                    break;

                                case MetaDataType.ShortString:
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/ShortStringValue.ascx");
                                    break;

                                case MetaDataType.LongString:
                                    cellTitle.VerticalAlign = VerticalAlign.Top;
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/LongStringValue.ascx");
                                    break;

                                case MetaDataType.LongHtmlString:
                                    cellTitle.VerticalAlign = VerticalAlign.Top;
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/LongHTMLStringValue.ascx");
                                    break;

                                case MetaDataType.DictionarySingleValue:
                                case MetaDataType.EnumSingleValue:
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/DictionarySingleValue.ascx");
                                    ((DictionarySingleValue)control).InitControl(field.Id, (field.AllowNulls ? !field.IsRequired : field.AllowNulls));
                                    break;

                                case MetaDataType.DictionaryMultivalue:
                                case MetaDataType.EnumMultivalue:
                                    cellTitle.VerticalAlign = VerticalAlign.Top;
                                    control = (System.Web.UI.UserControl)Page.LoadControl("~/Modules/EditControls/DictionaryMultivalue.ascx");
                                    ((DictionaryMultivalue)control).InitControl(field.Id);
                                    break;

                                default:
                                    if (fieldValue != null)
                                    {
                                        cellValue.Text = fieldValue.ToString();
                                    }
                                    break;
                                }

                                if (control != null)
                                {
                                    cellValue.Controls.Add(control);

                                    if (field.DataType == MetaDataType.File)
                                    {
                                        ((FileValue)control).MetaClassName = MetaClassName;
                                        ((FileValue)control).ObjectId      = ObjectId;
                                    }
                                    else if (field.DataType == MetaDataType.ImageFile)
                                    {
                                        ((ImageFileValue)control).MetaClassName = MetaClassName;
                                        ((ImageFileValue)control).ObjectId      = ObjectId;
                                    }

                                    ICustomField iCustomField = ((ICustomField)control);
                                    iCustomField.FieldName = field.Name;
                                    if (fieldValue != null && needToBind)
                                    {
                                        iCustomField.Value = fieldValue;
                                    }
                                    iCustomField.AllowEmptyValues = !mc.GetFieldIsRequired(field);
                                }
                            }
                        }
                    }
                }
            }
        }