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

Use required field validator for new records

5 Answers 657 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ICT
Top achievements
Rank 1
ICT asked on 21 Mar 2017, 08:41 AM

Hi

I want to validate new records before they gets added to the grid at client-side. After I clear out the "name" field tag I get the red *. Which is perfectly fine and just what I want to do. However, when I click on "Add new record" button more than once, new empty record gets added without being validated.

 

What is the most clean and neat way to achieve this? Below is my code using a classic ASP coding style, so you get everything at one place.

 

01.<%@ Page Language="C#" %>
02. 
03.<!DOCTYPE html>
04. 
05.<html>
06.<head runat="server">
07.  <title>[Title]</title>
08.  <script runat="server">
09.    protected override void OnInit(EventArgs e)
10.    {
11.      this.Load += HandleLoad;
12.      this.PreLoad += HandlePreLoad;
13. 
14.      base.OnInit(e);
15.    }
16.    private void HandleLoad(object sender, EventArgs e)
17.    {
18.      this.grid.DataSource = new[] { new { Id = 1, Name = "Name" } };
19.    }
20.    private void HandlePreLoad(object sender, EventArgs e)
21.    {
22.      this.grid.MasterTableView.AutoGenerateColumns = false;
23.      this.grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
24.      this.grid.MasterTableView.EditMode = GridEditMode.Batch;
25.    }
26.  </script>
27.</head>
28.<body>
29.  <form id="form" runat="server">
30.    <asp:ScriptManager runat="server" />
31.    <div style="width: 25%">
32.      <telerik:RadGrid ID="grid" runat="server">
33.        <MasterTableView>
34.          <Columns>
35.            <telerik:GridTemplateColumn HeaderText="Name">
36.              <EditItemTemplate>
37.                <asp:TextBox ID="name" runat="server" />
38.                <asp:RequiredFieldValidator runat="server" ControlToValidate="name" Text="*" />
39.              </EditItemTemplate>
40.              <ItemTemplate>
41.                <%# Eval("Name") %>
42.              </ItemTemplate>
43.            </telerik:GridTemplateColumn>
44.          </Columns>
45.        </MasterTableView>
46.      </telerik:RadGrid>
47.    </div>
48.  </form>
49.</body>
50.</html>

 

Regrads,

Garmco ICT

5 Answers, 1 is accepted

Sort by
0
ICT
Top achievements
Rank 1
answered on 23 Mar 2017, 01:08 PM
Is it very difficult?!
0
Eyup
Telerik team
answered on 24 Mar 2017, 07:43 AM
Hi,

First of all, please make sure that you are setting the DataSource property of the grid during NeedDataSource. Performing complex grid operations such as Inserting, Deleting, Updating, Hierarchy relations, Grouping, Exporting, Paging, Sorting, Filtering, etc. require accommodating appropriate database operations.  Therefore, we suggest you to avoid Simple Databinding and strongly recommend the use of more advanced databinding methods, which automatically handle the aforementioned functions:

Declarative DataSource (DataSourceID property)
Programmatic Data Binding (NeedDataSource event, + DetailTableDataBind for hierarchy). You should set the DataSource property ONLY within these event handlers.


As for limiting adding new records with Batch editing, you can examine the implementation demonstrated in the attached web site sample.

I hope this will prove helpful.

Regards,
Eyup
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
ICT
Top achievements
Rank 1
answered on 26 Mar 2017, 06:11 AM
I can see that your solution limits the number of rows a grid can have. However, I'm looking for a solution to validate the content of a new row before it gets added to the grid. Take for example a mandatory field which cannot be empty as the "name" field in my example above.
0
ICT
Top achievements
Rank 1
answered on 30 Mar 2017, 05:17 AM
Still waiting for the team to answer!
0
Eyup
Telerik team
answered on 30 Mar 2017, 06:16 AM
Hello,

Generally, RadGrid provides ColumnValidationSettings, which can be used to automatically add validation for editing functionality:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/validation#adding-a-validator-to-an-edit-field-in-telerik-radgrid

However, when you want to add the Required, Custom, RegularExpression or any kind of Validator programmatically depending on some condition, you can use the code-behind logic provided in the following section:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/validation#adding-a-validator-explicitly

For getting the data values you can use:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-raw-field-data-and-key-values

In addition, I am also sending a web site sample with a similar implementation.

Also, you can prevent a cell from editing using this event handler:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/client-side-programming/events/onbatcheditopening

Another option is to use the OnUserAction event and prevent the new row from opening or being added:
http://www.telerik.com/forums/next-page-event#fXUmvG81aU2Oz4o-I9t6SQ

Regards,
Eyup
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
ICT
Top achievements
Rank 1
Answers by
ICT
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or