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

RadGrid insert item validation

3 Answers 129 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gavin
Top achievements
Rank 1
Gavin asked on 09 Oct 2013, 05:30 AM
I have a problem when validating the data on records that are to be inserted into the grid.
The grid is just using the default insert item button on the grid with some template columns, some bound columns and some dateTimeColumns.

I am using the itemdatabound method to populate some dropdown controls in these template controls when the item is in edit mode.

When Insert command is called my code does some validation against the database and if this fails then it displays the failure message on the GridEditForm and sets the Canceled property of the GridCommandEventArgs to true.

This works fine however if I then click the button to Insert the record again and it fails again all of the controls are cleared losing all of their values.

Any help in working out why this is happening would be much appreciated.

Thanks
Gavin

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 11 Oct 2013, 01:30 PM
Hi Gavin,

I have prepared a sample page with the described scenario that works as expected on my side. Please take a look at the attached page and see how it differs from your project.

If you continue to face that issue, please provide additional information about your exact scenario or open a regular support ticket and prepare a sample, runnable project that isolates the described behavior, so we could inspect it locally.


Regards,
Konstantin Dikov
Telerik
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 the blog feed now.
0
Gavin
Top achievements
Rank 1
answered on 16 Oct 2013, 09:48 PM
Hi Konstantin,

I have identified where the issue was coming from.

Our code is adding a new literal control to the editform to display the reason for the validation failure in the InsertCommand.

After this is done then the next postback will lose all values from the dropdown.

I adjusted your example to use the NeedDataSource event to supply the data for the grid then added the line to add the error message and it duplicates the issue I am having.

Here is the adjusted code form your sample that duplicates the problem.

Thanks
Gavin
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "PerformInsert")
        {
            GridEditFormInsertItem editForm = (GridEditFormInsertItem)e.Item;
 
            Label label = editForm["TemplateColumn"].FindControl("Label1") as Label;
            if (int.Parse(label.Text) < 3)
            {
            label.Text = (int.Parse(label.Text) + 1).ToString();
            e.Canceled = true;
            editForm.EditFormCell.Controls.AddAt(0, new LiteralControl("<span class='FormMessage'>Stuff</span>"));
            }
        }
    }
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            RadComboBox drpList = item["TemplateColumn"].FindControl("DropDownList1") as RadComboBox;
            for (int i = 0; i < 5; i++)
            {
                RadComboBoxItem newListItem = new RadComboBoxItem();
                newListItem.Text = "test" + i;
                newListItem.Value = "test" + i;
                drpList.Items.Add(newListItem);
            }
        }
    }
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("MyTable");
 
        dt.Columns.Add("Column1");
        dt.Columns.Add("Column2");
 
        for (int i = 0; i < 10; i++)
        {
            DataRow r = dt.NewRow();
            dt.Rows.Add(r);
 
 
        }
 
        ds.Tables.Add(dt);
 
        RadGrid1.DataSource = ds;
    }
0
Konstantin Dikov
Telerik team
answered on 21 Oct 2013, 01:01 PM
Hi Gavin,

I am glad to see that you were able to locate the root of the problem in your project.

From the provided code I was able to replicate the issue on my end, and indeed, this is caused due to the fact that you are adding control at the begging of the control collection and this is not supported scenario. You could test this by just replacing the "AddAt()" with "Add()" and you will see that everything will work correctly.

If you wish to use Literal withing the InsertItemTemplate, you could use the following approach:
<InsertItemTemplate>
    <asp:Label Text="1" ID="Label1" runat="server" />
    <br />
    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    <telerik:RadComboBox ID="RadComboBox1" runat="server" ></telerik:RadComboBox>
</InsertItemTemplate>
And in the code-behind:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "PerformInsert")
    {
        GridEditFormInsertItem editForm = (GridEditFormInsertItem)e.Item;
 
        Label label = editForm["TemplateColumn"].FindControl("Label1") as Label;
        if (int.Parse(label.Text) < 3)
        {
            label.Text = (int.Parse(label.Text) + 1).ToString();
            Literal literal = editForm["TemplateColumn"].FindControl("Literal1") as Literal;
            literal.Text = "<span class='FormMessage'>Stuff</span>" + label.Text;
 
            e.Canceled = true;
        }
    }
}

Hope that this clarifies the issue. 

 

Regards,
Konstantin Dikov
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Gavin
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Gavin
Top achievements
Rank 1
Share this question
or