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

Page Redirection on Error

3 Answers 191 Views
Window
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 2
Dan asked on 15 Sep 2009, 10:29 PM
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

3 Answers, 1 is accepted

Sort by
0
Dan
Top achievements
Rank 2
answered on 19 Sep 2009, 04:36 AM
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.
0
Jan
Top achievements
Rank 1
answered on 25 Sep 2013, 06:09 PM
I am new to telerik RadWindow. Please let me know what is the namespace used for ErrorContainer.
0
Shinu
Top achievements
Rank 2
answered on 18 Oct 2013, 07:49 AM
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.
Tags
Window
Asked by
Dan
Top achievements
Rank 2
Answers by
Dan
Top achievements
Rank 2
Jan
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or