【问题标题】:How to close the parent window from its child?如何从子窗口关闭父窗口?
【发布时间】:2023-03-07 08:18:01
【问题描述】:

我有以下情况:

我的页面上有一个网格视图:

page1.aspx

我在rad window 中通过该gridview 打开另一个页面(page2.aspx),然后通过page2.aspx 上的某个按钮,我也在rad window 中打开最后一页(page3.aspx)。

所有这些步骤都是通过服务器端代码执行的:


 protected void OpenNewWindow(string url, int width, int height, int mode)
        {
            RadWindow newWindow = new RadWindow();
            newWindow.NavigateUrl = url;
            newWindow.VisibleOnPageLoad = true;
            newWindow.KeepInScreenBounds = true;
            newWindow.Skin = "Metro";
            if (width > 0)
            {
                newWindow.Width = width;


            }
            if (height > 0)
            {
                newWindow.Height = height;
            }
            newWindow.VisibleStatusbar = false;
            if (mode == 0)
            {
                {

                }
                //newWindow.OnClientClose = "OnChildWindowClosed";
                newWindow.DestroyOnClose = true;
                newWindow.InitialBehaviors = WindowBehaviors.Maximize;
            }
            RadWindowManager1.Windows.Add(newWindow);
        }

我想做的是:

当点击我 (page3.aspx) 上的特定按钮时,关闭它及其父级 page2.aspx

如何做到这一点(服务器端)?

我试试这个:但它只是关闭了子 page3.aspx 我也想关闭父 page2.aspx 吗?!


  protected void Button1_Click(object sender, EventArgs e)
        {
            ((RadAjaxManager)this.Parent.FindControl("RadAjaxManager1")).ResponseScripts.Add("CloseModal();");

            RadAjaxManager1.ResponseScripts.Add("CloseModal();");
        }

【问题讨论】:

  • 一旦 page2 和 page3 是单独的页面,您就不能轻松地从第 3 页关闭第 2 页。
  • page2.aspx 是父 pf page3.aspx 吗?
  • 是的page2.aspx 有一个按钮,当我点击它时会打开page3.aspx
  • 为什么你不想使用客户端?
  • 因为我在关闭孩子之前先做了一些服务器端代码

标签: c# asp.net ajax telerik radwindow


【解决方案1】:

您可以通过实现每个父级的接口来执行气泡事件。我试图说明我的想法。

public interface IClosingParent
{
    void Close();
}

public class PageA : System.Web.Page, IClosingParent
{
    public void IClosingParent.Close()
    {
        //Code to close it or hide it
    }
}

public class PageB : System.Web.Page, ClosingParent
{
    public void IClosingParent.Close()
    {
        ((IClosingParent)this.Parent).Close();

        //Code to close it or hide it
    }
}

public class PageC : System.Web.Page
{        
    protected void ButtonClose_Click(object sender, EventArgs e)
    {
        ((IClosingParent)this.Parent).Close();

        //Code to close it or hide it     
    }
}

这包括您必须将 RadWindow 声明为父级。所以在一个看起来像这样的分层视图中:

PageA : IClosingParent | |-- PageB : IClosingParent | |-- 页面C |-- 按钮关闭

我认为您可以使用此解决方案按您的意愿管理关闭流程,并随时或随心所欲地进行更改。

【讨论】:

    【解决方案2】:
    protected void Button1_Click(object sender, EventArgs e)
    {
        ((RadAjaxManager)this.Parent.FindControl("RadAjaxManager1")).ResponseScripts.Add("CloseModal();");
    
        RadAjaxManager1.ResponseScripts.Add("CloseModal();");
    }
    

    如果CloseModal() 事件正确触发,您应该可以在其中调用window.close()。 RadWindows 仅存在于父窗口的上下文中,因此您仍然应该能够调用 window.close(),这将关闭每个打开的窗口。

    编辑:虽然有点离题,Telerik 的this link 向您展示了如何从 RadWindow 获取父窗口的句柄。然后你可以调用客户端的close方法关闭浏览器窗口(page1.aspx),这将关闭所有后续的RadWindows。

    来自this link

    RadWindow 和浏览器弹出窗口之间的主要区别在于,就像任何其他 DHTML 元素一样,RadWindow 仅存在于创建它的页面的上下文中。它不能离开浏览器窗口的边界。

    【讨论】:

      【解决方案3】:

      这可能听起来很土,但是为什么不简单地在关闭之前执行你想要运行的服务器代码,然后返回给客户端一个 bool 参数,允许它运行一个关闭窗口的 if 语句(客户端) ?

      PS : 我在考虑算法,我对服务器端 C# 没有太多经验。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        • 2017-02-08
        • 2012-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多