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

[Solved] Insert Errors -> Client Side Javascript

6 Answers 198 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Christoph
Top achievements
Rank 1
Christoph asked on 22 Apr 2009, 03:08 PM
Dear Sirs

I have the following problem you might be able to help me.

I have the following scenario:
I have a RadGrid inside an Ajax update Panel.

If someone tries to insert an entry that already exists and the DB doesnt permit this I get an error.

I now use the Grid Item onInserted event handler where I get the error message I need.

What I do wanna do now is get that error message formated and that formated error string back in my Website's clientside javascript where I use this string to display the error message.

Any clues on how to get that error message back to the client?

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 23 Apr 2009, 11:45 AM
Hi Christoph,

One suggestion to achieve this functionality is by using the RegisterStartupScript() method as shown below. Give a try with this and see whether it helps.

CS:
 
protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e) 
    if (e.Exception != null
    { 
        e.ExceptionHandled = true
        string msg = "Product cannot be inserted. Reason: " + e.Exception.Message; 
        string script = "<script language='javascript' type='text/javascript'>showMessage('" + msg + "');</script>"
        ClientScript.RegisterStartupScript(this.GetType(), "showMessage", script);    
    } 

JavaScript:
 
<script type="text/javascript"
function showMessage(msg) 
    alert(msg); 
</script> 

Thanks,
Shinu.
0
Christoph
Top achievements
Rank 1
answered on 23 Apr 2009, 12:31 PM
Hi Shinu

Thanks much for your reply.
I tried this but it doesnt seem to work.

I'm not even sure if ClientScript.RegisterStartupScript can work through an Ajax Update Panel.
What happens is simply nothing :-(
No Client side script will get called with the suggested code.

I think in a normal Click / Postback situation the Startupscript will actually show the message.
If its an Ajax update though it wont or cannt register Startup Scripts after the page loaded.

Any other hints on how I could get a message back to the client?
0
Iana Tsolova
Telerik team
answered on 27 Apr 2009, 09:22 AM
Hi Christoph,

Can you confirm that the code you added on ItemInserted event is executed? You can try if ScriptManager.RegisterStartupScript() method would work for registering your client code.

Let me know how it goes.

Sincerely yours,
Iana
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.
0
Christoph
Top achievements
Rank 1
answered on 27 Apr 2009, 09:51 AM

Hi Lana

The code on ItemInserted is being executed yes. It also does the ClientScript.RegisterStartupScript doing no error at all.
The Problem is that the grid is inside an AJAX Update Panel. So register RegisterStartupScript seems do do absolutely nothing after the Page was initially loaded.

What i tried was removing the update panel (= Ugly page postback)
Which perfectly executed the JavaScript passed via RegisterStartupScript.

As soon as the Grid is inside an Update Panel it cannt register a startup script (how could he.. page is already loaded)

So my question is how do I get that error message.. past the AJAX Update Panel back to my Client Side JavaScript Code?

<asp:UpdatePanel ID="UpdatePanel2" runat="server">  
    <ContentTemplate> 
        &nbsp;<br /> 
        <div style="font-weight: bold;margin-bottom:8px;">  
            <%=(string)HttpContext.GetGlobalResourceObject("toptiplabel", "appsetting_Lager")%></div>  
        <telerik:RadGrid OnItemInserted="LagerRadGrid_ItemInserted" OnItemUpdated="LagerRadGrid_ItemUpdated" 
            OnItemDeleted="LagerRadGrid_ItemDeleted" Width="260px" ID="LagerRadGrid" runat="server" 
            AutoGenerateColumns="False" DataSourceID="LagerObjectDataSource" GridLines="None" 
            AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" OnItemCommand="LagerRadGrid_ItemCommand">  
            <HeaderContextMenu> 
                <CollapseAnimation Duration="200" Type="OutQuint" /> 
            </HeaderContextMenu> 
            <MasterTableView DataSourceID="LagerObjectDataSource" EditMode="InPlace" CommandItemDisplay="Top">  
                <CommandItemTemplate> 
                    <asp:ImageButton style="float:right;margin:2px;" OnClientClick="ToggleLager()" ID="ImageButton6" runat="server" ImageUrl="~/gfx/icons/close.gif" /> 
                    <asp:LinkButton Style="margin-left: 8px" ID="LinkButton2" runat="server" CommandName="InitInsert"><%=(string)HttpContext.GetGlobalResourceObject("toptiplabel", "lager_add")%></asp:LinkButton> 
                </CommandItemTemplate> 
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="ID" HeaderText="Id" UniqueName="Id" Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn Visible="True" HeaderStyle-Width="205px" DataField="Name" UniqueName="Name" HeaderText="<%$ Resources:toptiplabel, appsetting_Lager %>">  
                        <EditItemTemplate> 
                            <asp:TextBox Width="195px" Font-Size="9pt" ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox> 
                        </EditItemTemplate> 
                        <ItemTemplate> 
                            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridTemplateColumn HeaderText="" UniqueName="TemplateColumn">  
                        <EditItemTemplate> 
                            <asp:ImageButton Visible='<%# LagerRadGrid.MasterTableView.IsItemInserted %>' ID="ImageButton5" 
                                runat="server" CommandName="PerformInsert" Text="Insert" ImageUrl="~/gfx/icons/noerrors.gif" 
                                ToolTip="<%$ Resources:Global, Add %>"></asp:ImageButton> 
                            <asp:ImageButton Visible='<%# LagerRadGrid.EditIndexes.Count > 0 %>' ID="ImageButton1" 
                                runat="server" CommandName="Update" Text="Update" ImageUrl="~/gfx/icons/noerrors.gif" 
                                ToolTip="<%$ Resources:Global, Save %>"></asp:ImageButton> 
                            &nbsp;<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="false" 
                                CommandName="Cancel" Text="Cancel" ImageUrl="~/gfx/icons/cancel.gif" ToolTip="<%$ Resources:Global, Cancel %>">  
                            </asp:ImageButton> 
                        </EditItemTemplate> 
                        <ItemTemplate> 
                            <asp:ImageButton ID="ImageButton3" runat="server" CausesValidation="false" CommandName="Edit" 
                                Text="Edit" ImageUrl="~/gfx/icons/edit.gif" ToolTip="<%$ Resources:Global, aendern %>">  
                            </asp:ImageButton> 
                            <asp:ImageButton ID="ImageButton4" runat="server" OnClientClick="return DeleteCheck()" 
                                CausesValidation="false" CommandName="Delete" Text="Delete" ImageUrl="~/gfx/icons/delete.gif" 
                                ToolTip="<%$ Resources:Global, loeschen %>"></asp:ImageButton> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                </Columns> 
            </MasterTableView> 
            <FilterMenu> 
                <CollapseAnimation Duration="200" Type="OutQuint" /> 
            </FilterMenu> 
        </telerik:RadGrid> 
    </ContentTemplate> 
</asp:UpdatePanel> 
/// <summary>  
/// Rebind the LagerMatrixRadGrid to reflect the changes  
/// </summary>  
/// <param name="source"></param>  
/// <param name="e"></param>  
protected void LagerRadGrid_ItemInserted(object source, GridInsertedEventArgs e)  
{  
    if (e.Exception != null)  
    {  
        e.ExceptionHandled = true;  
        string msg = e.Exception.Message;  
        string script = "<script language='javascript' type='text/javascript'>showMessage('" + msg + "');</script>";  
        ClientScript.RegisterStartupScript(this.GetType(), "showMessage", script);  
    }    
 
    LagerMatrixRadGrid.MasterTableView.Rebind();  
0
Iana Tsolova
Telerik team
answered on 27 Apr 2009, 02:34 PM
Hi Christoph,

Indeed, the RegisterStartupScript() method works for me. Please check out the attached sample and let me know if it works on your end and what differs in your case.

Greetings,
Iana
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.
0
Christoph
Top achievements
Rank 1
answered on 27 Apr 2009, 02:53 PM
Hi Lana

Thanks much for that quick sample, and indeed it works here too.

ScriptManager.RegisterStartupScript seems to work
ClientScript.RegisterStartupScript doesnt

I'm glad this finally works because this got me some headache :-)

Thanks again for the quick help!

Greetings

Christoph
Tags
Grid
Asked by
Christoph
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Christoph
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or