Callback exception/error handling for ASP TreeView OnTreeNodePopulate?
We're using an asp:TreeView configured with lazy loading. The callback method assigned for OnTreeNodePopulate throws an exception if the user has been logged out since the page was loaded. What we want to do is to direct the user to the login开发者_运维问答 page.
First attempt was to catch the exception on the server and try Response.Redirect(...), but that doesn't work because you can't redirect within a callback.
I've tried various other approaches, including using ClientScript.RegisterStartupScript(...) but that doesn't seem to work for OnTreeNodePopulate.
If there was some way we could hook into the callback event handling on the client side then it would be easy, but the TreeView doesn't seem to offer anything here.
Suggestions?
OK, I have a workaround, though I'm still eager to hear other suggestions as this is total filth:
In my callback I catch the exception with the following block:
catch (Exception ex)
{
if (IsAuthException(ex))
{
e.Node.ChildNodes.Add(new TreeNode(@"<script type=""text/javascript"">window.location.href = 'Default.aspx';</script>"));
}
else
{
throw;
}
}
Fortunately the TreeView doesn't escape anything so the JS gets executed by the browser and directs the user to the login page.
精彩评论