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 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();
            }
        }