Page_Init 上的警报消息

发布于 2024-11-26 09:09:58 字数 867 浏览 1 评论 0原文

我有一个由 PageBase 类(所有页面都继承自)调用的安全类,如果用户尝试,该安全类会将用户重定向到主页或登录页面(取决于他们是否登录)并访问他们无权查看的页面。我想通过这样的话来提醒用户:

“您无权查看此页面,正在重定向您”

或类似的内容。我知道您可以从代码隐藏中调用 javascript 来向用户显示警报。

在我的 PageBase 类(同样,所有页面都将从该类继承)中,有一个通用的 Page_Init(..) 方法,当我尝试从那里调用我的 js 警报时,没有任何反应。我在代码中放置了断点,并且代码被命中,但用户没有收到警报。我认为这是因为 Page_Init 在页面生命周期中太早,无法执行 js,这就是导致此问题的原因。

有没有办法解决这个问题,而不必向每个单独的页面添加功能?另外,这是我尝试过的两种方法:

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);

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

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 saying 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>");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じее 2024-12-03 09:09:58

问题不在于 Page_Init 上没有任何页面。问题是服务器没有向浏览器发送任何内容,因为在 页面生命周期。 (更多内容即将推出)

正常情况下,即使您调用 Response.Write,响应数据也不会在 Rendering 事件之前发送到浏览器。通常可以通过调用 Response.Flush()< 来强制执行此操作/a> 紧接在 Response.Write 之后,但这通常会产生意想不到的后果。在 Page_Init 周期中,我认为调用 Response.Flush() 可能会导致很多问题。

您可能会更好地简单地创建一个带有“我们正在重定向”消息的静态 html 页面,并使用 javascrip.SetTimeout 方法进行倒计时,并在几秒钟后使用 javascript 进行重定向。这就是多年来网络上的做法。当用户未经授权时,您的基类可以简单地重定向到此页面。

“重定向”页面的代码。

<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> 

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> 
我家小可爱 2024-12-03 09:09:58

您需要在基页上实现 Page_Load 事件的逻辑,如下所示:

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文