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

How to keep the insert/edit form from clearing the inputed data.

5 Answers 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 04 Oct 2011, 02:02 PM
I'm using a stored procedure that returns an error message if the inserted/updated data violates certain criteria. (name already exists...etc)

I'm able to get the message back fine using
Protected Sub GridViewDataSource_Inserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs) Handles GridViewDataSource.Inserted
        Dim command As DbCommand
        command = e.Command
 
        If Not e.Exception Is Nothing Then
            e.ExceptionHandled = True
            SetMessage("Data cannot be inserted. Reason: " + e.Exception.Message)
        Else
            If command.Parameters("@RetMsg").Value.ToString().Length > 0 Then
                SetMessage("Data cannot be inserted. Reason: " + command.Parameters("@RetMsg").Value.ToString())
            Else
                SetMessage("New data has been inserted")
            End If
        End If
    End Sub

...but I also want to keep the insert/edit form in the grid from clearing the data the user was trying to enter.

Any ideas on how to accomplish this?

Once again, thanks to everyone for the continued guidance.

5 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 07 Oct 2011, 07:29 AM
Hello Chris,

There is not a built-in functionality in RadGrid to prevent the clearing of data in such cases. The thing is that the control rebinds and any unsaved values are lost. One thing that you could try is, if possible, to detect whether the insert/edit will fail in the Insert/UpdateCommand event and if so, cancel the event, so that the grid does not perform its default routine.

Other than this, you could save these values in session in Insert/UpdateCommand event and if the command fails, to populate the edit controls again with them.

Best wishes,
Tsvetina
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
Chris
Top achievements
Rank 1
answered on 07 Oct 2011, 01:48 PM
Thanks Tsvetina,

I added e.Canceled = True which prevented the form from being cleared but now my message doesn't show up.
Dim error_message As String = String.Empty
error_message = cmd.Parameters("@RetMsg").Value.ToString
 
If error_message.Length > 0 Then
    SetMessage("Cannot be inserted. Reason: " + error_message)   
Else
    SetMessage("Cannot be inserted")
End If
 
e.Canceled = True

0
Tsvetina
Telerik team
answered on 07 Oct 2011, 01:55 PM
Hi Chris,

This is expected since the ItemInserted event fires only if the InsertCommand runs in full. Therefore, if you managed to cancel InsertCommand when needed you could call the function, which sets the error message, in the same event handler immediatelly after that.

Kind regards,
Tsvetina
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
Chris
Top achievements
Rank 1
answered on 07 Oct 2011, 02:26 PM
Ok, I think I'll try the session approach. I can populate a test label but I can't populate the RadTextBox in my user control.
Protected Sub StatusCodesRadGrid_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles StatusCodesRadGrid.ItemCreated
        If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
            If Not IsNothing(Session("StatusCode")) Then
                Dim userControl As UserControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)
                CType(userControl.FindControl("StatusCode"), RadTextBox).Text = Session("StatusCode")
                lblTest.Text = Session("StatusCode")
            End If
        End If
 
        Session.Remove("StatusCode")
    End Sub

I take it I don't have this correct?
Dim userControl As UserControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)
CType(userControl.FindControl("StatusCode"), RadTextBox).Text = Session("StatusCode")
 
Thanks again for your help.
0
Tsvetina
Telerik team
answered on 07 Oct 2011, 03:28 PM
Hello Chris,

This seems to me like you should be getting an error about invalid cast. Have you tried using Session("StatusCode").ToString()?

Anyway, you could also try this on ItemDataBound or PreRender, since the grid may be overwriting the values set on ItemCreated when it binds.

Best wishes,
Tsvetina
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
Chris
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Chris
Top achievements
Rank 1
Share this question
or