private void InitializeFieldViews(SPField field, string webId, string listId)
        {
            SPSite _site = SPControl.GetContextSite(this.Context);
            SPWeb  _web  = _site.OpenWeb(new Guid(webId));
            SPList list  = _web.Lists[new Guid(listId)];

            foreach (SPView view in list.Views)
            {
                if ((view.Hidden || view.PersonalView) || !view.Type.Equals("HTML"))
                {
                    continue;
                }
                ListItem item   = new ListItem(view.Title, view.ID.ToString() + "|" + view.Url);
                string   viewId = string.Empty;

                if (field != null)
                {
                    CustomDropDownList f = field as CustomDropDownList;
                    if (Convert.ToString(f.GetCustomProperty(CustomDropDownList.VIEW)) != string.Empty)
                    {
                        viewId = Convert.ToString(f.GetCustomProperty(CustomDropDownList.VIEW));
                    }
                }

                if (((view.ID.ToString() == viewId) || (view.ID.ToString() + "|" + view.Url == viewId)))
                {
                    item.Selected = true;
                }
                ddlView.Items.Add(item);
            }

            ddlView.Items.Insert(0, new ListItem("", ""));
            ddlView.Visible = true;
            lbView.Visible  = true;
        }
        public void OnSaveChange(SPField field, bool isNewField)
        {
            CustomDropDownList _f = null;

            try { _f = field as CustomDropDownList; }
            catch { }
            SPSite _site = SPControl.GetContextSite(this.Context);
            SPWeb  _web  = _site.OpenWeb(new Guid(listTargetWeb.SelectedItem.Value));

            _f.LookupWebId = _web.ID;
            if (listTargetList.SelectedIndex < 0)
            {
                listTargetList.SelectedIndex = 0;
            }

            _f.LookupList  = listTargetList.SelectedItem.Value;
            _f.LookupField = listTargetColumn.SelectedItem.Value;
            _f.SetCustomProperty(CustomDropDownList.LINK, cbxLinkParent.Checked);
            _f.SetCustomProperty(CustomDropDownList.ALLOW_MULTIPLE, cbxMultipleValues.Checked);
            _f.SetCustomProperty(CustomDropDownList.SHOW_ALL_VALUES, cbxParentEmpty.Checked);
            if (cbxLinkParent.Checked && cbxLinkParent.Enabled)
            {
                //  if (listParentColumn.Items.Count != 0)
                {
                    _f.SetCustomProperty(CustomDropDownList.PARENT_COLUMN, listParentColumn.SelectedItem.Value);
                }

                /*  else
                 * {
                 *    _f.SetCustomProperty(CustomDropDownList.PARENT_COLUMN, "");
                 * }
                 * if (listLinkColumn.Items.Count != 0)*/
                {
                    _f.SetCustomProperty(CustomDropDownList.LINK_COLUMN, listLinkColumn.SelectedItem.Value);
                }

                /* else
                 * {
                 *   _f.SetCustomProperty(CustomDropDownList.LINK_COLUMN, "");
                 * }*/
            }
            else
            {
                _f.SetCustomProperty(CustomDropDownList.PARENT_COLUMN, "");
                _f.SetCustomProperty(CustomDropDownList.LINK_COLUMN, "");
            }

            _f.SetCustomProperty(CustomDropDownList.AUTO_COMPLETE, cbxAutoCompleteORFilter.Checked);

            _f.SetCustomProperty(CustomDropDownList.ADVANCE_SETTINGS, cbxAdvanceSettings.Checked);

            if (cbxAdvanceSettings.Checked)
            {
                if (!string.IsNullOrEmpty(ddlView.SelectedItem.Value))
                {
                    _f.SetCustomProperty(CustomDropDownList.VIEW, ddlView.SelectedItem.Value);
                }

                _f.SetCustomProperty(CustomDropDownList.SORT_BY_VIEW, chkSortByView.Checked);
                _f.SetCustomProperty(CustomDropDownList.ADDING_NEW_VALUES, chkAddingNewValues.Checked);
                _f.SetCustomProperty(CustomDropDownList.NEW_FORM, chkUseNewForm.Checked);

                string checked_additional_fields   = string.Empty;
                string unchecked_additional_fields = string.Empty;
                foreach (ListItem item in cblAdditionalFields.Items)
                {
                    if (item.Selected)
                    {
                        if (!string.IsNullOrEmpty(checked_additional_fields))
                        {
                            checked_additional_fields = checked_additional_fields + ";";
                        }
                        checked_additional_fields = checked_additional_fields + item.Value;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(unchecked_additional_fields))
                        {
                            unchecked_additional_fields = unchecked_additional_fields + ";";
                        }
                        unchecked_additional_fields = unchecked_additional_fields + item.Value;
                    }
                }
                _f.SetCustomProperty(CustomDropDownList.ADDITIONAL_FIELDS, checked_additional_fields);

                _f.EnsureAdditionalFields(checked_additional_fields, unchecked_additional_fields);

                string str = string.Empty;
                foreach (ListItem item in cblAdditionalFilters.Items)
                {
                    if (item.Selected)
                    {
                        if (!string.IsNullOrEmpty(str))
                        {
                            str = str + ";";
                        }
                        str = str + item.Value;
                    }
                }
                _f.SetCustomProperty(CustomDropDownList.ADDITIONAL_FILTERS, str);
            }

            _f.SetCustomProperty(CustomDropDownList.RELATIONSHIP_BEHAVIOR, cbxRelationshipBehavior.Checked);
            _f.SetCustomProperty(CustomDropDownList.RELATIONSHIP_BEHAVIOR_CASCADE, rdbCascadeDelete.Checked);
            if (!_f.AllowMultipleValues && cbxRelationshipBehavior.Checked)
            {
                if (rdbCascadeDelete.Checked)
                {
                    _f.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Cascade;
                }
                else if (rdbRestrictDelete.Checked)
                {
                    _f.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;
                }
                else
                {
                    _f.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.None;
                }

                _f.Indexed = true;
            }
            else
            {
                _f.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.None;
                _f.Indexed = false;
            }
        }
        public void InitializeWithField(SPField field)
        {
            EnsureChildControls();
            CustomDropDownList _f = null;

            try { _f = field as CustomDropDownList; }
            catch { }

            if (_f != null)
            {
                // this bit only happens when field is not null
                if (!IsPostBack)
                {
                    cbxMultipleValues.Checked = _f.AllowMultipleValues;
                    cbxLinkParent.Checked     = Convert.ToBoolean(_f.GetCustomProperty(CustomDropDownList.LINK));
                    cbxParentEmpty.Checked    = Convert.ToBoolean(_f.GetCustomProperty(CustomDropDownList.SHOW_ALL_VALUES));
                    if (cbxLinkParent.Checked)
                    {
                        InitialParentColumnValues_with_true();
                    }
                    else
                    {
                        InitialParentColumnValues_with_false();
                    }
                    cbxAutoCompleteORFilter.Checked = Convert.ToBoolean(_f.GetCustomProperty(CustomDropDownList.AUTO_COMPLETE));

                    TargetWebId          = _f.LookupWebId.ToString();
                    TargetListId         = _f.LookupList;
                    TargetColumnId       = _f.LookupField;
                    TargetParentColumnId = Convert.ToString(_f.GetCustomProperty(CustomDropDownList.PARENT_COLUMN));
                    TargetLinkColumnId   = Convert.ToString(_f.GetCustomProperty(CustomDropDownList.LINK_COLUMN));

                    cbxAdvanceSettings.Checked = Convert.ToBoolean(_f.GetCustomProperty(CustomDropDownList.ADVANCE_SETTINGS));

                    if (cbxAdvanceSettings.Checked)
                    {
                        cblAdditionalFields.Items.Clear();
                        cblAdditionalFilters.Items.Clear();
                        InitializeFieldAdvanceSettings(TargetWebId, TargetListId);
                        InitializeFieldViews(_f, TargetWebId, TargetListId);

                        if (!string.IsNullOrEmpty(Convert.ToString(_f.GetCustomProperty(CustomDropDownList.VIEW))))
                        {
                            // ddlView.SelectedIndex = ddlView.Items.IndexOf(ddlView.Items.FindByText(Convert.ToString(_f.GetCustomProperty(CustomDropDownList.VIEW))));
                            chkSortByView.Enabled = true;
                            chkSortByView.Checked = Convert.ToBoolean(_f.GetCustomProperty(CustomDropDownList.SORT_BY_VIEW));
                        }

                        chkAddingNewValues.Checked = Convert.ToBoolean(_f.GetCustomProperty(CustomDropDownList.ADDING_NEW_VALUES));

                        if (chkAddingNewValues.Checked)
                        {
                            chkUseNewForm.Enabled = true;
                            chkUseNewForm.Checked = Convert.ToBoolean(_f.GetCustomProperty(CustomDropDownList.NEW_FORM));
                        }

                        string add_fields = Convert.ToString(_f.GetCustomProperty(CustomDropDownList.ADDITIONAL_FIELDS));
                        if (!string.IsNullOrEmpty(add_fields))
                        {
                            foreach (string s in add_fields.Split(';'))
                            {
                                int index = cblAdditionalFields.Items.IndexOf(cblAdditionalFields.Items.FindByValue(s));
                                cblAdditionalFields.Items[index].Selected = true;
                            }
                        }

                        string add_filters = Convert.ToString(_f.GetCustomProperty(CustomDropDownList.ADDITIONAL_FILTERS));
                        if (!string.IsNullOrEmpty(add_filters))
                        {
                            foreach (string s in add_filters.Split(';'))
                            {
                                int index = cblAdditionalFilters.Items.IndexOf(cblAdditionalFilters.Items.FindByValue(s));
                                cblAdditionalFilters.Items[index].Selected = true;
                            }
                        }
                    }
                    else
                    {
                        cblAdditionalFields.Visible   = false;
                        lbAdditionalFilters.Visible   = false;
                        cblAdditionalFilters.Visible  = false;
                        chkSortByView.Visible         = false;
                        pnlConvertFromRegular.Visible = false;
                        ddlView.Visible            = false;
                        lbView.Visible             = false;
                        chkAddingNewValues.Visible = false;
                        chkUseNewForm.Visible      = false;
                    }

                    if (_f.AllowMultipleValues)
                    {
                        cbxRelationshipBehavior.Enabled = false;
                    }

                    if (Convert.ToBoolean(_f.GetCustomProperty(CustomDropDownList.RELATIONSHIP_BEHAVIOR)))
                    {
                        cbxRelationshipBehavior.Checked = true;
                        rdbRestrictDelete.Enabled       = true;
                        rdbCascadeDelete.Enabled        = true;

                        if (Convert.ToBoolean(_f.GetCustomProperty(CustomDropDownList.RELATIONSHIP_BEHAVIOR_CASCADE)))
                        {
                            rdbRestrictDelete.Checked = false;
                            rdbCascadeDelete.Checked  = true;
                        }
                        else
                        {
                            rdbRestrictDelete.Checked = true;
                            rdbCascadeDelete.Checked  = false;
                        }
                    }
                    else
                    {
                        cbxRelationshipBehavior.Checked = false;
                        rdbRestrictDelete.Enabled       = false;
                        rdbRestrictDelete.Checked       = true;
                        rdbCascadeDelete.Enabled        = false;
                        rdbCascadeDelete.Checked        = false;
                    }
                }
            }

            // this bit must always happen, even when field is null
            if (!IsPostBack)
            {
                SetTargetWeb();
                SetControlVisibility();
            }
        }