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

Validation fires in both insert and edit row

18 Answers 197 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 08 Mar 2012, 03:48 PM
Hi.
I have followed this example to use asp.net validation controls inside GridTemplateColumns:
http://www.telerik.com/help/aspnet-ajax/grid-validation.html

I am using the following settings for the MasterTableView:
<MasterTableView EditMode="EditForms" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage"> 

But when both insert and editable rows are open, the validation executes for all of them. Am I the only one with this problem, or is this a known problem?

Please let me know if my explanation is insufficient.

18 Answers, 1 is accepted

Sort by
0
Alexander
Top achievements
Rank 1
answered on 12 Mar 2012, 12:01 PM
Nobody got any idea how to solve this?
0
Andrey
Telerik team
answered on 13 Mar 2012, 12:06 PM
Hello,

The behavior you are observing is caused by that both the insert and edit operations uses the same edit form. In this case there are two approaches:
  • You could use different edit and insert forms as described in this help topic.
  • Another approach is to apply different validation when the item is in insert or edit mode. You could use the ItemDataBound event and to check whether the item is in EditMode or whether the TableView has begun an insert operation:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        //do some custom logic here...
    }
    else if (RadGrid1.MasterTableView.IsItemInserted)
    {
        //do some custom logic here...
    }
}

It is up to you to decide which approach you will use.


All the best,
Andrey
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.
0
Alexander
Top achievements
Rank 1
answered on 13 Mar 2012, 03:51 PM
Thanks for the response, but if that would be the case - shouldn't it be avoided by using GridTemplateComlumn with separate EditItemTemplate and InsertItemTemplate?

<telerik:GridTemplateColumn HeaderText="Kommunekode" SortExpression="MunicipalityCode" UniqueName="MunicipalityCode">
    <ItemTemplate>
        <asp:Label runat="server" ID="lblMunicipalityCode" Text='<%# Eval("MunicipalityCode") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox runat="server" ID="tbxMunicipalityCode" MaxLength="4" Width="80" Text='<%# Bind("MunicipalityCode") %>'></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Kommunekode skal udfyldes." ForeColor="Red" ControlToValidate="tbxMunicipalityCode"></asp:RequiredFieldValidator>
        <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" TargetControlID="RequiredFieldValidator1" runat="server"></asp:ValidatorCalloutExtender>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Kommunekode kan kun bestÃ¥ af tal." ForeColor="Red" ControlToValidate="tbxMunicipalityCode" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
        <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" TargetControlID="CompareValidator1" runat="server"></asp:ValidatorCalloutExtender>
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:TextBox runat="server" ID="tbxMunicipalityCode" MaxLength="4" Width="80"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Kommunekode skal udfyldes." ForeColor="Red" ControlToValidate="tbxMunicipalityCode"></asp:RequiredFieldValidator>
        <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" TargetControlID="RequiredFieldValidator1" runat="server"></asp:ValidatorCalloutExtender>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Kommunekode kan kun bestÃ¥ af tal." ForeColor="Red" ControlToValidate="tbxMunicipalityCode" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
        <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" TargetControlID="CompareValidator1" runat="server"></asp:ValidatorCalloutExtender>
    </InsertItemTemplate>
</telerik:GridTemplateColumn>

0
Andrey
Telerik team
answered on 14 Mar 2012, 03:35 PM
Hello,

Yes, you could also use this approach. I suggested the other approach because it is applicable in more scenarios. You could always use the scenario that is most convenient to you.

Greetings,
Andrey
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.
0
Alexander
Top achievements
Rank 1
answered on 15 Mar 2012, 01:47 PM
I have already implemented the GridTemplateColumn with Edit-/InsertItemTemplate (and this is not working)

Now, I have tried to apply the validation in the ItemDataBound event, but when creating a insert item row, the following check validates true:

if (e.Item is GridEditableItem && e.Item.IsInEditMode)
0
Andrey
Telerik team
answered on 16 Mar 2012, 04:10 PM
Hi,

When you are checking whether an item is inserted you should use the following code:

if(e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted)
{
 
}

What is the problem with the GridTemplateColumn?

Greetings,
Andrey
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.
0
Alexander
Top achievements
Rank 1
answered on 19 Mar 2012, 09:50 AM
I am using the following code to insert validation controls dynamically. I have posted (most of) the rest of the ItemDataBound event too:

if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
    GridEditableItem editRowItem = e.Item as GridEditableItem;
  
    foreach (var value in Enum.GetNames(typeof(LetterTemplateEnablingEnum)))
    {
        try
        {
            GridDropDownListColumnEditor dropDownList = editRowItem.EditManager.GetColumnEditor(value) as GridDropDownListColumnEditor;
  
            if (dropDownList != null)
            {
                dropDownList.DataSource = GetLetterTemplateEnabling();
                dropDownList.ComboBoxControl.Width = Unit.Pixel(120);
                dropDownList.DataBind();
            }
  
            if (e.Item.DataItem is LetterTemplate && dropDownList != null)
            {
                LetterTemplate dataItem = (LetterTemplate)e.Item.DataItem;
                LetterTemplateEnabling letterTemplateEnabling = GetLetterTemplateEnabling(dataItem, (LetterTemplateEnablingEnum)Enum.Parse(typeof(LetterTemplateEnablingEnum), value));
  
                dropDownList.SelectedValue = letterTemplateEnabling.Id;
            }
        }
        catch { }
    }
}
 
if (e.Item is GridEditFormInsertItem && rgManage.MasterTableView.IsItemInserted)
{
    GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
    Control tbxMunicipalityCode = insertItem.FindControl("tbxMunicipalityCode");
    ControlCollection collection = tbxMunicipalityCode.Parent.Controls;
  
    RequiredFieldValidator requiredFieldValidator = new RequiredFieldValidator
                                                        {
                                                            ID = "reqVal_" + tbxMunicipalityCode.ID,
                                                            ErrorMessage = "Kommunekode skal udfyldes",
                                                            ForeColor = Color.Red,
                                                            ControlToValidate = tbxMunicipalityCode.ID,
                                                        };
  
    collection.Add(requiredFieldValidator);
}


But apparently, that does NOT make it unique. Still, while this is the only validation on the page, it get's triggered when I have opened both an insert and a edit row. If I select update in the edit row, the validation controls in the insert-row validates false.

Here is one of the GridTemplateColumn:
<telerik:GridTemplateColumn HeaderText="Kommunekode" SortExpression="MunicipalityCode" UniqueName="MunicipalityCode">
    <ItemTemplate>
        <asp:Label runat="server" ID="lblMunicipalityCode" Text='<%# Eval("MunicipalityCode") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox runat="server" ID="tbxMunicipalityCodeEdit" MaxLength="4" Width="80" Text='<%# Bind("MunicipalityCode") %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:TextBox runat="server" ID="tbxMunicipalityCodeInsert" MaxLength="4" Width="80"></asp:TextBox>
    </InsertItemTemplate>
</telerik:GridTemplateColumn>
0
Princy
Top achievements
Rank 2
answered on 20 Mar 2012, 07:34 AM
Hi Alexander,

I tried the same using EditItemTemplate and InsertItemTemplate and I could get it working. I have used the following condition to differentiate edit mode and insert mode.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditableItem editRowItem = e.Item as GridEditableItem;
    }
    if (e.Item is GridEditFormInsertItem && RadGrid1.MasterTableView.IsItemInserted)
    {
        GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
    }
 
}

Hope this helps.

Thanks,
-Princy.
0
Alexander
Top achievements
Rank 1
answered on 21 Mar 2012, 09:20 AM
Nope, the validation in the insert row still fires when update button in edit row is pressed.
0
Andrey
Telerik team
answered on 22 Mar 2012, 10:42 AM
Hello,

I have created a sample project to test the code. Give the project a try and check what are the differences with your project.

Kind regards,
Andrey
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.
0
Alexander
Top achievements
Rank 1
answered on 26 Mar 2012, 10:02 AM
Now I have tried to use your sample project - I've added the TemplateColumn with the following controls:
<telerik:GridTemplateColumn>
    <ItemTemplate>
        <asp:Label ID="lblShipName" runat="server" Text='<%# Eval("ShipName") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox runat="server" ID="tbxShipNameEdit" Text='<%# Bind("ShipName") %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:TextBox runat="server" ID="tbxShipNameInsert"></asp:TextBox>
    </InsertItemTemplate>
</telerik:GridTemplateColumn>

Then, I am adding validation controls in the ItemDataBound event of RadGrid1 (I've changed GridEditableItem to GridEditFormInsertItem - because it otherwise wouldn't find tbxShipNameInsert TextBox):
if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
{
    GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
 
    Control tbxShipNameInsert = insertItem.FindControl("tbxShipNameInsert");
    ControlCollection collection = tbxShipNameInsert.Parent.Controls;
 
    RequiredFieldValidator requiredFieldValidator = new RequiredFieldValidator
                                                        {
                                                            ID = "reqVal_" + tbxShipNameInsert.ID,
                                                            ErrorMessage = "Kommunekode skal udfyldes",
                                                            ForeColor = Color.Red,
                                                            ControlToValidate = tbxShipNameInsert.ID
                                                        };
    collection.Add(requiredFieldValidator);
}

But still, when I am running the test:
- Open a row in edit-mode, open a new record row (the order doesn't matter).
- Then, I press the update button for the edit-mode row
- Although the edit row shouldn't have validation, the validation control in the insert row is marked with error.

Is seems like the update button AND the insert button are gathered in the same validation group. Because of this, I have tried to change this, but I don't seem to be able to fetch the insert button (the insertButton is always null):
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
    if (e.Item.OwnerTableView.IsItemInserted)
    {
        LinkButton insertButton = (LinkButton)e.Item.FindControl("PerformInsertButton");
        insertButton.CausesValidation = true;
        insertButton.ValidationGroup = "Insert";
    }
    else
    {
        LinkButton updateButton = (LinkButton)e.Item.FindControl("UpdateButton");
        updateButton.CausesValidation = true;
        updateButton.ValidationGroup = "Update";
    }
}
0
Andrey
Telerik team
answered on 29 Mar 2012, 09:24 AM
Hi,

Now I think I better understand the problem. What is happening here is that ItemDataBound event is fired for every item in RadGrid. Since the EditItem is still open the event will be fired for this item too and then for the other items as well.

The approach you are trying to use could prove helpful. However you could not find the PerformInsert and Update button in the same item. The PerformInsert button belongs to the CommandItem. So, in order to find it you need to change your code as follows:

if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{  
    LinkButton updateButton = (LinkButton)e.Item.FindControl("UpdateButton");
    updateButton.CausesValidation = true;
    updateButton.ValidationGroup = "Update";  
}
else if(e.Item is GridCommandItem)
{
    LinkButton insertButton = (LinkButton)e.Item.FindControl("PerformInsertButton");
    insertButton.CausesValidation = true;
    insertButton.ValidationGroup = "Insert";
}

Give this approach a try and check whether you get the desired effect.

All the best,
Andrey
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.
0
Alexander
Top achievements
Rank 1
answered on 29 Mar 2012, 10:29 AM
The insertButton object is still null. I tried to separate the following code-block, but neither in ItemDataBound or ItemCreated, it worked:
if (e.Item is GridCommandItem)
{
    LinkButton insertButton = (LinkButton)e.Item.FindControl("PerformInsertButton");
    if (insertButton != null)
    {
        insertButton.CausesValidation = true;
        insertButton.ValidationGroup = "Insert";
    }
}
0
Andrey
Telerik team
answered on 29 Mar 2012, 04:17 PM
Hello,

I modified the project to show you the approach you need to use. Give the project a try and check whether it is working for you.

Regards,
Andrey
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.
0
Alexander
Top achievements
Rank 1
answered on 30 Mar 2012, 10:50 AM
I can't really see what approach you are talking about.....?!
0
Andrey
Telerik team
answered on 30 Mar 2012, 02:40 PM
Hi,

This is the approach I am referring to:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormInsertItem)
        {
            LinkButton insertButton = (LinkButton)e.Item.FindControl("PerformInsertButton");
            if (insertButton != null)
            {
                insertButton.CausesValidation = true;
                insertButton.ValidationGroup = "Insert";
            }
        }
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            LinkButton updateButton = (LinkButton)e.Item.FindControl("UpdateButton");
            if (updateButton != null)
            {
                updateButton.CausesValidation = true;
                updateButton.ValidationGroup = "Update";
            }
        }
    }

However, you need to verify that the PerformInsert button is LinkButton. If it is an ImageButton you need to cast the control to ImageButton.

Regards,
Andrey
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.
0
Alexander
Top achievements
Rank 1
answered on 30 Mar 2012, 03:00 PM
But your example is exactly the same as the one I posted earlier. I can't see any "new approach" about it...
0
Andrey
Telerik team
answered on 02 Apr 2012, 04:18 PM
Hello,

The "difference" between the both approaches is the GridItem that is used:

  • "Your" approach:
    if (e.Item is GridCommandItem)
    {
        LinkButton insertButton = (LinkButton)e.Item.FindControl("PerformInsertButton");
        if (insertButton != null)
        {
            insertButton.CausesValidation = true;
            insertButton.ValidationGroup = "Insert";
        }
    }
  • The approach I have sent you:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem)
            {
                LinkButton insertButton = (LinkButton)e.Item.FindControl("PerformInsertButton");
                if (insertButton != null)
                {
                    insertButton.CausesValidation = true;
                    insertButton.ValidationGroup = "Insert";
                }
            }
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                LinkButton updateButton = (LinkButton)e.Item.FindControl("UpdateButton");
                if (updateButton != null)
                {
                    updateButton.CausesValidation = true;
                    updateButton.ValidationGroup = "Update";
                }
            }
        }

Greetings,
Andrey
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
Alexander
Top achievements
Rank 1
Answers by
Alexander
Top achievements
Rank 1
Andrey
Telerik team
Princy
Top achievements
Rank 2
Share this question
or