Adding Validators to Columns in a Hierarchical Grid

4 posts, 0 answers
  1. Shane Milton
    Shane Milton avatar
    219 posts
    Member since:
    Dec 2003

    Posted 12 Feb 2009 Link to this post

    I have a hierarchical grid where AutoGenerateColumns is FALSE (i.e. I specify all columns) but most columns I would like to use the specialized Telerik columns (GridBoundColumn, GridDateTimeColumn, GridNumericColumn, etc.), not GridTemplateColumn, but I would like to add validators for these fields when in Insert/Edit modes (I use popup style inserts/edits).

    The best thing I have found so far is based on this post. This gets rather interesting, and hacky, because the ItemCreated event handler is for the entire grid and not for the specific MasterTableViews within the hierarchical grid. Is there a better way to do this? I would like to add things such as date ranges, numerical ranges, and a required validator, among other things (pretty simple validations) for this specific problem, but this really should be much more flexible than that, even.

    My primary observation is that there is no simple way to just hook up a validator to an edit field, and this is something that should be a relatively trivial thing without requiring templates. I think it would make sense to add a new collection to a Grid, GridValidators, where I could add as many validators as I wanted. Then give each grid column a property, ValidatorToUse and link it up to one of those validators in that collection. Or perhaps allow the use to have several validators assigned to a single column somehow. Something...

    And if I'm wrong and this exists, my apologies. I've missed some very obvious things in the past. ;-)

    Thanks!
    -Shane
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Feb 2009 Link to this post

    Hello Shane,

    You can check for the name property of the OwnerTableView and then add the RequiredFieldValidator as shown below:
    aspx:
     <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="false" OnItemCreated="RadGrid1_ItemCreated" > 
              <MasterTableView EditMode="PopUp" Name="Master" DataSourceID="SqlDataSource1"
                 <DetailTables  > 
                 <telerik:GridTableView DataSourceID="SqlDataSource2" EditMode="PopUp" Name="Detail1" > 
                 <Columns> 
                  <telerik:GridBoundColumn DataField="ProductName" UniqueName="ProductName"
                  </telerik:GridBoundColumn> 
                 ..... 

    cs:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridEditableItem && e.Item.IsInEditMode && e.Item.OwnerTableView.Name == "Detail1"
            { 
                GridEditableItem editItem = (GridEditableItem)e.Item; 
                TextBox txtbx = (TextBox)editItem["ProductName"].Controls[0]; 
                txtbx.ID = "TextBox"
                RequiredFieldValidator validator = new RequiredFieldValidator(); 
                validator.ErrorMessage = "RequiredField"
                validator.ControlToValidate = "TextBox"
                editItem["ProductName"].Controls.Add(validator); 
            } 
         } 

    Thanks
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shane Milton
    Shane Milton avatar
    219 posts
    Member since:
    Dec 2003

    Posted 13 Feb 2009 Link to this post

    Thanks, Princy...

    However, I've not found a way to implement such a technique to be a 100% solution. I specifically have problems with GridDateTimeColumn fields. Performing this technique results in a javascript error and I've made attempts to attach the validator to other controls within the resulting RadDatePicker control without success. I've reproduced this in a very simple application. Please see the attached code to reproduce the javascript error when you click "Edit".

    Do you know how I could add a validator to such a control?

    Thanks!! :-)

    ASPX:
    1     <asp:ScriptManager ID="ScriptManager1" runat="server"
    2     </asp:ScriptManager> 
    3     <div> 
    4         <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="ObjectDataSource1" OnItemCreated="grid_ItemCreated" 
    5             AutoGenerateEditColumn="true" AutoGenerateColumns="false"
    6             <MasterTableView Name="ThisGrid" CommandItemDisplay="TopAndBottom" EditMode="PopUp"
    7                 <Columns> 
    8                     <telerik:GridDateTimeColumn DataField="DateCreated" DataType="System.DateTime" HeaderText="Date Created" 
    9                         SortExpression="DateCreated" UniqueName="DateCreated"
    10                     </telerik:GridDateTimeColumn> 
    11                 </Columns> 
    12             </MasterTableView> 
    13         </telerik:RadGrid> 
    14     </div> 
    15     <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="InsertFoo" 
    16         SelectMethod="GetFoo" TypeName="GridDateRequiredValidatorPoC.Foo" UpdateMethod="InsertFoo"
    17         <UpdateParameters> 
    18             <asp:Parameter Name="dateCreated" Type="DateTime" /> 
    19         </UpdateParameters> 
    20         <InsertParameters> 
    21             <asp:Parameter Name="dateCreated" Type="DateTime" /> 
    22         </InsertParameters> 
    23     </asp:ObjectDataSource> 
    24  

    Code-Behind:
    1         protected void grid_ItemCreated( object sender, Telerik.Web.UI.GridItemEventArgs e ) 
    2         { 
    3             GridEditableItem item = e.Item as GridEditableItem; 
    4  
    5             // Only do this for items we're inserting/editing. 
    6             if ( item is GridEditableItem && item.IsInEditMode ) 
    7             { 
    8                 switch ( item.OwnerTableView.Name.ToLower() ) 
    9                 { 
    10                     case "thisgrid"
    11                         // EffectiveDate Column (Required) 
    12                         AddValidatorToEditField( item[ "DateCreated" ].Controls[ 0 ], "DateCreated" ); 
    13                         break
    14                     default
    15                         break
    16                 } 
    17             } 
    18         } 
    19  
    20         private void AddValidatorToEditField( Control ctrlToValidate, string dataField ) 
    21         { 
    22             RequiredFieldValidator requiredValidator = new RequiredFieldValidator(); 
    23  
    24             if ( ( ctrlToValidate is RadDatePicker ) ) 
    25             { 
    26                 // RadDatePickers already have an ID set so we can't set it. However, this still doesn't work. :-/ 
    27                 requiredValidator.ControlToValidate = ( (RadDatePicker)ctrlToValidate ).ID; 
    28             } 
    29             else 
    30             { 
    31                 // For some reason these controls were originally being rendered with no ID so ctrlToValidate.ID was an empty string. This fixed that. 
    32                 ctrlToValidate.ID = dataField; 
    33                 requiredValidator.ControlToValidate = ctrlToValidate.ID; 
    34             } 
    35  
    36             requiredValidator.ID = dataField + ".RequiredValidator"
    37             requiredValidator.ErrorMessage = " * Required"
    38             requiredValidator.EnableClientScript = true
    39             ctrlToValidate.Parent.Controls.Add( requiredValidator ); 
    40         } 

    Data Object for the ObjectDataSource:
    1     // Goes nowhere, does nothing 
    2     public class Foo 
    3     { 
    4         public DateTime DateCreated { getset; } 
    5  
    6         [DataObjectMethod( DataObjectMethodType.Select )] 
    7         public List<Foo> GetFoo() 
    8         { 
    9             List<Foo> foos = new List<Foo>(); 
    10             Foo foo; 
    11  
    12             for ( int i = 0; i < 8; i++ ) 
    13             { 
    14                 foo = new Foo(); 
    15                 foo.DateCreated = DateTime.Now.Subtract( new TimeSpan( ( new Random() ).Next( 50000 ) ) ); 
    16                 foos.Add( foo ); 
    17             } 
    18  
    19             return foos; 
    20         } 
    21  
    22         [DataObjectMethod( DataObjectMethodType.Insert )] 
    23         public void InsertFoo( DateTime dateCreated ) 
    24         { 
    25             // Code to make sure compiler doesn't do dumb things 
    26             if ( dateCreated < DateCreated ) 
    27             { 
    28                 throw new Exception(); 
    29             } 
    30         } 
    31  
    32         [DataObjectMethod( DataObjectMethodType.Update )] 
    33         public void UpdateFoo( DateTime dateCreated ) 
    34         { 
    35             // Code to make sure compiler doesn't do dumb things 
    36             if ( dateCreated < DateCreated ) 
    37             { 
    38                 throw new Exception(); 
    39             } 
    40         } 
    41     } 
    42  

  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Feb 2009 Link to this post

    Hello Shane,

    The javascript error you tried occurs as you have set the EnableClientScript for the RequiredFieldValidator to true. Try setting it to false to avoid the error. I tried your code, but couldnt get it to work as expected. You can try the following code instead:
    cs:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
            { 
                GridEditableItem item = (GridEditableItem)e.Item; 
                switch (item.OwnerTableView.Name) 
                { 
                    case "Detail2": 
                        // EffectiveDate Column (Required)  
                        AddValidatorToEditField(item, "date"); 
                        break; 
                    default: 
                        break; 
                } 
            }  
       } 
     
    private void AddValidatorToEditField(GridEditableItem editItem, string dataField) 
        { 
            RadDatePicker rddtpickr = (RadDatePicker)editItem["DateCreated"].Controls[0]; 
            rddtpickr.ID = dataField
            RequiredFieldValidator validator = new RequiredFieldValidator(); 
            validator.ErrorMessage = "* Required"
            validator.ControlToValidate = rddtpickr.ID
            editItem["DateCreated"].Controls.Add(validator); 
        } 

    Thanks
    Princy.
Back to Top