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

What is proper way to display error message inside auto-generated form

2 Answers 428 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Albert
Top achievements
Rank 1
Albert asked on 21 Dec 2009, 08:22 PM
I am using the auto-generated edit form on the grid and handling any errors in ItemInserted/ItemUpdated event. My question is - what is the proper way to display an error message inside the edit form? If that's not possible, do you have any sample code that would illustrate some other technique like display a popup or anything user friendly? Thank you.

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Dec 2009, 07:29 AM
Hello Albert,

Here is the code that I tried to show Error message in EditForm when updating the record. I added a Label in the EditForm in ItemDataBound event to show the message.

CS:
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode && check) 
        { 
            GridEditFormItem item = (GridEditFormItem)e.Item; 
            Label lbl = new Label(); 
            lbl.ID = "ErrorLabel"
            lbl.Text = "Error"
            lbl.ForeColor = System.Drawing.Color.Red; 
            (item.FindControl("CancelButton").Parent).Controls.Add(lbl); 
            check = false
        } 
    } 
    protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e) 
    { 
        GridEditFormItem item = (GridEditFormItem)e.Item; 
        if (e.Exception != null
        { 
            e.KeepInEditMode = true
            e.ExceptionHandled = true
            check = true
        } 
    } 

You can also show a pop-up alert from code instead of this approach. Checkout the KB article that shows how to call an radalert from code.
Calling radalert from codebehind

-Shinu.
0
Albert
Top achievements
Rank 1
answered on 22 Dec 2009, 03:46 PM
Hello Shinu,
thanks for the sample code. This helped. However, I see some strange grid behavior. Suppose I want to perform the same error handling for both insert errors as well as update errors. If I also have an ItemInserted event handler, the approach below doesn't quite work because ItemDataBound doesn't appear to fire after the error. So, I ended up creating a function like this:

    private void DisplayInsertUpdateError(GridEditableItem item, string message)  
    {  
        Label lbl = new Label();  
        lbl.ID = "ErrorLabel";  
        lbl.Text = "<br>" + message;  
        lbl.ForeColor = System.Drawing.Color.Red;  
        (item.FindControl("CancelButton").Parent).Controls.Add(lbl);  
    } 

I then call this function from ItemInserted event to display insert related messages, while calling it from ItemDataBound event to display update related messages. BTW, when I attempted to call this function on ItemUpdated event, the label never appeared.

While this is a workable solution, I'd love to know if there was a more consistent solution - either handle insert/update errors from ItemInserted/ItemUpdated events OR handle both insert/update errors from ItemDataBound event.
Tags
Grid
Asked by
Albert
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Albert
Top achievements
Rank 1
Share this question
or