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

InPlace Editing - Dynamically add RequiredFieldValidator controls

2 Answers 111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Thad
Top achievements
Rank 2
Thad asked on 05 Nov 2010, 03:30 PM
Hello,

Does anyone know which event on the RadGrid I can dynamically add RequiredFieldValidators and have them fire client side?  Physically placing the Validators inside of the <EditTemplate> works like a champ.

Right now we are using the ItemDataBound, and also have tried the ItemCreated event.  Those seem to be the only places to inject validators when the update button is clicked...

The .AddValidator method is an extension method tied to controls to determine which validation message to assign to the validator and then dynamically add the validator.  This code works on the rest of the site, just not for InPlace editing of grids so far.

Code to add the validator to the page.  Maybe I'm adding the validator to the wrong place, but I'd think that control.Parent would represent the "cell" the control lives in.
// control is the Control validation is being added to
control.Parent.Controls.Add(requiredFieldValidator);

Sample ItemDataBound method
protected void rgPmpDetails_ItemDataBound(object sender, GridItemEventArgs e)
{
    const string valGroup = "vgEditPmpDetails";
 
    GridHelper.AddEditValidator(e, "rntbCreditPercentGrid", valGroup, "Premium Credit");
    GridHelper.SetFocusToFirstControlWhenInEditMode(e, "rntbCreditPercentGrid");
}

And the GridHelper function does this:
public static void AddEditValidator(GridItemEventArgs e, string controlId, string validationGroup, string fieldName, bool isMultiSelect = false)
{
    if ((e.Item is GridEditableItem) && e.Item.IsInEditMode)
    {
        Control control = e.Item.FindControl(controlId);
        if (control != null)
        {
 
            if (control is TextBox)
            {
                (control as TextBox).ValidationGroup = validationGroup;
                (control as TextBox).AddValidator(fieldName);
            }
            else if (control is DropDownList)
            {
                (control as DropDownList).ValidationGroup = validationGroup;
                (control as DropDownList).AddValidator(fieldName);
            }
            else if (control is RadNumericTextBox)
            {
                (control as RadNumericTextBox).ValidationGroup = validationGroup;
                (control as RadNumericTextBox).AddValidator(fieldName);
            }
            else if (control is RadComboBox)
            {
                (control as RadComboBox).ValidationGroup = validationGroup;
                (control as RadComboBox).AddValidator(fieldName, isMultiSelect);
            }
            else if (control is RadDatePicker)
            {
                (control as RadDatePicker).DateInput.ValidationGroup = validationGroup;
                (control as RadDatePicker).AddValidator(fieldName);
            }
            else
            {
                throw new NotImplementedException("Controls of type [" + control.GetType() + "] have not been implemented in GridHelper.AddEditValidator.");
            }
        }
 
        ImageButton imageButton = e.Item.FindControl("UpdateButton") as ImageButton;
        if (imageButton != null)
        {
            imageButton.ValidationGroup = validationGroup;
        }
    }
}

2 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 10 Nov 2010, 04:36 PM
Hi Thad,

The valudators should beed in the ItemCreated event to the grid cells as described in this article.
Can you check it out and see if the solution provided there works for you and what differs in your case?

Best wishes,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Thad
Top achievements
Rank 2
answered on 11 Nov 2010, 07:31 PM
Perfect!  I'll try that and get back to you.

Thad
Tags
Grid
Asked by
Thad
Top achievements
Rank 2
Answers by
Iana Tsolova
Telerik team
Thad
Top achievements
Rank 2
Share this question
or