private void PublishResponseToControl(XElement publishElement, IServerBoundControl control)
        {
            try
            {
                PublishResponseToServerControl(publishElement, control);

                control.PublishXml(publishElement);

                if (control is IValueDestination)
                {
                    PublishResponseToValueDestinationControl(control, publishElement);
                }

                if (control is IEditableControl)
                {
                    PublishResponseToEditableControl(control, publishElement);
                }

                if (control is IFieldErrorMessage)
                {
                    PublishResponseToFieldErrorMessageControl(control, publishElement);
                }

                if (control is IFieldLabel)
                {
                    PublishResponseToFieldLabelControl(control, publishElement);
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }
        private void RegisterServerBoundControl(IServerBoundControl control)
        {
            string id = control.FieldId;

            if (id != null)
            {
                // Check for attribute suffix, field_@DayOfWeek or field_@label
                if (control is IEditableControl && id.IndexOf(".@") > 0)
                {
                    int p = id.IndexOf(".@");

                    if (p + 2 < id.Length)
                    {
                        string property = id.Substring(p + 2);

                        id = id.Substring(0, p);

                        if (!PropertyFields.ContainsKey(id))
                        {
                            PropertyFields.Add(id, new Dictionary <IEditableControl, string>());
                        }

                        PropertyFields[id].Add((IEditableControl)control, property);
                    }
                }

                Controls.Add(control);
            }
        }
        private void PublishResponseToServerControl(XElement publishElement, IServerBoundControl control)
        {
            bool?hidden = null;

            if (publishElement.Attribute(Common.PublishFieldHidden) != null)
            {
                hidden = Common.boolValue(publishElement.Attribute(Common.PublishFieldHidden).Value);
                control.SetVisible(!(bool)hidden);
            }
        }
        private void PublishResponseToFieldErrorMessageControl(IServerBoundControl control, XElement publishElement)
        {
            IFieldErrorMessage errorMessageControl = (IFieldErrorMessage)control;

            if (publishElement.Attribute(Common.valid) != null && !IsLoading)
            {
                bool isFieldValueValid = Common.boolValue(publishElement.Attribute(Common.valid).Value);

                errorMessageControl.IsFieldValueValid = isFieldValueValid;

                if (isFieldValueValid)
                {
                    errorMessageControl.HideError();
                }
            }
        }
        private void PublishResponseToFieldLabelControl(IServerBoundControl control, XElement publishElement)
        {
            var    labelControl = control as IFieldLabel;
            string label        = publishElement.GetAttributeValue(Common.PublishFieldLabel);

            if (label == null && string.IsNullOrEmpty(labelControl.LabelText))
            {
                label = control.FieldId;
            }

            if (label != null)
            {
                labelControl.SetLabel(label);
            }

            //if (publishElement.Attribute(Common.PublishFieldLabel) != null)
            //{
            //    //check for a field with this id + _label
            //    string l = control.FieldId.Replace(".", "_") + "_label";
            //    /**/
            //    object o = _visualContainer.FindName(l);
            //    if (o != null)
            //    {
            //        UIElement fieldUI = (UIElement)control;
            //        FrameworkElement labelUI = (FrameworkElement)o;

            //        if (o is LabelEx)
            //            ((LabelEx)o).Target = fieldUI as FrameworkElement;

            //        if (o is IFieldLabel)
            //        {
            //            IFieldLabel L = (IFieldLabel)o;
            //            L.SetLabel(label);
            //        }

            //        //regardless of whether its a 'managed' label, bind its visibility to field
            //        Binding b = new Binding();
            //        b.Source = fieldUI;
            //        b.Path = new PropertyPath("Visibility");
            //        labelUI.SetBinding(FrameworkElement.VisibilityProperty, b);
            //    }
            //}
        }
        private void PublishResponseToEditableControl(IServerBoundControl control, XElement publishElement)
        {
            IEditableControl editableControl = (IEditableControl)control;

            if (publishElement.Attribute(Common.PublishFieldDisabled) != null)
            {
                editableControl.SetEditable(!Common.boolValue(publishElement.Attribute(Common.PublishFieldDisabled).Value));
            }

            if (HasPropertyFields)
            {
                string id = publishElement.Attribute(Common.IDAttrib).Value;

                if (FixedContext != null)
                {
                    id = id.Substring(FixedContext.Length + 1);
                }

                if (PropertyFields.ContainsKey(id))
                {
                    IDictionaryEnumerator de = PropertyFields[id].GetEnumerator();

                    while (de.MoveNext())
                    {
                        foreach (XAttribute a in publishElement.Attributes())
                        {
                            if (a.Name == de.Value.ToString())
                            {
                                IEditableControl ec = (IEditableControl)de.Key;
                                ec.SetValue(a.Value);
                                break;
                            }
                        }
                    }
                }
            }
        }
        private void PublishResponseToValueDestinationControl(IServerBoundControl control, XElement publishElement)
        {
            var valueControl = control as IValueDestination;

            if (publishElement.Attribute(Common.PublishFieldHint) != null)
            {
                string hint = publishElement.Attribute(Common.PublishFieldHint).Value;
                valueControl.SetHint(hint);
            }

            if (publishElement.Attribute(Common.ValueIsNull) != null && Common.boolValue(publishElement.Attribute(Common.ValueIsNull).Value))
            {
                valueControl.SetNull();

                if (control is IContainerSummaryText)
                {
                    CheckDirtySummaryContainer((IContainerSummaryText)control);
                }
            }
            else if (publishElement.Attribute(Common.PublishFieldValue) != null)
            {
                string textVal = publishElement.Attribute(Common.PublishFieldValue).Value;

                if (textVal == Common.LongDataValueIndicator)
                {
                    textVal = publishElement.Value;
                }

                valueControl.SetValue(textVal);

                if (control is IContainerSummaryText)
                {
                    CheckDirtySummaryContainer((IContainerSummaryText)control);
                }
            }
        }
        private void PublishResponseToControl(XElement publishElement, IServerBoundControl control)
        {
            try
            {
                PublishResponseToServerControl(publishElement, control);

                control.PublishXml(publishElement);

                if (control is IValueDestination)
                    PublishResponseToValueDestinationControl(control, publishElement);

                if (control is IEditableControl)
                    PublishResponseToEditableControl(control, publishElement);

                if (control is IFieldErrorMessage)
                    PublishResponseToFieldErrorMessageControl(control, publishElement);

                if (control is IFieldLabel)
                    PublishResponseToFieldLabelControl(control, publishElement);
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }
        private void RegisterServerBoundControl(IServerBoundControl control)
        {
            string id = control.FieldId;

            if (id != null)
            {
                // Check for attribute suffix, field_@DayOfWeek or field_@label
                if (control is IEditableControl && id.IndexOf(".@") > 0)
                {
                    int p = id.IndexOf(".@");

                    if (p + 2 < id.Length)
                    {
                        string property = id.Substring(p + 2);

                        id = id.Substring(0, p);

                        if (!PropertyFields.ContainsKey(id))
                            PropertyFields.Add(id, new Dictionary<IEditableControl, string>());

                        PropertyFields[id].Add((IEditableControl)control, property);
                    }
                }

                Controls.Add(control);
            }
        }
        private void PublishResponseToValueDestinationControl(IServerBoundControl control, XElement publishElement)
        {
            var valueControl = control as IValueDestination;

            if (publishElement.Attribute(Common.PublishFieldHint) != null)
            {
                string hint = publishElement.Attribute(Common.PublishFieldHint).Value;
                valueControl.SetHint(hint);
            }

            if (publishElement.Attribute(Common.ValueIsNull) != null && Common.boolValue(publishElement.Attribute(Common.ValueIsNull).Value))
            {
                valueControl.SetNull();

                if (control is IContainerSummaryText)
                    CheckDirtySummaryContainer((IContainerSummaryText)control);
            }
            else if (publishElement.Attribute(Common.PublishFieldValue) != null)
            {
                string textVal = publishElement.Attribute(Common.PublishFieldValue).Value;

                if (textVal == Common.LongDataValueIndicator)
                    textVal = publishElement.Value;

                valueControl.SetValue(textVal);

                if (control is IContainerSummaryText)
                    CheckDirtySummaryContainer((IContainerSummaryText)control);
            }
        }
        private void PublishResponseToServerControl(XElement publishElement, IServerBoundControl control)
        {
            bool? hidden = null;

            if (publishElement.Attribute(Common.PublishFieldHidden) != null)
            {
                hidden = Common.boolValue(publishElement.Attribute(Common.PublishFieldHidden).Value);
                control.SetVisible(!(bool)hidden);
            }
        }
        private void PublishResponseToFieldLabelControl(IServerBoundControl control, XElement publishElement)
        {
            var labelControl = control as IFieldLabel;
            string label = publishElement.GetAttributeValue(Common.PublishFieldLabel);

            if (label == null && string.IsNullOrEmpty(labelControl.LabelText))
                label = control.FieldId;

            if (label != null)
                labelControl.SetLabel(label);

            //if (publishElement.Attribute(Common.PublishFieldLabel) != null)
            //{
            //    //check for a field with this id + _label
            //    string l = control.FieldId.Replace(".", "_") + "_label";
            //    /**/
            //    object o = _visualContainer.FindName(l);
            //    if (o != null)
            //    {
            //        UIElement fieldUI = (UIElement)control;
            //        FrameworkElement labelUI = (FrameworkElement)o;

            //        if (o is LabelEx)
            //            ((LabelEx)o).Target = fieldUI as FrameworkElement;

            //        if (o is IFieldLabel)
            //        {
            //            IFieldLabel L = (IFieldLabel)o;
            //            L.SetLabel(label);
            //        }

            //        //regardless of whether its a 'managed' label, bind its visibility to field
            //        Binding b = new Binding();
            //        b.Source = fieldUI;
            //        b.Path = new PropertyPath("Visibility");
            //        labelUI.SetBinding(FrameworkElement.VisibilityProperty, b);
            //    }
            //}
        }
        private void PublishResponseToFieldErrorMessageControl(IServerBoundControl control, XElement publishElement)
        {
            IFieldErrorMessage errorMessageControl = (IFieldErrorMessage)control;

            if (publishElement.Attribute(Common.valid) != null && !IsLoading)
            {
                bool isFieldValueValid = Common.boolValue(publishElement.Attribute(Common.valid).Value);

                errorMessageControl.IsFieldValueValid = isFieldValueValid;

                if (isFieldValueValid)
                    errorMessageControl.HideError();
            }
        }
        private void PublishResponseToEditableControl(IServerBoundControl control, XElement publishElement)
        {
            IEditableControl editableControl = (IEditableControl)control;

            if (publishElement.Attribute(Common.PublishFieldDisabled) != null)
                editableControl.SetEditable(!Common.boolValue(publishElement.Attribute(Common.PublishFieldDisabled).Value));

            if (HasPropertyFields)
            {
                string id = publishElement.Attribute(Common.IDAttrib).Value;

                if (FixedContext != null)
                    id = id.Substring(FixedContext.Length + 1);

                if (PropertyFields.ContainsKey(id))
                {
                    IDictionaryEnumerator de = PropertyFields[id].GetEnumerator();

                    while (de.MoveNext())
                    {
                        foreach (XAttribute a in publishElement.Attributes())
                        {
                            if (a.Name == de.Value.ToString())
                            {
                                IEditableControl ec = (IEditableControl)de.Key;
                                ec.SetValue(a.Value);
                                break;
                            }
                        }
                    }
                }
            }
        }