开发者

Alert message on Page_Init

开发者 https://www.devze.com 2023-03-23 01:34 出处:网络
I have a security class that is called by a PageBase class (that all pages will inherit from) that will redirect the user to either the home page or the login page (depending on if they\'re logged in

I have a security class that is called by a PageBase class (that all pages will inherit from) that will redirect the user to either the home page or the login page (depending on if they're logged in or not), if they try and access a page they aren't authorized to see. I want to alert the user of this by sayi开发者_开发技巧ng something like,

"You are not authorized to view this page, redirecting you"

Or something like that. I know you can call javascript from your codebehind to show an alert to the user.

In my PageBase class, (again, that all pages will inherit from), there is a common Page_Init(..) method, and when I try and call my js alert from there, nothing happens. I've put breakpoints in my code, and the code is being hit, but the user isn't getting the alert. I assume this is because Page_Init is too early in the page's lifecycle to execute js, and that that is what is causing this issue.

Is there a way around this without having to add a function to each individual page? Also, here are the two ways I have tried:

System.Web.UI.Page page = HttpContext.Current.CurrentHandler as System.Web.UI.Page;
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), "alert", "alert('You do not have access to this page. You are being redirected')", true);

and

Response.Write("<script type='text/javascript'>alert('You do not have access to this page. You are being redirected');</script>");


The issue is NOT that there isn't any page on the Page_Init. The problem is that the server is not sending out anything to the browser because it's too early in the Page Lifecycle. (more coming)

Under normal circumstances, response data is not sent to the browser until the Rendering event, even if you call Response.Write. This can often be forced by calling Response.Flush() immediately after Response.Write BUT that often has unintended consequences. In the Page_Init cycle, I think that calling Response.Flush() would probably cause you plenty of problems.

You might be better served simply creating a static html page with the "we're redirecting" message, and using a javascrip.SetTimeout method to count down and use javascript to redirect after a few seconds. That's how it's been done on the web for years. Your base class can simply redirect to this page when the user is not authorized.

Code for the "redirect" page.

<html>
 <head>
 <script type="text/javascript">
 function delayedRedirect(){
     window.location = "/default.aspx"
 }
 </script>
 </head>
 <body onLoad="setTimeout('delayedRedirect()', 3000)">
 <h2>You are not authorized to view this page, redirecting you</h2>
 </body>
 </html> 


You need to do implement the logic on Page_Load event on the base page as so:

ScriptManager.RegisterStartupScript(this, this.GetType(), "keykey", "alert('You do not have access to this page. You are being redirected'); window.location='http://caracol.com.co';", true);
0

精彩评论

暂无评论...
验证码 换一张
取 消