private Panel BuildEditControl(GridControlEditMode mode)
        {
            List <Control> controls = new List <Control>();

            if (this.SelectedItem == null &&
                mode == GridControlEditMode.Edit)
            {
                return(new Panel());
            }

            WebUtilities.Controls.Table table = new WebUtilities.Controls.Table();

            foreach (string field in this.Fields)
            {
                WebUtilities.Controls.TableRow tableRow =
                    new WebUtilities.Controls.TableRow();

                WebUtilities.Controls.TableCell tableCellFieldName =
                    new WebUtilities.Controls.TableCell();
                WebUtilities.Controls.TableCell tableCellFieldValue =
                    new WebUtilities.Controls.TableCell();

                Label lblFieldName = new Label();
                lblFieldName.Name = field;

                //object value = this.SelectedItem.GetValue(field);

                Control control = null;

                control = BuildControl(field, mode == GridControlEditMode.Edit);

                control.ID = this.ID + "_" + field;

                controls.Add(control);

                tableCellFieldName.Controls.Add(lblFieldName);
                tableCellFieldValue.Controls.Add(control);

                tableRow.Cells.Add(tableCellFieldName);
                tableRow.Cells.Add(tableCellFieldValue);

                table.Rows.Add(tableRow);
            }

            // Run through all additional fields.
            foreach (AdditionalField <T> field in this.AdditionalFields)
            {
                if (!field.ShowInEdit)
                {
                    continue;
                }

                if (field.Condition != null && field.Condition(this.SelectedItem) == false)
                {
                    continue;
                }

                WebUtilities.Controls.TableRow tableRow = new WebUtilities.Controls.TableRow();

                WebUtilities.Controls.TableCell tableCellFieldName =
                    new WebUtilities.Controls.TableCell();
                WebUtilities.Controls.TableCell tableCellFieldValue =
                    new WebUtilities.Controls.TableCell();

                Label lblFieldName = new Label();
                lblFieldName.Name = field.Name;

                Control value = field.Render.Invoke(
                    mode == GridControlEditMode.Edit ?
                    this.SelectedItem :
                    null
                    );

                if (value == null)
                {
                    continue;
                }

                controls.Add(value);

                tableCellFieldName.Controls.Add(lblFieldName);
                tableCellFieldValue.Controls.Add(value);

                tableRow.Cells.Add(tableCellFieldName);
                tableRow.Cells.Add(tableCellFieldValue);

                table.Rows.Add(tableRow);
            }

            WebUtilities.Controls.TableRow tableRowButtons =
                new WebUtilities.Controls.TableRow();

            WebUtilities.Controls.TableCell tableCellButtons =
                new WebUtilities.Controls.TableCell();
            tableCellButtons.ColumnSpan      = 2;
            tableCellButtons.HorizontalAlign = HorizontalAlign.Right;

            Button defaultButton = null;

            if (mode == GridControlEditMode.Add)
            {
                Button btnAddConfirm = new Button();
                btnAddConfirm.ID     = this.ID + "BoxAdd";
                btnAddConfirm.Name   = "Add";
                btnAddConfirm.Click += btnAddConfirm_Click;

                defaultButton = btnAddConfirm;

                tableCellButtons.Controls.Add(btnAddConfirm);
            }
            else
            {
                Button btnSave = new Button();
                btnSave.ID     = this.ID + "BoxSave";
                btnSave.Name   = "Save";
                btnSave.Click += btnSave_Click;

                defaultButton = btnSave;

                tableCellButtons.Controls.Add(btnSave);
            }

            Button btnCancel = new Button();

            btnCancel.Name = "Cancel";

            tableCellButtons.Controls.Add(btnCancel);

            tableRowButtons.Cells.Add(tableCellButtons);

            table.Rows.Add(tableRowButtons);

            dragBox       = new Box();
            dragBox.ID    = this.ID + mode.ToString();
            dragBox.Title = mode.ToString() + (this.Items.Count > 0 ? this.Items[0].GetType().Name : "");
            dragBox.TitleLanguageLabel = true;
            dragBox.Dragable           = true;
            dragBox.Visible            = true;

            dragBox.Controls.Add(table);

            Panel panel = new Panel();

            panel.Controls.Add(dragBox);

            foreach (Control control in controls)
            {
                if (control.GetType() == typeof(TextBox))
                {
                    TextBox textBox = (TextBox)control;

                    textBox.Button = defaultButton.ID;
                }
            }

            return(panel);
        }
        protected bool Validate(BaseItem <T> element, GridControlEditMode mode)
        {
            bool result = true;

            // Run through all fields.
            foreach (string field in this.Fields)
            {
                // Check if a string max value for this field is defined.
                if (!this.GridFieldsStringMaxLength.ContainsKey(field))
                {
                    continue;
                }

                // Get the item's value of this field.
                string value = element.GetValue <string>(field);

                // Check if the value's length is shorter than the max string length.
                if (value.Length > this.GridFieldsStringMaxLength[field])
                {
                    if (this.dragBox == null)
                    {
                        this.Controls.Add(BuildEditControl(mode));
                    }

                    string errorText = base.LanguageManager.GetText("MaxLengthExeeded").
                                       Replace("###FIELD###", base.LanguageManager.GetText(field)).
                                       Replace("###MAXLENGTH###", this.GridFieldsStringMaxLength[field].ToString());

                    this.dragBox.ShowError2(errorText);

                    result = false;
                }
            }

            // Run through all additional fields.
            foreach (AdditionalField <T> additionalField in this.AdditionalFields)
            {
                // Check if a string max value for this field is defined.
                if (!this.GridFieldsStringMaxLength.ContainsKey(additionalField.Name))
                {
                    continue;
                }

                // Check if the additional field is an edit field.
                if (!additionalField.ShowInEdit)
                {
                    continue;
                }

                string value = additionalField.GetValue(element as BaseItem <T>).ToString();

                // Check if the value's length is shorter than the max string length.
                if (value.Length > this.GridFieldsStringMaxLength[additionalField.Name])
                {
                    if (this.dragBox == null)
                    {
                        this.Controls.Add(BuildEditControl(mode));
                    }

                    string errorText = base.LanguageManager.GetText("MaxLengthExeeded").
                                       Replace("###FIELD###", base.LanguageManager.GetText(additionalField.Name)).
                                       Replace("###MAXLENGTH###", this.GridFieldsStringMaxLength[additionalField.Name].ToString());

                    this.dragBox.ShowError2(errorText);

                    result = false;
                }
            }

            if (this.HasAdditionalFieldError)
            {
                result = false;
            }

            // Return the result.
            return(result);
        }