InPlace Editing - Dynamically add RequiredFieldValidator controls

3 posts, 0 answers
  1. Thad
    Thad avatar
    106 posts
    Member since:
    Sep 2010

    Posted 05 Nov 2010 Link to this post

    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. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 10 Nov 2010 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Thad
    Thad avatar
    106 posts
    Member since:
    Sep 2010

    Posted 11 Nov 2010 Link to this post

    Perfect!  I'll try that and get back to you.

    Thad
Back to Top