This is a migrated thread and some comments may be shown as answers.

[Solved] Required field validator

2 Answers 181 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Dario Zanelli
Top achievements
Rank 1
Dario Zanelli asked on 26 Jan 2010, 09:00 AM
Hi all!
I am developing a Required Field Validator for a RadCombobox. The RadCombobox appears when I am modifying an item of a RadGrid, so when the CreateColumnEditor event fires.
My problem is: when I add a new item to my table, there a re no problem, while when I am editing ad existing item the value of the RadComboBox is not taken, so it gives a database exception of violation of integrity contraints.
Here there is the most significative part of my class.
When debugging I can see that the value of the radcomboboxes are wrong, even if the right item is selected.
The validation is done clientside with a javascript, because the RequiredFieldValidator wasn't working (could you please explain me why it wan't? It is in the commented code).

public class ComboEditor : GridDropDownListColumnEditor
{
    private RadComboBox combo;
    private String _datafield;
    private bool _mandatory;
    private bool _enableChange;
    private Unit _width;
    //private RequiredFieldValidator _reqValidator;
    private CustomValidator _reqValidator;

    public ComboEditor(GridDropDownColumn owner, bool mandatory, String datafield, bool enableChange, Unit width)
        : base(owner)
    {
        this._datafield = datafield;
        this._mandatory = mandatory;
        this._enableChange = enableChange;
        this._width = width;
    }


    public RadComboBox ComboControl
    {
        get
            return base.ComboBoxControl;
    }

    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override int SelectedIndex
    {
        get
            return base.SelectedIndex;
        set
            base.SelectedIndex = value;
    }

    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override string SelectedValue
    {
        get
            return base.SelectedValue;
        set
            base.SelectedValue = value;
    }

    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override string SelectedText
    {
        get
            return base.SelectedText;
        set
            base.SelectedText = value;
    }

    protected override void LoadControlsFromContainer()
    {
        this.combo = this.ContainerControl.Controls[0] as RadComboBox;
    }

    protected override void AddControlsToContainer()
    {
        HtmlGenericControl divCombo = new HtmlGenericControl("DIV");
        divCombo.Attributes.Add("style", "padding-bottom: 3px;");

        if (this.ContainerControl.Parent != null)
        {
            foreach (GridTableRow row in this.ContainerControl.Parent.Parent.Controls)
                (row.Controls[0] as GridEditFormItem.EditFormTableCell).VerticalAlign = VerticalAlign.Top;
        }

        this.combo.NoWrap = false;
        this.combo.MarkFirstMatch = true;
        this.combo.AllowCustomText = false;
        this.combo.Skin = "WebBlue";
        this.combo.CausesValidation = true;
        this.combo.Width = this._width;
        this.combo.ToolTip = "Select " + this._datafield + "...";
        this.combo.ID = "cmb" + this._datafield.Replace(" ", "");
        divCombo.Controls.Add(this.combo);
        this.ContainerControl.Controls.Add(divCombo);

        if (this._mandatory)
        {
            this._reqValidator = new CustomValidator();
            this._reqValidator.ID = "req" + this._datafield.Replace(" ", "");
            this._reqValidator.ErrorMessage = "  This field is required.";
            this._reqValidator.ControlToValidate = this.combo.ID;
            this._reqValidator.ClientValidationFunction = "ValidateComboBox";
            this._reqValidator.EnableClientScript = true;
            this.ContainerControl.Controls.Add(this._reqValidator);
            
            /*this._reqValidator = new RequiredFieldValidator();
            this._reqValidator.ID = "pippo";
            this._reqValidator.ErrorMessage = "This field is required.";
            this._reqValidator.ControlToValidate = this.combo.ID;
            this._reqValidator.InitialValue = "";
            this.ContainerControl.Controls.Add(this._reqValidator);  */
        }       
    }

    protected override void CreateControls()
    {
        base.CreateControls();
        base.ControlsCreated = true;
        this.combo = base.ComboBoxControl;
    }

}

2 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 28 Jan 2010, 01:29 PM
Hello Dario Zanelli,

How have you bound the SelectedValue property of the RadComboBox to a field in the data source? 

In order to make the RequiredFieldValidator to work with RadComboBox, you need to use the ClientID of the latter, i.e.:

this._reqValidator.ControlToValidate = this.combo.ClientID;

Greetings,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Dario Zanelli
Top achievements
Rank 1
answered on 28 Jan 2010, 02:42 PM
Hi Simon,
thanks for the answer. Now I am trying a different solution but I will keep your suggestion for the future.

Regards,

Dario
Tags
ComboBox
Asked by
Dario Zanelli
Top achievements
Rank 1
Answers by
Simon
Telerik team
Dario Zanelli
Top achievements
Rank 1
Share this question
or