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

Why RadGrid does not retain user input?

10 Answers 125 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Justin Maslow
Top achievements
Rank 1
Justin Maslow asked on 10 Oct 2013, 01:05 PM
I have been playing with RadGrid in my asp.net web application for a while. One of the problems I have encountered with RadGrid is that it does not retain user inputs on postback.  I've found no elegant solution to this problem.

For example, I have multiple row editable in my RadGrid, when I save through a Save button at the bottom of the page and validation fails, the page refreshes, and all of my inputs are lost.  The NeedDataSource event handler fires, but of course, it only retrieves my old data from the database and rebinds.  It should keep my new input in place when validation fails.

Am I doing anything wrong?  Please advise, thank you!

10 Answers, 1 is accepted

Sort by
0
Justin Maslow
Top achievements
Rank 1
answered on 11 Oct 2013, 04:16 PM
Anyone knows why? Any elegant solution?
0
Justin Maslow
Top achievements
Rank 1
answered on 14 Oct 2013, 03:27 PM
Even technical support at telerik does not have an answer? I am sure a lot of people are wondering about this problem.
0
Konstantin Dikov
Telerik team
answered on 15 Oct 2013, 08:29 AM
Hello Justin,

Could you please provide more information about your scenario by answering the following questions:
  • What edit mode you are using?
  • How do you validate the input?
  • How do you save the changes?

In the meantime, please take a look at our online demo "Grid - Flexible Server-Side Validation" that could help you with your project.


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
Justin Maslow
Top achievements
Rank 1
answered on 15 Oct 2013, 04:02 PM
Thank you for getting back, Telerik. I really want to get this going.

To answer your questions.

  1. I have AllowMultiRowEdit="true" to allow editing multiple rows.  Also enabled Add NewRow at the bottom of the RadGrid.
  2. I have a custom validator that validates user input in the whole grid
  3. I have a Save button OUTSIDE of the RadGrid, when it is clicked, I harvest all user input in the RadGrid and save them to my database.

I created an isolated project, in which I have a asp.net GridView control and a RadGrid side by side.

I change and enter some data in both GridView and the RadGRid, then I hit the save button to try to Save my input to database.

Bang! Validation fails, page refreshes, yet the GridView nicely keeps my input, but the RadGrid totally wipes out all changes or new input from the grid, and rebinds to the old set of data. This is a very bad design, and makes it very hard to use.  

Is there anything I am missing or doing wrong with the RadGrid?  Please advise, thank you!
0
Justin Maslow
Top achievements
Rank 1
answered on 16 Oct 2013, 02:15 PM
Mr. Konstantin? Are you there? Could you take a look at my response to your questions and the situation I have? Thank you.
0
Konstantin Dikov
Telerik team
answered on 18 Oct 2013, 09:03 AM
Hi Justin,

Could you please test the attached sample page that works as expected on my side and see what differs from your project.

If you continue to experience the issue, please modify the attached project so it could replicate the problem so we could test 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
Justin Maslow
Top achievements
Rank 1
answered on 21 Oct 2013, 02:43 PM
Thank you for providing the sample project.  I played with it a little bit and I found how you can repeat the issue.

Here is my web app's requirement with regard to RadGrid:

  1. Put RadGrid into edit mode at page load, in other words, the RadGrid is already in Edit mode immediately after the page loads. We don't even have the GridEditCommandColumn in the Grid.
  2. Save the whole grid with a click of the Save button OUTSIDE of the RadGrid.

Fairly simple.  We do have other functionality requirement such as adding a new row from the bottom, but it's irrelevant.

In order to achieve 1, I have to declare  OnPreRender="RadGrid1_PreRender" in the markup and implement this event handler as shown below:

 protected void RadGrid1_PreRender(object sender, System.EventArgs e)
  {
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
      if (item is GridEditableItem)
        (item as GridEditableItem).Edit = true;
    }

    if (RadGrid1.MasterTableView.IsItemInserted)
    {
      var insertedItem = RadGrid1.MasterTableView.GetInsertItem();
      insertedItem.Edit = true;
    }

    RadGrid1.Rebind();
  }

This PreRender event handler causes my input to lose.  As soon as I add this event handler, I lose my input when validation fails, and as soon as I remove this PreRender event handler, and restore the GridEditCommandColumn (just so I can turn rows into edit mode), RadGrid nicely keeps my input.

So, what's the solution? Removing the PreRender event handler is not an option for me unless you have another way to turn this entire RadGrid into edit mode on page loads.

The updated Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager runat="server" ID="radscriptmanager1">
        </telerik:RadScriptManager>

        <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender"
            AutoGenerateColumns="false" AllowMultiRowEdit="true" MasterTableView-EditMode="InPlace">
            <MasterTableView>
                <Columns>
                    <%--<telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>--%>
                    <telerik:GridBoundColumn HeaderText="UNITS" DataField="UnitsInStock" UniqueName="UnitsInStock"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="PRINCE" DataField="UnitPrice" UniqueName="UnitPrice"></telerik:GridBoundColumn>

                    <%--<telerik:GridTemplateColumn HeaderText="UnitsInStock#" UniqueName="UnitsInStock">
<ItemTemplate>
<asp:Label ID="lblPart_No" runat="server" Text='<%# Bind("UnitsInStock") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPart_No" runat="server" Text='<%# Bind("UnitsInStock") %>' Width="60px"></asp:TextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>--%>

                    <%--<telerik:GridTemplateColumn HeaderText="UnitPrice" UniqueName="UnitPrice">
<ItemTemplate>
<asp:Label ID="lblPartName" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPartName" runat="server" Text='<%# Bind("UnitPrice") %>' Width="60px"></asp:TextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>--%>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

        <asp:CustomValidator ID="CustomValidator1" EnableClientScript="false"
            OnServerValidate="CustomValidator1_ServerValidate"
            Text="Validation failed"
            runat="server" />

        <asp:Button ID="Button1" Text="Save" OnClick="SaveButtonClick" runat="server" />
    </form>
</body>
</html>

And the updated code behind:

using System;
using System.Linq;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }

  protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
  {
    RadGrid1.DataSource = Enumerable.Range(1, 20).Select(i => new
    {
      UnitsInStock = i,
      UnitPrice = i
    });
  }

  protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
  {
    args.IsValid = false;
  }

  protected void SaveButtonClick(object sender, EventArgs e)
  {
    if (IsValid)
    {
      //Save data.
    }
  }

  protected void RadGrid1_PreRender(object sender, System.EventArgs e)
  {
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
      if (item is GridEditableItem)
        (item as GridEditableItem).Edit = true;
    }

    if (RadGrid1.MasterTableView.IsItemInserted)
    {
      var insertedItem = RadGrid1.MasterTableView.GetInsertItem();
      insertedItem.Edit = true;
    }

    RadGrid1.Rebind();
  }
}

I am very serious with this issue, and I am looking forward to your response. Thank you very much for your continued support.


0
Justin Maslow
Top achievements
Rank 1
answered on 22 Oct 2013, 12:09 PM
Hi, Mr. Dikov? Could you take a look at my response and tell me if there is a solution to the problem? Thank you!
0
Konstantin Dikov
Telerik team
answered on 23 Oct 2013, 10:52 AM
Hi Justin,

With the provided modification on my sample page I was able to observe the described behavior. 

Nevertheless, in your scenario, every time when you postback you call the Rebind() method in RadGrid's "PreRender" event handler and the values in the edit items are being loaded again from the data source.

Could you please try to change your "PreRender" event handler with the following and see if it helps in your project:
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
    if (!Page.IsPostBack)
    {
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            if (item is GridEditableItem)
                (item as GridEditableItem).Edit = true;
        }
 
        if (RadGrid1.MasterTableView.IsItemInserted)
        {
            var insertedItem = RadGrid1.MasterTableView.GetInsertItem();
            insertedItem.Edit = true;
        }
 
        RadGrid1.Rebind();
    }
}

If further assistance is needed, please feel free to get back to us.

 

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
Justin Maslow
Top achievements
Rank 1
answered on 24 Oct 2013, 12:47 PM
Thank you, Mr. Dikov for your support.

Including the check of Page.IsPostBack works. Thank you. I will continue to test as I have other controls on the same page and I will check and see if any problem happens.  In particular, I wonder if the RadGrid will not be rendered into edit mode on postback since we are basically saying, render it into edit mode only if !Page.IsPostBack is true.
Tags
Grid
Asked by
Justin Maslow
Top achievements
Rank 1
Answers by
Justin Maslow
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or