ASP.NET ONPAGECLOSE事件

发布于 2025-02-12 02:55:30 字数 1591 浏览 1 评论 0原文

我试图找到一种从按钮打开页面的方法,如果它的 已经打开。

到目前为止,我拥有的是2页。 默认页面通过一个按钮打开选项页面,例如: -

        protected void btnCreateNewWindow_Click(object sender, EventArgs e)
    {
        if (SharedMethods.Is_Page_Displayed == 0)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "Order", "<script type='text/javascript'>window.open('Option1.aspx', '', 'fullscreen = yes')</script>");
            SharedMethods.Is_Page_Displayed = 1;
        }
    }

共享会话代码: -

namespace ns_SharedMethods
{
public class SharedMethods
{

    #region Session
    public static String sIs_Page_Displayed = "Is_Page_Displayed";
    #endregion Session

    public static int Is_Page_Displayed
    {
        get
        {
            if (System.Web.HttpContext.Current.Session[sIs_Page_Displayed] != null)
            {
                return (Convert.ToInt32(System.Web.HttpContext.Current.Session[sIs_Page_Displayed]));
            }
            else
            {
                return (0);
            }
        }
        set
        {
            System.Web.HttpContext.Current.Session[sIs_Page_Displayed] = value;
        }

    }

    public static String Is_The_Page_Displayed()
    {
        if (Is_Page_Displayed == 1)
        {
            return ("YES");
        }
        else
        {
            return ("NO");
        }
   }
}

}

我尝试使用的是将会话变量用作标志来表示该页面已打开/关闭。

是否可以在第二页中触发OnClosePage事件来设置会话变量“ IS_PAGE_DISPLAYED = 0”?

任何例子都将不胜感激。

TIA

PS我试图发布所有页面.aspx源代码,但是格式正在扭曲:(

I'm trying to find a way of opening a page from a button if its NOT already open.

What I have so far is 2 pages.
Default page opens Option page via a button like:-

        protected void btnCreateNewWindow_Click(object sender, EventArgs e)
    {
        if (SharedMethods.Is_Page_Displayed == 0)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "Order", "<script type='text/javascript'>window.open('Option1.aspx', '', 'fullscreen = yes')</script>");
            SharedMethods.Is_Page_Displayed = 1;
        }
    }

Shared Session code:-

namespace ns_SharedMethods
{
public class SharedMethods
{

    #region Session
    public static String sIs_Page_Displayed = "Is_Page_Displayed";
    #endregion Session

    public static int Is_Page_Displayed
    {
        get
        {
            if (System.Web.HttpContext.Current.Session[sIs_Page_Displayed] != null)
            {
                return (Convert.ToInt32(System.Web.HttpContext.Current.Session[sIs_Page_Displayed]));
            }
            else
            {
                return (0);
            }
        }
        set
        {
            System.Web.HttpContext.Current.Session[sIs_Page_Displayed] = value;
        }

    }

    public static String Is_The_Page_Displayed()
    {
        if (Is_Page_Displayed == 1)
        {
            return ("YES");
        }
        else
        {
            return ("NO");
        }
   }
}

}

I've tried is using a Session variable as a flag to denote the page is open/closed.

Is it possible to trigger an OnClosePage event in the second Page to set a Session variable "Is_Page_Displayed = 0"?

Any examples is appreciated.

tia

PS I've tried to publish all pages .aspx source code but the formatting was being distorted :(

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

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

发布评论

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

评论(1

似最初 2025-02-19 02:55:30

基于使用的初始代码,添加: -

在“ option1.aspx”上
将其添加到源: -

    <script>
    // Window Event to trigger when the page has closed
    window.onbeforeunload = function (event)
    {
        // C# method called from the Java Script
        PageMethods.SetWebPageClosedSession();           
    }         
</script>

在“&lt; body&gt;&lt; form id =“ form1” runat =“ server”&gt;“”下。

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

将其添加到Option1.aspx.cs: -

    [System.Web.Services.WebMethod]
    public static void SetWebPageClosedSession()
    {
        // Method called from the .aspx Script function WebPageClosed()
        SharedMethods.Is_Page_Displayed = 0;
    }

因此,我所做的就是使用window.onbeforeunload事件,在Option1.aspx中,调用C#方法来设置会话变量。 default.aspx页面在尝试打开option1.aspx之前检查

Base on the initial code used, add:-

On "Option1.aspx"
Add this to the source:-

    <script>
    // Window Event to trigger when the page has closed
    window.onbeforeunload = function (event)
    {
        // C# method called from the Java Script
        PageMethods.SetWebPageClosedSession();           
    }         
</script>

And this under the "< body > < form id="form1" runat="server" >"

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

Add this to the Option1.aspx.cs:-

    [System.Web.Services.WebMethod]
    public static void SetWebPageClosedSession()
    {
        // Method called from the .aspx Script function WebPageClosed()
        SharedMethods.Is_Page_Displayed = 0;
    }

So what I've done is use the window.onbeforeunload event, in Option1.aspx, to call a C# method to set a Session variable. The Default.aspx page checks prior to trying to open Option1.aspx

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