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

How to display an error message in an EditForm

16 Answers 338 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve Newbery
Top achievements
Rank 1
Steve Newbery asked on 03 Oct 2008, 11:02 AM
I'm using an EditForm in my radGrid, and I'm handling SQL Server errors using the technique shown in the example here http://www.telerik.com/help/aspnet-ajax/grderrorhandling.html

This works ok, except that the error message is displayed at the very bottom of the grid.
 
How can I display the message in the EditForm itself? I tried adapting the code for the DisplayMessage sub, passing e.Item as below, but it doesn't work. No error, but the message isn't visible:

    Private Sub DisplayMessage(ByVal text As String, ByVal theItem As GridEditableItem)  
         If (TypeOf theItem Is GridEditableItem) Then  
            'cast the item to GridEditFormItem  
            Dim formItem As GridEditFormItem = CType(theItem, GridEditFormItem)  
            'get the cell which wraps the controls inside the edit form  
            Dim cell As TableCell = CType(formItem.EditFormCell, TableCell)  
            ' Add a control with the error message  
            cell.Controls.Add(New LiteralControl([text]))  
        End If  
    End Sub  
 

Can anyone help please?

Many thanks, Steve

16 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 06 Oct 2008, 01:38 PM
Hi Steve,

Attached to this message, is a small application, which handles a similar functionality.
I hope this helps.

All the best,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Steve Newbery
Top achievements
Rank 1
answered on 10 Oct 2008, 10:17 AM
Yavor thanks for this. I have a couple of issues:

1. In my RadGrid, I'm using templated columns, and I'm not sure how to adapt your example for that.

2. When I try it with a GridBoundColumn, the error message is not visible. I'm guessing this is because the grid is Ajaxified?

3. I want to make the error message display generic, so it isn't associated with a particular control.

I have tried the following approach, which partially works. First I create a new label control in the EditForm during the ItemCreated event:

    Private Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated  
        '-----------------------------------------------------------------------  
        ' Add a label for the error message  
        '-----------------------------------------------------------------------  
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then  
 
            'cast the item to GridEditFormItem  
            Dim formItem As GridEditFormItem = CType(e.Item, GridEditFormItem)  
 
            'get the cell which wraps the controls inside the edit form  
            Dim cell As TableCell = CType(formItem.EditFormCell, TableCell)  
 
            ' Add a control  
            Dim theLabel As New Label  
            theLabel.ID = "errMessageLabel" 
            theLabel.Text = "errMessageLabel" 
            theLabel.CssClass = "validationMessageBox" 
            cell.Controls.Add(theLabel)  
 
        End If  
 
    End Sub  
 

This works, and I can see the label. This approach allows me to add css styling to the label, eg red text etc.

Then in the error handler, which is called by the ItemUpdated, ItemInserted and ItemDeleted handlers, I set the text of the label:

    Private Sub DisplayMessage2(ByVal errText As String, ByVal theItem As GridEditableItem)  
          
        'cast the item to GridEditFormItem  
        Dim editForm As GridEditFormItem = CType(theItem, GridEditFormItem)  
 
        'get the cell which wraps the controls inside the edit form  
        Dim cell As TableCell = CType(editForm.EditFormCell, TableCell)  
 
        'set the error message  
        Dim theLabel As Label = cell.FindControl("errMessageLabel")  
        theLabel.Text = errText 
              
    End Sub  
 

But this doesn't work. In debugging I can see that the label text is indeed set to the message text, but presumably as there is no callback we don't see the change.

I was thinking I have to wire up the Update button with AjaxManager so that the code will work, but I couldn't figure out how to do it....

Can you help me here plz? Many thanks, Steve

0
Steve Newbery
Top achievements
Rank 1
answered on 10 Oct 2008, 01:17 PM

Yavor, I can see a bit more what is happening.

The example you setup uses the OnUpdate event, which works ok. But, the event I have to use is ItemUpdated, and in this case it does not work.

    protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e)  
    {  
        e.KeepInEditMode = true;  
        LiteralControl literalControl = new LiteralControl();  
        literalControl.Text = "Error message goes here";  
        GridEditFormItem editForm = (GridEditFormItem)e.Item;  
        editForm["CategoryName"].Controls.Add(literalControl);  
    } 

In addition, any entered form values are reset, which would look strange to the user.

So I'd like to know how I can get the message displayed in the EditForm when an exception is found in the ItemUpdated event, eg an SQL Server error, and also how to preserve the entered form values in that case.

Regards, Steve

0
Yavor
Telerik team
answered on 14 Oct 2008, 07:56 AM
Hello Steve,

The update command allows you to get the data entered by the user, and then act according to it. Indeed, the values entered prior to a failed validation are lost, however, since you have a chance to get them, you can then restore them; once you cancel out the update event, you can add the warning, and then reference the textboxes in the editForm, and reset the last values entered.
I hope this information helps.

Best wishes,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Steve Newbery
Top achievements
Rank 1
answered on 14 Oct 2008, 11:24 AM
Yavor, thanks for the reply. I will try and implement the idea for restoring the form values.

But my main problem is getting the error message to be displayed in the EditForm itself. The new LiteralControl added is not visible.

Could you adapt your example project to use the ItemUpdated event? You'll see what I mean I think.

Regards, Steve

0
Yavor
Telerik team
answered on 16 Oct 2008, 11:37 AM
Hi Steve,

The ItemUpdated event handler is raised only after the event is complete, and may be used for error handling with automatic operations. You can use the itemcommand/Updatecommand handlers to achieve this task, as shown in the sample attached earlier.
Let me know how this meets your requirements.

Best wishes,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Steve Newbery
Top achievements
Rank 1
answered on 23 Oct 2008, 12:25 PM
Yavor, ok so with automatic operations, I can only trap server errors in the ItemUpdated/Inserted/Deleted handlers?

This is a pity, because it means I cannot display the error message within the edit form, and also the entered values are lost.

So could I acheive what I want by not using automatic operations, and trapping the error in the UpdateCommand handler?
0
Yavor
Telerik team
answered on 27 Oct 2008, 10:49 AM
Hello Steve,

You can use ItemCommand event handler, to detect any error, or handle validation of user input. This event can be canceled, and the values which the user entered can be manually restored. Although this requires a little extra coding, it is still a straightforward approach.

Regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Will Ngo
Top achievements
Rank 1
answered on 23 Nov 2008, 03:57 AM
Steve and Yavor

Since my update to Q3 2008 to fix the bugs in Q2, my entire application's error handling crashed terribly. I am really frustrated with every update where I have to change my code significantly to adapt to the newer version.

I used to be able to do what Steve wanted to do in Q2 2008. I had template edit column where I place a permanent placeholder which can be used to add error message if Exception != null in the ItemUpdated event. The error message displayed properly where i put the PlaceHolder AND the EditForm keeps in Edit Mode the user inputed values rather than restoring them to the original values without additional coding.

Now in Q3 2008, I have no idea how to detect when Exception occurs AND display the error message in the Edit form. Also now I have to write code to keep the user inputed values after an exception has happened.

Yavor, I don't see in your code how you detected when there is an Exception. You just had e.Canceled = true right at the beginning of the handler. How do you know if there was an error to respond to?

I would appreciate if you provide further help on this.

I have also opened a support ticket.

Thanks.
0
Yavor
Telerik team
answered on 25 Nov 2008, 06:47 AM
Hello Will Ngo,

To avoid duplicate posts, we will continue our communication in the support ticket that you have opened on the matter.

All the best,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Phil C
Top achievements
Rank 2
answered on 05 Dec 2008, 03:46 PM
I too am having to adjust Q3 broken error-messaging code - not a problem if things change for the better, although can't see it yet. Previously working code (as per the current Help file and example) is producing blanks for e.Item("FieldName").Text on ItemUpdated etc.

To save everybody opening tickets and pushing string, is it possible to have a proper example of inline field error handling and update messaging (with templates and a dedicated inline label, not a LiteralControl tacked on the bottom of the grid)?
0
Yavor
Telerik team
answered on 08 Dec 2008, 02:14 PM
Hello Phil,

I will make sure the corresponding help article is updated as per the information in this thread.

Sincerely yours,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Will Ngo
Top achievements
Rank 1
answered on 12 Dec 2008, 03:13 AM
Hello Telerik

CAN I SUGGEST the previous version's approach to exception handling for Automatic Update/Insert in EditForm template be restored in the next hotfix/service pack. That is the NEW VALUES as entered or set by end user in the Edit/Insert form template's controls (eg Textbox) are kept unchanged if an exception occurred and KeepInEditMode = true is set in ItemUpdated event handler.

This is more sensible from user's experience pov and requires no code from developer's pov.

All other approaches that have been provided in this post requires so much coding and are far less extensible/customizable/reusable.

The old way was so elegant.

Thanks
0
Yavor
Telerik team
answered on 16 Dec 2008, 09:37 AM
Hi Will,

Thank you for getting back to us.
As mentioned in the support ticket on the matter, we will consider altering this behavior, if possible, for one of the upcoming versions of the control.

Best wishes,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Imran Khalid
Top achievements
Rank 1
answered on 12 May 2009, 09:05 PM
Whats the update on it... i am also losing my new values when exception is thrown and data access layer is called to populate with the original values from the database.. which is JUST USELESS trip to database....

Any comments by Telerik.... ???
0
Georgi Krustev
Telerik team
answered on 15 May 2009, 10:20 AM
Hello Imran,

I hope you will agree with me that we could not change the source code of RadGrid in order to achieve the suggested behavior without performing thorough research/creating a stable solution and consider whether this is attainable at all. Actually we already did that and it turns out that we are not able to implement the requested functionality because of the complex object model and the internal logic of RadGrid for ASP.NET AJAX which cannot be altered in such way.

I hope you understand our position and the workaround provided in this forum thread is applicable for your situation.

Kind regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Steve Newbery
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Steve Newbery
Top achievements
Rank 1
Will Ngo
Top achievements
Rank 1
Phil C
Top achievements
Rank 2
Imran Khalid
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or