Page Redirection on Error

4 posts, 0 answers
  1. Daniel
    Daniel avatar
    4 posts
    Member since:
    Sep 2009

    Posted 15 Sep 2009 Link to this post

    Hi,

    I'm using a modal RadWindow control as a popup which allows users to select certain elements for processing.  When this processing successfully completes, the RadWindow uses the RegisterStartupScript method of the ScriptManager object to call a Javascript function which closes the RadWindow and runs another Javascript function on the parent page to refresh a RadGrid control.

    In the RadWindow code behind, the button that initiates the processing is tied to a RadAjaxPanel control for the purpose of displaying a LoadingPanel.  I am overriding the OnError event to capture and record the details of any error that bubbles up from the other application layers.

    The code works wonderfully when the processing completes successfully, or when a user clicks on a "cancel" button to exit the RadWindow and return to the parent page.  However, if there is an error, I can't figure out how to get the RadWindow to close.  I'm using IE 7, and all I see is the yellow "warning" triangle that contains mention of an error message to the effect of "'length' is null or not an object" (although I'm not using the "length" property of anything, referencing arrays, etc. in my code).

    In the overridden OnError event, I've tried using ScriptManager.RegisterStartupScript and ClientScript.RegisterStartupScript, and good old Response.Write() to try to get the page to run any Javascript after the error--but nothing is working.  Any ideas on what I'm missing?  Code is below:

    RadWindow definition on the parent page:
    <telerik:RadWindowManager ID="RadWindowManager1" runat="server"
                    <Windows> 
                        <telerik:RadWindow ID="NewSamplePopup" 
                            Behaviors="Move,Pin" 
                            ReloadOnShow="true" 
                            Modal="true" 
                            runat="server" 
                            Height="520" 
                            Width="600" 
                            NavigateUrl="./NewSamplePopup.aspx" 
                            VisibleStatusbar="false" 
                            Title="Getting Available Sample Types...Please Wait" 
                            ShowContentDuringLoad="false"
                        </telerik:RadWindow> 
    </telerik:RadWindowManager> 





    Javascript definition in the RadWindow's markup:
    <body> 
        <telerik:RadCodeBlock runat="server"
            <script type="text/javascript"
                function GetRadWindow() { 
                    var oWindow = null
                    if (window.radWindow) 
                        oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog        
                    else if (window.frameElement.radWindow) 
                        oWindow = window.frameElement.radWindow; //IE (and Moz as well)        
                    return oWindow; 
                } 
     
                function RadWindowClose() { 
                    GetRadWindow().Close(); 
                } 
                 
                function CloseAndRebind(args) { 
                    GetRadWindow().Close(); 
                    GetRadWindow().BrowserWindow.refreshGrid(args); 
                } 
                 
            </script> 
        </telerik:RadCodeBlock> 

    Relevant methods from the RadWindow code-behind:
            protected void btnGetTests_Click(object sender, EventArgs e) 
            { 
                // code that throws an error... 
            } 
     
           protected void btnCancel_Click(object sender, EventArgs e) 
            { 
                // close the form - this works!!! 
                ScriptManager.RegisterStartupScript(thisthis.GetType(), "close""RadWindowClose();"true); 
            } 
     
    protected override void OnError(EventArgs e) 
            { 
                // At this point we have information about the error 
                HttpContext ctx = HttpContext.Current; 
     
                Exception exception = ctx.Server.GetLastError(); 
                ///////// 
                // Logic to handle error here 
                ///////// 
                ctx.Server.ClearError(); 
     
                // Gets called but doesn't work! 
                ScriptManager.RegisterStartupScript(thisthis.GetType(), "close""RadWindowClose();"true); 
     
            } 

    Ultimately I would like the Javascript function to redirect the parent page to a custom error screen.  I'm pretty sure I can do this once I get the RadWindow to execute any Javascript.

    Thanks!

    Dan

  2. Daniel
    Daniel avatar
    4 posts
    Member since:
    Sep 2009

    Posted 18 Sep 2009 Link to this post

    Fortunately I managed to figure out how to do this.  For those without the patience to read the long post above, I was trying to get a RadWindow used as a modal popup to catch an error, close itself, and redirect the parent page to a custom error page.  I'll post this for any others that may be trying to do something similar.

    Step 1: Create a JS file for your Javascript.  You will need the following functions, so paste all of this in the file.  (You can add other functions here as well (such as close/rebind functions), so if you have more than one modal popup sharing the same code you can maintain it in one place.)

    Sys.Application.add_init(AppInit); 
     
    function AppInit(sender) { 
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); 
     
    function GetRadWindow() { 
        var oWindow = null
        if (window.radWindow) 
            oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog        
        else if (window.frameElement.radWindow) 
            oWindow = window.frameElement.radWindow; //IE (and Moz as well)        
        return oWindow; 
    function EndRequestHandler(sender, args) { 
        if (args.get_error() != null) { 
            args.set_errorHandled(true); 
            CloseAndSendToError(); 
        } 
    function CloseAndSendToError() { 
        GetRadWindow().Close(); 
        GetRadWindow().BrowserWindow.SendToError(); 


    Step 2: Add the following Javascript to your parent ASPX page:

    function SendToError() { 
         // redirection to the custom error page 
         window.location = "Error.aspx"


    Step 3: Modify the ScriptManager object on the ASPX page that contains your RadWindow modal popup content.  You will need to add an event handler for the OnAsyncPostBackError event and add a ScriptReference to your Javascript file.

    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" OnAsyncPostBackError="RadScriptManager1_AsyncPostBackError"
        <Scripts> 
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" /> 
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" /> 
            <asp:ScriptReference Path="scripts/GlobalFunctions.js" /> 
        </Scripts> 
    </telerik:RadScriptManager> 


    Step 4: Add the following event to the C# code behind file of the RadWindow modal popup page:

            protected void RadScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e) 
            { 
                // First, get the error from the HttpContext 
                HttpContext ctx = HttpContext.Current; 
                Exception exception = ctx.Server.GetLastError(); 
     
                // Here, I put error information in a custom object and  
                // store it in session.  The custom error page pulls the 
                // object from session.  You can do whatever you want here... 
                ErrorContainer err = new ErrorContainer(); 
                err.CallingPage = PAGE_NAME; 
                err.OtherStuff = "some other stuff here"
                err.ErrorMessage = exception.ToString(); 
                Session["ErrorMessage"] = err; 
     
                // Clear the error message 
                ctx.Server.ClearError(); 
                RadScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message; 
            } 

    Enjoy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jan
    Jan avatar
    1 posts
    Member since:
    Sep 2013

    Posted 25 Sep 2013 Link to this post

    I am new to telerik RadWindow. Please let me know what is the namespace used for ErrorContainer.
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Oct 2013 Link to this post

    Hi Jan,

    There is no such Class "ErrorContainer" under any .NET namespace or Telerik.Web.UI namespace. I guess you are referring the code Daniel had posted. The ErrorContainer used in Daniel's code can be some custom Class he has defined to manage errors.

    Thanks,
    Shinu.
Back to Top