window.close()无法正常工作 [英] window.close() is not working

查看:818
本文介绍了window.close()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的弹出窗口中有一个按钮.单击该按钮时,它会处理某些方法并检查该过程是否成功完成.如果是这样,它将在消息框中通知用户.此meesagebox是一个JavaScript代码,alert()方法.在仍然处于按钮单击过程中之后,我还有另一个javascript代码正在刷新主页,并且我试图关闭弹出窗口.

我的代码是:

I have button in my popup. When the button is clicked it processes some methods and checks if the process is successfully completed. If so, on a messagebox it notifies the user. This meesagebox is a javascript code, alert() method. After that still inside button click process I have another javascript code which is refreshing the main page and I am trying to close the popup window.

My code is:

protected void btnMyButton_Click(object sender, EventArgs e)
{
  MessageBox("Parts are successfully grouped!");
  BindGridView(ddlParts.SelectedValue);
  string scriptString = "<script language='JavaScript'>window.opener.document.forms(0).submit();window.close();</script>";                    
  if (!Page.ClientScript.IsClientScriptBlockRegistered(scriptString))
  {
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", scriptString);
  }                      
}


private void MessageBox(string msg)
{
  Page.Controls.Add(new LiteralControl("<script language='javascript'> window.alert('" + msg.Replace("'", "\\'") + "')</script>"));
}



因此,当我评论消息框"行时,主页将被刷新,弹出窗口被关闭.但是,如果我不对消息框行进行注释,则会出现该消息框,刷新主页面,但不会关闭弹出窗口.

有没有办法显示消息框并关闭弹出窗口?谢谢大家



So when I comment the Messagebox line the main page is getting refreshed and popup gets closed. But if i keep the messagebox line uncommented the message box appears, the main page gets refreshed but the popup window will not close.

Is there a way to show the messagebox, and close the popup window? thank you all

推荐答案


如果可以的话请尝试...

Hi,
try this if could help...

MessageBox.Show(this, "Parts are successfully grouped!");





public class MessageBox
   {
       private static Hashtable m_executingPages = new Hashtable();
       private MessageBox() { }
       public static void Show(string sMessage)
       {
           // If this is the first time a page has called this method then
           if (!m_executingPages.Contains(HttpContext.Current.Handler))
           {
               // Attempt to cast HttpHandler as a Page.
               Page executingPage = HttpContext.Current.Handler as Page;
               if (executingPage != null)
               {
                   // Create a Queue to hold one or more messages.
                   Queue messageQueue = new Queue();
                   // Add our message to the Queue
                   messageQueue.Enqueue(sMessage);
                   // Add our message queue to the hash table. Use our page reference
                   // (IHttpHandler) as the key.
                   m_executingPages.Add(HttpContext.Current.Handler, messageQueue);
                   // Wire up Unload event so that we can inject
                   // some JavaScript for the alerts.
                   executingPage.Unload += new EventHandler(ExecutingPage_Unload);
               }
           }
           else
           {
               // If were here then the method has allready been
               // called from the executing Page.
               // We have allready created a message queue and stored a
               // reference to it in our hastable.
               Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];
               // Add our message to the Queue
               queue.Enqueue(sMessage);
           }
       }
       public static void Show(System.Web.UI.Page page, string msg)
       {
           ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "clientScript", "javascript:alert('" + msg + "');", true);
       }

       // Our page has finished rendering so lets output the
       // JavaScript to produce the alert's
       private static void ExecutingPage_Unload(object sender, EventArgs e)
       {
           // Get our message queue from the hashtable
           Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];
           if (queue != null)
           {
               StringBuilder sb = new StringBuilder();
               // How many messages have been registered?
               int iMsgCount = queue.Count;
               // Use StringBuilder to build up our client slide JavaScript.
               sb.Append("<script language='javascript'>");
               // Loop round registered messages
               string sMsg;
               while (iMsgCount-- > 0)
               {
                   sMsg = (string)queue.Dequeue();
                   sMsg = sMsg.Replace("\n", "\\n");
                   sMsg = sMsg.Replace("\"", "'");
                   sb.Append(@"alert( """ + sMsg + @""" );");
               }
               // Close our JS
               sb.Append(@"</script>");
               // Were done, so remove our page reference from the hashtable
               m_executingPages.Remove(HttpContext.Current.Handler);
               // Write the JavaScript to the end of the response stream.
               HttpContext.Current.Response.Write(sb.ToString());
           }
       }
   }



请别忘了投票,如果有帮助的话,以便其他人可以看到并认为这是答案...



Please do not forget to vote if could help, so that others may see and consider as an answer...

Regards,


检查您的浏览器是否启用了javascript
Check where javascript is enabled on your browser or not


这篇关于window.close()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆