RadGrid and CustomValidator with AJAX

11 posts, 0 answers
  1. Stuart
    Stuart avatar
    9 posts
    Member since:
    May 2009

    Posted 22 May 2009 Link to this post

    Hi,

    I'm using a radgrid with a templated edit form and popup edit mode. Most of my validators are requireduiredfieldvalidators but one of my fields need to be validated agains the database. I therefore need to use a CustomValidator which is OK but if I do the validation server side then the error is not shown at the same time as the client side validator errors in the validationsummary.

    Therefore I'm trying to call an AJAX method in the custom validator:

    ClientValidationFunction="InitiateAsyncRequest('this.id')";

    This calls the a server side method:

      public void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)

    In this method I would like to perform the validation and then set the IsValid flag on the customervalidator. The problem I am having is finding the CustomValidator control in the AjaxRequest method on the server. The SelectedItems property on the RadGrid is empy and RadGrid.FindControl("Customvalidator1") returns null. Is this possible? Or is there an easier way to do this?

    Thanks, Stuart


  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 25 May 2009 Link to this post

    Hi Stuart,

    Try accessing your custom validator as follows:

    ((GridDataItem)(RadGrid1.EditItems[0])).EditFormItem.FindControl("CustomValidator1"

    I hope this helps.

    All the best,
    Tsvetoslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Stuart
    Stuart avatar
    9 posts
    Member since:
    May 2009

    Posted 26 May 2009 Link to this post

    Hi,

    Unfortunately RadGrid1.EditItems is empty.

    ? RadGrid1.EditItems

    {Telerik.Web.UI.GridItemCollection}

    Count: 0

    IsReadOnly: false

    IsSynchronized: false

    SyncRoot: {object}


    ? RadGrid1.SelectedItems

    {Telerik.Web.UI.GridItemCollection}

    Count: 0

    IsReadOnly: false

    IsSynchronized: false

    SyncRoot: {object}


    Thanks,

    Stuart

  5. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 26 May 2009 Link to this post

    Hi Stuart,

    If you have at least one item in edit mode then the EditItems collection should not be empty.

    Please, take a look at the attached sample for comparison with your implementation and let us know of the result.

    Best Regards,
    Tsvetoslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Stuart
    Stuart avatar
    9 posts
    Member since:
    May 2009

    Posted 27 May 2009 Link to this post

    Sorry, yes EditItems does find the value when editing an item, the only problem is finding the customvalidator when inserting an item. I cannot find an equivalent collection to EditItems when adding a new record.

    Thanks,

    Stuart 
  7. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 27 May 2009 Link to this post

    Hello Stuart,

    You need to obtain a reference to the insert item and then use the FindControl method to get hold of the validator:

     CustomValidator validator = RadGrid1.MasterTableView.GetInsertItem().FindControl("SendNotificationToValidator"as CustomValidator; 


    All the best,
    Tsvetoslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 16 Jul 2009 Link to this post

    Hi,

    I have the same problem.
    I try to validate an insert form.
    When I check (was my first thought) Grid1.EditItems - I get nothing.
    This brought me to this thread.
    Than I tried the approach here Grid1.MasterTableView.GetInsertItem()
    This returns
    "Insert item is available only when grid is in insert mode"
    Ok - my next idea - the master is not in insert - it is one of the childs
    So I tried this Grid1.MasterTableView.DetailTables[1].GetInsertItem - the same result!!!
    And yes index 1 is correct - I have to detail tables.

    By the way - it is a bad thing that GridTableView can't have an ID.
    So in this case I user .. DetailTables[1] - and get it running somehow.
    Later the customer asks to put this detail table in the first position -- and the I have to "renumber" all the code behind.

    But that's a different thing - my current problem - how can I access the controls in the insert form from a validator handler?

    Regards

    Manfred
  9. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 16 Jul 2009 Link to this post

    Hello ManniAT,

    The GetInsertItem() method of the GridTableView can be used to get reference to the GridDataInsertItem when the insertion form is displayed.

    To access the GridDataInsertItem container from within event handler of a validator inside it, use the NamingContainer property of the validator object from in its server handler.

    Here is how this can be done for the edited items in the grid (see the SelectedIndexChanged handler in the code):
    http://demos.telerik.com/aspnet-ajax/grid/examples/programming/accessingcellsandrows/defaultcs.aspx

    Best regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  10. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 16 Jul 2009 Link to this post

    Hi Sebastian,

    thanks a lot - this did the trick :)
    ULTRAFAST support - really cool - you made my day!!!

    Here is the snippet made out of your tip:
    protected void cvProdukt_ServerValidate(object source, ServerValidateEventArgs args) {  
        CustomValidator cV = source as CustomValidator;  
        GridEditableItem gI = cV.NamingContainer as GridEditableItem;  
        MyCtrl mC=gI.FindControl("... 

    Thanks again

    Manfred
  11. James
    James avatar
    4 posts
    Member since:
    Sep 2010

    Posted 08 Sep 2014 in reply to Sebastian Link to this post

    Hi Sebastian,

    Uh...where's the reference to SelectedIndexChanged on the page you referenced:

    "Here is how this can be done for the edited items in the grid (see the SelectedIndexChanged handler in the code):
    http://demos.telerik.com/aspnet-ajax/grid/examples/programming/accessingcellsandrows/defaultcs.aspx"

    Can't find this as part of the examples appearing on the page you referenced...Because, I'd really like to find out how to reference a set of inserted items based upon the first inserted item's (a radcombobox) SelectedIndexChanged event...

    Jim
  12. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 11 Sep 2014 Link to this post

    Hi James,

    I would suggests you to use the approach below for achieving the required functionality:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                RadComboBox list = (e.Item as GridEditableItem)["ContactName"].FindControl("RadComboBox1") as RadComboBox;                      
                list.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(list_SelectedIndexChanged);
            }       
        }
      
        void list_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadComboBox rdcbx=(RadComboBox)o;
            GridEditableItem editedItem =rdcbx.NamingContainer as GridEditableItem;
            RadComboBox ddList = editedItem["TemplateColumn"].FindControl("RadComboBox2") as RadComboBox;
                 
        }



    Regards,
    Maria Ilieva
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017