警报框问题

发布于 2024-08-23 21:45:31 字数 600 浏览 4 评论 0原文

如何首先显示警报框,然后

if (machineID.Count != 0)
            {

                checkMachineGrpState(machineID);
        else
                {
                    Response.Write("<script>alert('You are being logged out')</script>");

                   GoSignOut();
                }
    }

 private void GoSignout()
    {
        FormsAuthentication.SignOut();
        Session.Abandon();
        Response.Redirect("~/Default.aspx");
    }

在此处注销如果我不调用 GosignOut 函数,我可以看到警报框,但当我调用注销时,它不会显示警报框并简单地退出

所以我在想是否有我可以先看到警报框,然后当我按“确定”时,它应该在我的代码后面调用注销函数...谢谢

how to show the alertbox first and then log out

if (machineID.Count != 0)
            {

                checkMachineGrpState(machineID);
        else
                {
                    Response.Write("<script>alert('You are being logged out')</script>");

                   GoSignOut();
                }
    }

 private void GoSignout()
    {
        FormsAuthentication.SignOut();
        Session.Abandon();
        Response.Redirect("~/Default.aspx");
    }

here if i dont call the GosignOut function i can see the alert box but the moment i put call the signout it does not show me the alertbox and simply signs out

So i was thinking if there is a way i can see the alertbox first and then when i press OK it should call signout function in my code behind... thanks

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

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

发布评论

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

评论(2

⊕婉儿 2024-08-30 21:45:31

使用调用此注销代码的服务器端按钮的 clientclick,然后返回 true

,例如

<asp:Button id="button1" runat="server" OnClientClick="javascript:alert('You are being logged out');return true;" />

编辑重新思考你想要什么。试试这个,

if (machineID.Count != 0)
            {

                checkMachineGrpState(machineID);
        else
                {
                   GoSignOut();
                  Page.ClientScript.RegisterStartupScript(this.GetType(), "Redirect", "<script>alert('You are being logged out');window.location.href='" + ResolveUrl("~/Default.aspx") + "';</script>");
                }
    }

 private void GoSignout()
    {
        FormsAuthentication.SignOut();
        Session.Abandon();
    }

如果您需要允许重定向到登录页面以外的页面(这是由于SignOut)您可以将如下内容添加到您的 web.config 中。我不是 100% 确定您想要允许这样做,因为它将不允许经过身份验证的用户访问该页面

<location path="whateverpage.aspx">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>

use the clientclick of the server side button that calls this logout code, then return true

e.g.

<asp:Button id="button1" runat="server" OnClientClick="javascript:alert('You are being logged out');return true;" />

edit re thought what you want. try this, I have not tested with the use of session.abandon and the Signout (which may force you to the login page)

if (machineID.Count != 0)
            {

                checkMachineGrpState(machineID);
        else
                {
                   GoSignOut();
                  Page.ClientScript.RegisterStartupScript(this.GetType(), "Redirect", "<script>alert('You are being logged out');window.location.href='" + ResolveUrl("~/Default.aspx") + "';</script>");
                }
    }

 private void GoSignout()
    {
        FormsAuthentication.SignOut();
        Session.Abandon();
    }

if you need to allow the redirect to go to a page other than the login page (which is forced due to the SignOut) you can add something like the below to you web.config. I am not 100% sure you want to allow this as it would allow none authenticated users to access the page

<location path="whateverpage.aspx">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
另类 2024-08-30 21:45:31

Response.write 会将内容写入浏览器,当浏览器加载页面时,它会解析该数据(渲染 html 并执行 js)。如果浏览器不通过 GET 或 POST(表单提交、链接、ajax)联系服务器,则不会向服务器(您的 asp.net webforms 代码运行的位置)提供进一步的通信或反馈。

发生的情况是您将输出定向到客户端,但随后立即调用 Response.Redirect,因此客户端永远不会获取您的输出。相反,它只是立即重定向,然后显示 default.aspx 页面。

您可以做的仍然是进行重定向,但将一些数据传递给客户端(查询字符串将是执行此操作的一种方法),然后 default.aspx 页面将知道查找该数据。如果找到该数据,它将完成任何必要的操作。就您而言,您希望它提醒用户。

一种方法是重定向到“~/Default.aspx?alert=1”。在 default.aspx 中,在页面加载时,它将查看查询字符串并查看警报键是否存在且值为 1。如果是这样,它将执行您的警报。

Response.write will write stuff out to the browser and when the browser loads the page, it parse that data (render html and execute js). No further communication or feedback is presented to the server (which is where your asp.net webforms code runs) without the browser contacting the server via a GET or POST (form submission, link, ajax).

Whats happening is you are directing output to the client but then immediately calling Response.Redirect, so the client never gets your output. Instead, it is just redirecting immediately and then displays the default.aspx page.

What you could do is still do the redirect but pass some data to the client (the querystring would be a way to do this) and then the default.aspx page would know to look for that data. If that data is found, it would complete any action necessary. In your case, you want it to alert the user.

One way of doing this is by redirecting to "~/Default.aspx?alert=1". In default.aspx, on pageload, it would look to the querystring and see if the alert key is there with the value of 1. If that was the case, it'd perform your alert.

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