Home - Forums-.NET - FlyTreeView (ASP.NET) - Response.Redirect cannot be called in a Page callback

FlyTreeView (ASP.NET)

Technical support and KB related to the FlyTreeView control

This forum related to following products: FlyTreeView for ASP.NET

Response.Redirect cannot be called in a Page callback
Link Posted: 12-Jun-2007 12:28
I have a tree where I am populating the nodes on demand.  In one certain error condition, upon an attempt at the populate on demand, I need to redirect the page to an error page.  However, when I try this, I get the following error:

Response.Redirect cannot be called in a Page callback

Is there a way to do a response.redirect within the PopulateNodes event?  

For example, my test populate routine is as follows and wired up as follows:

...
ftv.PopulateNodes += new FlyTreeNodeEventHandler(flyTreeView_PopulateNodes);
...


protected void flyTreeView_PopulateNodes(object sender, NineRays.WebControls.FlyTreeNodeEventArgs e)
{
        Response.Redirect(\"http://www.microsoft.com\");
}


I get the Response.Redirect cannot be called in a Page callback error on the Response.Redirect.  

Thanks!
Link Posted: 13-Jun-2007 02:08
This is an exception thrown by ASP.NET runtime. And yes, callback does not affect the rest of the page. So from my point of view, you should rather display error node instead of redirection like node.Text = \"Access denied.\", etc. On the other side, you can handle populatefailed node event at client side or expanded to redirect browser using javascript. See more at http://www.9rays.net/asp.net_2/treeview/miscpages/doc/ClientEvents.aspx
Link Posted: 26-Jun-2007 11:41
Although I understand, this is unfortunate.  Other AJAX objects are able to do this.  My opinion is probably not worth much, but I would urge you to consider allowing for this type of functionality.  

Thanks
Link Posted: 26-Jun-2007 12:25
Do you mean ASP.NET AJAX framework?

It buries much deeper into ASP.NET runtime to make this happen.

Since FlyTreeView supports ASP.NET AJAX, you can place FlyTreeView control into UpdatePanel, set postbackonexpand and redirect the way you need when nodes are populating.

But this is ASP.NET AJAX feature, not core ASP.NET Callback one.
Link Posted: 29-Jun-2007 13:18
I'm getting the same error:

Response.Redirect cannot be called in a Page callback.

when doing a redirect in the handler for a context menu. I'm actually outputing a file for download. This works, but as soon as I click on another node to expand on demand, I get this error.

After debugging some more, I see that my context menu handler is being called again when I expand another node. It looks like the Redirect is causing this. In my debugger I see the context menu handler called, I then do the redirect. Afterwards, I then click to exand a different node, and my debugger immediately enters the context menu handler code. It looks like the old context menu event is being raised again. If I comment out the redirect, I only hit the context menu handler once.

How do I fix this? Is this a known bug?

For now I am doing this on the client side, getting the node when the menu item is clicked and doing a location.href...

Thanks,
Rich
Link Posted: 30-Jun-2007 23:05
Rich,

Could you please post here how do you redirect?
Or create a small standalone example that we can use to debug the problem.

I created a simple example using built-in filesystem demo (it uses load-on-demand) and put context menu here having the only item that causes page to postback and redirects to a desired location.


So codebehind is:



using System;

public partial class Demo_Classic : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FileSystemNodes.AddNodes(flyTreeView.Nodes, string.Empty);
        }
    }
    protected void flyTreeView_PopulateNodes(object sender, NineRays.WebControls.FlyTreeNodeEventArgs e)
    {
        FileSystemNodes.AddNodes(e.Node.ChildNodes, e.Node.Path);
    }
    protected void fcm_Command(object sender, NineRays.WebControls.FlyContextMenuCommandEventArgs e)
    {
        Response.Redirect(\"http://www.google.com/\");
    }
}



Context menu declaration:


    
        
    


So when I click the context menu item, page is posted back and redirected.
Link Posted: 06-Jul-2007 16:58
[quote="EvgenyT"]Rich,

Could you please post here how do you redirect?
Or create a small standalone example that we can use to debug the problem.



Hi Evgeny,

As I said in my previous post, the redirect works for me initially. The problem arises when I click another node that populates on demand. I then get the error message.

Can you expand your example to include another node that does a PopulateOnDemand callback and see if that generates the error?


Thanks,
Rich
Link Posted: 07-Jul-2007 23:14
Rich,

I cannot get your idea. Every node in the example uses load-on-demand.
What do you mean \"to include another node that does a PopulateOnDemand callback\".

Could you put any code, or send test page to flytreeview.asp |at| 9rays.net ?
Link Posted: 27-Aug-2007 10:19
I ended up writing a work around for this.  I setup a client-side onNodeEventHandler that deals with this.  When the dynamic populate fails for me, I get an event \"populatefailed\".  I can then check for that event client-side and redirect to an appropriate page.  

function onNodeEventHandler(sender, node, eventType)
{
     if (eventType == \"\"populatefailed\"\")
     {
          top.location.replace(\"http://www.whatever.com\");
     }
}