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

How can I validate the controls?

7 Answers 213 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stavros
Top achievements
Rank 1
Stavros asked on 04 Nov 2008, 08:17 AM
Is there any way to validate the text that is entered either in the filter or when editing a record?(for example a datetime field generates error if the user enters something else).
Thank you.

7 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 04 Nov 2008, 02:50 PM
Hello Stavros,

Try the following approach:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    GridFilteringItem item = e.Item as GridFilteringItem; 
    if (item != null
    { 
        yourValidator validator1 = new yourValidator(); 
        validator1.ID = "validator1"
        validator1.ControlToValidate = (item["yourColumn"].Controls[0] as TextBox).ID; 
        item["yourColumn"].Controls.Add(validator1); 
    } 

Adding a validator to an edit field in Telerik RadGrid

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
maggi
Top achievements
Rank 1
answered on 24 Nov 2008, 06:42 AM
Hi Daniel,

    I tried your approach but am recieveing an error

The ControlToValidate property of 'validator1' cannot be blank.


Can you tell me what i might be missing.

Thanks
maggi
0
Shinu
Top achievements
Rank 2
answered on 24 Nov 2008, 11:42 AM
Hi Maggi,

Try the following code snippet in the ItemDataBound event and see if it is working.

CS:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridFilteringItem) 
        { 
            GridFilteringItem item = e.Item as GridFilteringItem; 
            if (item != null) 
            { 
                RequiredFieldValidator validator1 = new RequiredFieldValidator(); 
                validator1.ID = "validator1"
                validator1.ErrorMessage = "FilterTextBox cannot be empty"
                validator1.ControlToValidate = (item["columnUniqueName"].Controls[0] as TextBox).ID; 
                item["ProductName"].Controls.Add(validator1); 
            } 
        } 
    } 


Thanks
Shinu.
0
maggi
Top achievements
Rank 1
answered on 24 Nov 2008, 11:52 AM
Hi Shinu

     Thanks for the reply but the problem is that the filter input does not have an id on aspx i tried to find the id using view source but all i could find was the name no id so the problem comes in

(item[

"Quantity"].Controls[0] as TextBox).ID

 

Is there anyother way for this?

Thanks & Regards

maggi
0
Daniel
Telerik team
answered on 26 Nov 2008, 06:08 PM
Hello Maggi,

I suppose the reason for the error is that you use RadControls for ASP.NET and not RadControls for ASP.NET AJAX.

It is possible to add the ID manually in the ItemCreated event handler in this case.
(item["Number"].Controls[0] as TextBox).ID = item.ClientID + "_FilteringTextBox"

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Michael Nagy
Top achievements
Rank 2
answered on 22 Jan 2009, 12:46 AM
hello all ,
 i have the same problem .
i want to add some validatoers at some bounded column and i tried hardly to let it work but i failed so please help me in this issue :-
first of all i have grid has 4 Bounded column
 <telerik:GridBoundColumn  DataField="EmployerName" HeaderText="Employer&nbsp;Name"  UniqueName="EmployerName"/>
 <telerik:GridBoundColumn DataField="Position" HeaderText="Position" UniqueName="Position" />
 <telerik:GridDateTimeColumn DataField="FromDT" HeaderText="From" UniqueName="FromDT" />
 <telerik:GridDateTimeColumn DataField="ToDT" HeaderText="To" UniqueName="ToDT"/>

i need to add 4 required validation on these column
i registered ItemCreated event and try to add validations like that
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            RequiredFieldValidator validator1 = new RequiredFieldValidator();
            validator1.ID = "validator1";
            validator1.ErrorMessage = " * " ;
            (item["EmployerName"].Controls[0] as TextBox).ID = item.ClientID + "_TextBox";
            validator1.ControlToValidate = (item["EmployerName"].Controls[0] as TextBox).ID;
            item["EmployerName"].Controls.Add(validator1);
        }
  }


the vaildator worked well but at insert or update event handler after called extract values mehtod i figured that the value for the EmployerName column was always equal null .

item.ExtractValues(newValues); // newValues["EmployerName"] even if i entered value in the edit form

after that i tried to add the validator at item data bound event but it is still the same and the values returned null also.

i think when i changed the id of the item[ColumnUniqueName].control[0] the extract item didn't work well .

so i need help for my problem :)


0
Daniel
Telerik team
answered on 26 Jan 2009, 05:23 PM
Michael,

Let us know where you are attaching your ItemCreated event.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Stavros
Top achievements
Rank 1
Answers by
Daniel
Telerik team
maggi
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Michael Nagy
Top achievements
Rank 2
Share this question
or