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

RadGrid and CustomValidator with AJAX

10 Answers 361 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stuart
Top achievements
Rank 1
Stuart asked on 22 May 2009, 01:37 PM
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


10 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 25 May 2009, 04:19 PM
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.
0
Stuart
Top achievements
Rank 1
answered on 26 May 2009, 11:18 AM
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

0
Tsvetoslav
Telerik team
answered on 26 May 2009, 03:07 PM
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.
0
Stuart
Top achievements
Rank 1
answered on 27 May 2009, 09:46 AM
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 
0
Tsvetoslav
Telerik team
answered on 27 May 2009, 01:52 PM
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.
0
ManniAT
Top achievements
Rank 2
answered on 16 Jul 2009, 01:01 PM
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
0
Sebastian
Telerik team
answered on 16 Jul 2009, 01:20 PM
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.
0
ManniAT
Top achievements
Rank 2
answered on 16 Jul 2009, 01:38 PM
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
0
GokuJim
Top achievements
Rank 1
answered on 08 Sep 2014, 12:35 PM
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
0
Maria Ilieva
Telerik team
answered on 11 Sep 2014, 11:14 AM
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.

 
Tags
Grid
Asked by
Stuart
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Stuart
Top achievements
Rank 1
ManniAT
Top achievements
Rank 2
Sebastian
Telerik team
GokuJim
Top achievements
Rank 1
Maria Ilieva
Telerik team
Share this question
or