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

Radgrid perform validation only in edit or insert mode and not both.

8 Answers 437 Views
Grid
This is a migrated thread and some comments may be shown as answers.
m chotu
Top achievements
Rank 1
m chotu asked on 21 Jan 2010, 07:42 PM

Hi Telerik team;

I have a master grid and a detail grid in my aspx page.  I have edit and insert set up in the detail grid.  I have added required field validator in the code behind for the text box.  I am running into an issue with validation happening on both edit and insert mode at the same time when any text is not entered in the text box in both the modes and any of the update or insert buttons are clicked.  The grid allows both the edit and insert modes to be turned on at any time.  I have attached a screen shot in order to help explain it better.  Is there any way, that the validation could be performed only in edit or insert mode at any time or just to turn edit or insert mode only and not allow both of these to be enabled?  I have also attached my code that I have for this.

Thanks in advance.  Any help would be appreciated.

Meera

aspx page:

 

<telerik:RadGrid ID="viewGrid" runat="server" ActiveItemStyle-BorderColor="ActiveBorder" 
                                        ShowStatusBar="True" AllowSorting="true" ItemStyle-BorderWidth="1px" OnSortCommand="viewGrid_Sort" 
                                        MasterTableView-ExpandCollapseColumn-CollapseImageUrl="~/App_Themes/Images/Content/collapse.gif"   
                                        MasterTableView-ExpandCollapseColumn-ItemStyle-CssClass="textalignleft"   
                                        MasterTableView-ExpandCollapseColumn-ButtonType="ImageButton"   
                                        MasterTableView-ExpandCollapseColumn-ExpandImageUrl="~/App_Themes/Images/Content/expand.gif" 
                                        OnUpdateCommand="viewGrid_UpdateCommand" Width="900px" 
                                        MasterTableView-ExpandCollapseColumn-ItemStyle-HorizontalAlign="Center" OnItemCreated="viewGrid_ItemCreated" 
                                        MasterTableView-ExpandCollapseColumn-ItemStyle-VerticalAlign="Middle" OnItemDataBound="viewGrid_DataBound" 
                                        MasterTableView-ExpandCollapseColumn-ItemStyle-Width="2%" OnDetailTableDataBind="viewGrid_DetailTableDataBind"   
                                        OnNeedDataSource="viewGrid_NeedDataSource" OnPreRender="viewGrid_PreRender"   
                                         OnInsertCommand="viewGrid_InsertCommand" AllowMultiRowEdit="false">  
                                        <MasterTableView AutoGenerateColumns="False" HierarchyLoadMode="ServerBind" 
                                         DataKeyNames="DomainName,ParentDomainValue" Name="Master" > 
                                             <DetailTables> 
                                             <telerik:GridTableView DataKeyNames="DomainName,ParentDomainName,ParentDomainValue,DomainValue" Name="DomainLabel"   
                                                AutoGenerateColumns="false" BorderStyle="None" ItemStyle-BorderWidth="0px" AllowSorting="false"   
                                                ShowHeader="false" Width="900px" 
                                                CommandItemDisplay="Top" EditMode="InPlace">  
                                                <ParentTableRelation> 
                                                <telerik:GridRelationFields DetailKeyField="DomainName" MasterKeyField="DomainName" /> 
                                                </ParentTableRelation> 
                                                <CommandItemSettings AddNewRecordText="<%$ Resources:DomainAdmin, locAddDomainValue %>" /> 
                                                <Columns> 
                                                    <telerik:GridTemplateColumn UniqueName="DomainLabelcol" > 
                                                    <ItemTemplate> 
                                                            <asp:Label ID="plSubject" runat="server" Text="Domain Values"/>  
                                                    </ItemTemplate> 
                                                    <ItemStyle Width="150px" Wrap="false" HorizontalAlign="Left" CssClass="panelstyle"   
                                                     ForeColor="#62267f" Font-Bold="true"/>  
                                                    </telerik:GridTemplateColumn> 
                                                    <telerik:GridBoundColumn UniqueName="DomainValue" DataType="System.String" 
                                                        DataField="DomainValue" ColumnEditorID="GridTextBoxColumnEditor1" 
                                                        ItemStyle-HorizontalAlign="Left" 
                                                        ItemStyle-Wrap="false" 
                                                        ItemStyle-Width="300px">  
                                                    </telerik:GridBoundColumn> 
                                                    <telerik:GridCheckBoxColumn DataField="ActiveFlag" UniqueName="ActiveFlag"   
                                                        DataType="System.Boolean"    
                                                        ItemStyle-HorizontalAlign="Left" 
                                                        ItemStyle-Wrap="false" 
                                                        ItemStyle-Width="20px">   
                                                    </telerik:GridCheckBoxColumn> 
                                                    <telerik:GridTemplateColumn UniqueName="DomainLabelcol" > 
                                                    <ItemTemplate> 
                                                        <asp:Label ID="lblActive" Text="Active" runat="server" CssClass="ActiveLabelStyle" /> 
                                                    </ItemTemplate> 
                                                    <ItemStyle Width="130px" Wrap="false" HorizontalAlign="Left" /> 
                                                    </telerik:GridTemplateColumn> 
                                                    <telerik:GridEditCommandColumn ButtonType="ImageButton"   
                                                        EditImageUrl="../App_Themes/Images/Buttons/domain_pur_edit.gif" 
                                                        InsertImageUrl="../App_Themes/Images/Buttons/domain_gr_insert.gif" 
                                                        CancelImageUrl="../App_Themes/Images/Buttons/domain_gr_cancel.gif" 
                                                        UpdateImageUrl="../App_Themes/Images/Buttons/domain_gr_update.gif" 
                                                        UniqueName="EditDomainValueColumn" 
                                                        ItemStyle-HorizontalAlign="Right" 
                                                        ItemStyle-Wrap="false" 
                                                        ItemStyle-Width="200px">  
                                                    </telerik:GridEditCommandColumn> 
                                                    <telerik:GridCheckBoxColumn DataField="EditableFlag" UniqueName="EditFlag"   
                                                        DataType="System.Boolean" Visible="false" >   
                                                    </telerik:GridCheckBoxColumn> 
                                                      
                                                </Columns> 
                                            </telerik:GridTableView> 
                                            </DetailTables> 
                                            <Columns> 
                                                <telerik:GridBoundColumn UniqueName="DomainName" DataType="System.String" HeaderText="<%$ Resources:DomainAdmin, locDomainName %>" 
                                                    DataField="DomainName" SortExpression="DomainName"   
                                                    ItemStyle-HorizontalAlign="Left" 
                                                    HeaderStyle-Width="200px" HeaderStyle-HorizontalAlign="Left" ItemStyle-Wrap="false" 
                                                    HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="#62267f" ItemStyle-Width="200px" 
                                                    SortAscImageUrl="~/App_Themes/Images/Buttons/arrow_sort_up.png"   
                                                    SortDescImageUrl="~/App_Themes/Images/Buttons/arrow_sort_down.png" 
                                                    > 
                                                </telerik:GridBoundColumn> 
                                                <telerik:GridBoundColumn UniqueName="ParentDomainName" DataType="System.String" 
                                                    HeaderText="<%$ Resources:DomainAdmin, locParentDomainName %>" DataField="ParentDomainName"   
                                                    SortExpression="ParentDomainName" 
                                                    ItemStyle-HorizontalAlign="Left" 
                                                    HeaderStyle-Width="300px" HeaderStyle-HorizontalAlign="Left" ItemStyle-Wrap="false" 
                                                    HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="#62267f" ItemStyle-Width="300px" 
                                                    SortAscImageUrl="~/App_Themes/Images/Buttons/arrow_sort_up.png"   
                                                    SortDescImageUrl="~/App_Themes/Images/Buttons/arrow_sort_down.png">                                                      
                                                </telerik:GridBoundColumn> 
                                                <telerik:GridBoundColumn UniqueName="ParentDomainValue" DataType="System.String" HeaderText="<%$ Resources:DomainAdmin, locParentDomainValue %>" 
                                                    DataField="ParentDomainValue" SortExpression="ParentDomainValue" 
                                                    ItemStyle-HorizontalAlign="Left" 
                                                    HeaderStyle-Width="300px" HeaderStyle-HorizontalAlign="Left" ItemStyle-Wrap="false" 
                                                    HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="#62267f" ItemStyle-Width="300px" 
                                                    SortAscImageUrl="~/App_Themes/Images/Buttons/arrow_sort_up.png"   
                                                    SortDescImageUrl="~/App_Themes/Images/Buttons/arrow_sort_down.png">  
                                                </telerik:GridBoundColumn> 
                                            </Columns> 
                                            <AlternatingItemStyle Wrap="false"/>  
                                            <ItemStyle Wrap="false"/>  
                                            <RowIndicatorColumn Visible="False">  
                                                <HeaderStyle Width="20px" /> 
                                            </RowIndicatorColumn> 
                                            <ExpandCollapseColumn ButtonType="ImageButton">  
                                            </ExpandCollapseColumn> 
                                            <EditFormSettings> 
                                                <PopUpSettings ScrollBars="None" /> 
                                            </EditFormSettings> 
                                        </MasterTableView> 
                                    </telerik:RadGrid> 

 

code behind:

 

protected void viewGrid_ItemCreated(object sender, GridItemEventArgs e)  
{  
     if (e.Item.OwnerTableView.Name == "DomainLabel")  
     {  
     //Add requiredfieldvalidator for insert and edit.  
      if ((e.Item is GridEditableItem) && e.Item.IsInEditMode)  
      {  
          GridEditableItem editItem = (GridEditableItem)e.Item;  
          TextBox txtbx = (TextBox)editItem["DomainValue"].Controls[0];  
          txtbx.ID = "TextValidated";  
 
          RequiredFieldValidator reqfdvalidtr = new RequiredFieldValidator();  
          reqfdvalidtr.ID = "RequiredFieldValidator1";  
          reqfdvalidtr.ErrorMessage = "Required";  
          reqfdvalidtr.ControlToValidate = "TextValidated";  
          editItem["DomainValue"].Width = Unit.Pixel(100);  
          editItem["DomainValue"].Controls.Add(reqfdvalidtr);  
 
      } 
     }
}

8 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 27 Jan 2010, 06:39 AM
Hi m chotu,

You can use both approaches to handle the functionality, which you mentioned.
You can use the ItemCommand, to determine which command the user issued last. Then, depending on this, you can either toggle off the insert item, or clear the edit indices for the grid.
Alternatively, you can dynamically set different validation groups for the two pairs of validation controls. This can be done, for example, in the ItemDataBound event handler, or in the PreRender event handler.
I hope these suggestions get you started properly.

Greetings,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Andrey
Top achievements
Rank 1
answered on 20 Sep 2010, 02:52 PM
I have the same problem.
I have next grid
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="OnNeedDAtaSource" AutoGenerateColumns="false" OnItemDataBound="OnItemDataBound">
            <MasterTableView  CommandItemDisplay="Top">
                <Columns>
                    <telerik:GridTemplateColumn HeaderText="name">
                        <ItemTemplate>
                            <asp:Label ID="lValue" Text='<%# Eval("Name") %>' runat="server"></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="tbValue" Text='<%# Bind("Name") %>' runat="server"></asp:TextBox>
                            <span>*</span>
                            <asp:RequiredFieldValidator ID="rvValue" ControlToValidate="tbValue" ErrorMessage="Cannot be empty"
                                runat="server"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridEditCommandColumn UniqueName="Edit" ButtonType="ImageButton" CancelText="cancel"
                                            EditText="edit" InsertText="insert">
                                        </telerik:GridEditCommandColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>

and next code in code behind
public partial class _Default : Page
    {        
          protected void OnNeedDAtaSource(object source, GridNeedDataSourceEventArgs e)
       {
           RadGrid1.DataSource = new List<Test> {new Test("qwe"), new Test("fth")};
       }
 
       protected void OnItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridEditableItem && e.Item.IsInEditMode)
           {
               var rvValue = (RequiredFieldValidator) e.Item.FindControl("rvValue");
               if (e.Item is GridEditFormInsertItem)
               {
                   rvValue.ValidationGroup = "addRowValidation";
               }
               else
               {
                   rvValue.ValidationGroup = "editRowValidation";
               }
           }
       }
   }
   public class Test
   {
       public  Test(string name)
       {
           Name = name;
       }
       public string Name { get; set; }
   }

But validation does not work properly. Can anyone help me?
0
Mira
Telerik team
answered on 23 Sep 2010, 12:01 PM
Hello Andrey,

Could you provide more information on validation groups used?
If you want them to work properly, you should set the ValidationGroup property of the update/insert button to the corresponding value. For additional information, you can examine this article.

However, I tried your code without the validation groups and it is working fine. You can find the test project attached to this message.

I hope this helps.

All the best,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Andrey
Top achievements
Rank 1
answered on 23 Sep 2010, 07:49 PM
Hello Mira,

 Without validation groups it doesn't work fine in one case. 
Steps to reproduce:
1. Press "Add new record" 
2. Press Edit button
3. Now you have two opened forms
4. Press "Update" in Edit form(not in "Insert form")

Result: You can't perform updating because of validation in Insertion form.

Therefore I'm trying to use Validation groups. And if you can tell me how I can set the ValidationGroup property of the update/insert button I would appreciate it.
0
Mira
Telerik team
answered on 27 Sep 2010, 02:44 PM
Hello Andrey,

I have followed your scenario and prepared a sample project for you demonstrating how the desired functionality can be implemented. You can find it attached to this message.

I hope it helps.

Regards,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Andrey
Top achievements
Rank 1
answered on 27 Sep 2010, 04:21 PM
Thank you Mira. That is what I wanted to do. That works fine.
0
Kumar
Top achievements
Rank 1
answered on 06 Mar 2013, 11:38 AM
Hi,
I have downloaded the attached zip file "radgridvalidationgroupsinsertupdate.zip" and midified it according to my requirement. But it is not working while I am trying to insert a value.

Code snippet is as follows:
'Format Code Block'
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="OnNeedDAtaSource"
            AutoGenerateColumns="false" OnItemCreated="OnItemCreated" OnItemDataBound="OnItemDataBound">
            <MasterTableView CommandItemDisplay="Top">
                <Columns>
                    <telerik:GridTemplateColumn HeaderText="name">
                        <ItemTemplate>
                            <asp:Label ID="lValue" Text='<%# Eval("Name") %>' runat="server"></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="tbValue" Text='<%# Bind("Name") %>' runat="server"></asp:TextBox>
                            <span>*</span>
                            <asp:RequiredFieldValidator ID="rvValue" ControlToValidate="tbValue" ErrorMessage="Cannot be empty"
                                runat="server"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>

                    <telerik:GridTemplateColumn HeaderText="Dob">
                        <ItemTemplate>
                            <asp:Label ID="lblDob" Text='<%# Eval("Dob") %>' runat="server"></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                         <telerik:RadDatePicker ID="PayrollDatePicker" runat="server" SelectedDate ='<%# Bind("Dob")%>'>
                                    </telerik:RadDatePicker>
                            <span>*</span>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="PayrollDatePicker" ErrorMessage="Cannot be empty"
                                runat="server"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>

                    <telerik:GridEditCommandColumn UniqueName="Edit" ButtonType="ImageButton" CancelText="cancel"
                        EditText="edit" InsertText="insert">
                    </telerik:GridEditCommandColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
'Format Code Block'

Code behind:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class _Default : System.Web.UI.Page
{
    protected void OnNeedDAtaSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = new List<Test> { new Test("qwe", new DateTime(2012, 7, 1)), new Test("fth", new DateTime(2012, 8, 8)) };
    }

    protected void OnItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            var rvValue = (RequiredFieldValidator)e.Item.FindControl("rvValue");
            if (e.Item is GridEditFormInsertItem)
            {
                rvValue.ValidationGroup = "addRowValidation";
                ((e.Item as GridEditFormItem).FindControl("PerformInsertButton") as LinkButton).ValidationGroup = "addRowValidation";
            }
            else
            {
                rvValue.ValidationGroup = "editRowValidation";
                ((e.Item as GridEditFormItem).FindControl("UpdateButton") as LinkButton).ValidationGroup = "editRowValidation";
            }
        }
    }
}
public class Test
{
    public Test(string name, DateTime dob)
    {
        Name = name;
        Dob = dob;
    }
    public string Name { get; set; }
    public DateTime Dob { get; set; }
}
0
Kostadin
Telerik team
answered on 11 Mar 2013, 09:29 AM
Hi Kumar,

Note that you have to set a ValidationGroup to all your validators. I modified your sample and attached it to this forum post.

Kind regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
m chotu
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Andrey
Top achievements
Rank 1
Mira
Telerik team
Kumar
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or