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

How to show error message in Insert Popup

3 Answers 366 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bill CT
Top achievements
Rank 1
Bill CT asked on 21 Jan 2010, 05:56 AM
If you look in the attached image, I am inserting a row into a radgrid with the Editmode="Popup".  The insert is failing because there is already a row with the user/password combination.

I am using the following code to display the error.
        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e) 
        { 
            if (e.Exception != null) 
            { 
                Exception eex = e.Exception; 
 
                string msg; 
                if (ex.InnerException != null) msg = ex.InnerException.Message; 
                else msg = ex.Message; 
 
                RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", msg))); 
                e.ExceptionHandled = true
            } 
 
             
        } 

I would like to change the code above to display the error in the popup edit window instead of at the bottom of the grid itself.  Anyone know how to do this?

I've tried to put a label in the edit window but whenever I assign a value to the label in the RadGrid1_ItemInserted function, the new value is never rendered on the popup window.

Thanks.


3 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 25 Jan 2010, 12:14 PM
Hello Bill CT,

If you need to display the error message in the popup window, you will need to find the popup structure in the contols collection of your e.Item object:

protected void RadGrid1_ItemInserted(object sender, GridInsertedEventArgs e)
{
    if (e.Exception != null)
    {
        string msg = e.Exception.InnerException != null ? e.Exception.InnerException.Message : e.Exception.Message;
 
        Table popupTable = e.Item.Cells[1].Controls[0].Controls[1].Controls[0] as Table;
        popupTable.Rows.Add(new TableRow());
 
        TableCell cell = new TableCell();
        cell.ColumnSpan = 2;
        cell.Text = String.Format("<span style='color:red'>Error: {0}</span>", msg);
        popupTable.Rows[popupTable.Rows.Count - 1].Cells.Add(cell);
 
        e.ExceptionHandled = true;
    }
}

The above code finds the <table> element rendered in to the popup window and adds an additional row and cell to its bottom. Thus, we display the error message in the popup.

Sincerely yours,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Amber
Top achievements
Rank 1
answered on 17 Apr 2010, 12:39 AM
This works fine on an insert, but on an update I'm trying the same code.  The code is executing and blocking the update, but the label is not showing up. 
0
Veli
Telerik team
answered on 19 Apr 2010, 02:01 PM
Hi Amber,

For updates, the control behavior is slightly different, i.e. the control rebinds after update, no matter if there is an exception or not. This means that the edit form item you are updating does no longer exist once the grid is rebound. The popup is recreated and, effectively, the objects are different. This means that you cannot use the ItemUpdated event handler to modify the popup. You need to use ItemDataBound instead:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !string.IsNullOrEmpty(msg))
    {
        Table popupTable = e.Item.Cells[1].Controls[0].Controls[1].Controls[0] as Table;
        popupTable.Rows.Add(new TableRow());
 
        TableCell cell = new TableCell();
        cell.ColumnSpan = 2;
        cell.Text = String.Format("<span style='color:red'>Error: {0}</span>", msg);
        popupTable.Rows[popupTable.Rows.Count - 1].Cells.Add(cell);
    }
}
 
string msg = "";
 
protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e)
{
    if (e.Exception != null)
    {
        msg = e.Exception.InnerException != null ? e.Exception.InnerException.Message : e.Exception.Message;
        e.ExceptionHandled = true;
    }
}


Regards,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Bill CT
Top achievements
Rank 1
Answers by
Veli
Telerik team
Amber
Top achievements
Rank 1
Share this question
or