How can I validate the controls?

8 posts, 0 answers
  1. Stavros
    Stavros avatar
    89 posts
    Member since:
    Sep 2008

    Posted 04 Nov 2008 Link to this post

    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.
  2. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 04 Nov 2008 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. maggi
    maggi avatar
    58 posts
    Member since:
    Mar 2007

    Posted 24 Nov 2008 Link to this post

    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
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 24 Nov 2008 Link to this post

    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.
  6. maggi
    maggi avatar
    58 posts
    Member since:
    Mar 2007

    Posted 24 Nov 2008 Link to this post

    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
  7. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 26 Nov 2008 Link to this post

    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.
  8. Michael Nagy
    Michael Nagy avatar
    18 posts
    Member since:
    May 2008

    Posted 21 Jan 2009 Link to this post

    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 :)


  9. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 26 Jan 2009 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017