请叫如何实现弹出一个窗口,其父窗口变灰色(父窗口不可编辑)? 
这种效果在www.microsoft.com上面就有应用,但是不知道如何实现 
请指导下 
最好用.NET(C#)实现,客户端用javascript

解决方案 »

  1.   


    var w1 = window.open();
    w1.focus();
    w1.attachEvent('onblur', function() {w1.focus();});
      

  2.   

    /// <summary>
    /// Msg 的摘要说明
    /// </summary>
    public class Message
    {
        #region 私有变量
        /// <summary>
        /// 提示信息
        /// </summary>
        string _msg = string.Empty;
        /// <summary>
        /// 新增记录的id
        /// </summary>
        string _recordid = string.Empty;
        /// <summary>
        /// 通用执行的js函数
        /// </summary>
        string _commondjs = string.Empty;
        /// <summary>
        /// 记录关键字
        /// </summary>
        string _keyword = "CurId";
        /// <summary>
        /// 自定义按钮集
        /// </summary>    EnumMsgType iMsgType = EnumMsgType.NewEditExit;
        
        Array _clearField = null;
        NameValueCollection _buttoncollection = new NameValueCollection();
        #endregion    #region 构造函数
        public Message()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="msg">信息</param>
        public Message(string msg)
        {
            this._msg = msg;
        }
        public Message(NameValueCollection btcollection)
        {
            this._buttoncollection = btcollection;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <param name="coll">自定义按钮集合</param>
        public Message(string msg, NameValueCollection btcollection)
        {
            this._msg = msg;
            this._buttoncollection = btcollection;
        }    #endregion    #region 属性    /// <summary>
        /// 提示信息按钮的默认的几种类型
        /// No  自定义按钮
        /// NewEditExit 新增 编辑 退出
        /// OkOnly 只有确定按钮
        /// </summary>
        public EnumMsgType MsgType
        {
            get { return iMsgType; }
            set { iMsgType = value; }
        }
        string sMsgTitle = Strings.GetString("MessageTitle");
        /// <summary>
        /// 提示信息的标题
        /// </summary>
        public string MsgTitle
        {
            get { return sMsgTitle.ToLower(); }
            set { sMsgTitle = value; }
        }
        private EnumTipPicType _tipPic;
        /// <summary>
        /// 提示信息显示的图片
        /// </summary>
        public EnumTipPicType TipPic
        {
            get { return _tipPic; }
            set { _tipPic = value; }
        }
     
        /// <summary>
        /// 新增记录的ID
        /// 默认为 CurId
        /// </summary>
        public string Identity
        {
            set
            {
                this._recordid = value;
                if (string.IsNullOrEmpty(_recordid))
                    throw new Exception("请填写新增记录的ID");
            }
        }
        /// <summary>
        /// 获取或设置提示信息内容
        /// </summary>
        public string Msg
        {
            get { return _msg; }
            set { _msg = value; }
        }
        /// <summary>
        /// 记录关键字
        /// </summary>
        public string KeyWord
        {
            get { return _keyword.ToLower(); }
            set { _keyword = value; }
        }
        /// <summary>
        /// 需要清除的字段集合
        /// </summary>
        public Array ClearField
        {
            get { return _clearField; }
            set {_clearField = value;}
        }
        /// <summary>
        /// 通用的执行脚本对象,在增加或修改
        /// 成功后需要操作的对象
        /// </summary>
        public string CommondJS
        {
            set { this._commondjs = value; }
        } 
        TargetType _target = TargetType.self;
        /// <summary>
        /// 获取或者设置信息提示的显示Target
        /// </summary>
        public TargetType Target
        {
            get { return _target; }
            set { _target = value; }
        }
     
        private bool _autoClientShow = true;
        /// <summary>
        /// 设置或获取 在信息显示设置为客户端调用的时候
        /// 是否自动触发信息还是手工触发信息,默认是自动的
        /// </summary>
        public bool AutoClientShow
        {
            get { return _autoClientShow; }
            set { _autoClientShow = value; }
        }
        /// <summary>
        /// 提示按钮集合
        /// </summary>
        public NameValueCollection ButtonCollection
        {
            get { return _buttoncollection; }
            set { _buttoncollection = value; }
        }
        #endregion    #region 调用方法
        /// <summary>
        /// 客户端调用显示的Message的方式
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public string GetHtml()
        {
            //如果设置图片为警告或者失败时候,只有确定按钮
            if (this.TipPic.Equals(EnumTipPicType.Falsed) || this.TipPic.Equals(EnumTipPicType.Warn))
                this.MsgType = EnumMsgType.OKOnly;        string str ="<script>"+ BuildClientObject();
            if (this._autoClientShow)
            {
                if (string.IsNullOrEmpty(this._commondjs))
                    str += ";" + this._target.ToString() + ".MessageShow(MessageCollection,\"" + this._target.ToString() + "\");</script>";
                else
                    str += ";" + this._commondjs + ";" + this._target.ToString() + ".MessageShow(MessageCollection,\"" + this._target.ToString() + "\");</script>";
            }
            else
               if (string.IsNullOrEmpty(this._commondjs))
                    str += ";" +"</script>";
                else
                    str += ";" + this._commondjs + ";" + "</script>";
            return str;
        }
      

  3.   

      /// <summary>
        /// 处理Url
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string GetNewUrl(string type)
        {
            string str = string.Empty;
            string oldurl = System.Web.HttpContext.Current.Request.Url.ToString().ToLower() ; 
            NameValueCollection urlcollection = System.Web.HttpContext.Current.Request.QueryString;
            oldurl = oldurl.ToLower();
            string currid = urlcollection[this.KeyWord] == null ? "0" : urlcollection[this.KeyWord].Trim();
            switch (type)
            {
                case "add":
                    if (currid.Equals("0"))
                        str = oldurl;
                    else
                        str = oldurl.Replace(this.KeyWord.ToLower()+"=" + currid.ToLower(), this.KeyWord.ToLower()+"=0");
                    if (null != _clearField && _clearField.Length > 0)
                    {
                        //新增替换字符
                        string v = "";
                        foreach (string s in _clearField)
                        {
                            v = System.Web.HttpContext.Current.Request.Params[s];
                            if (!string.IsNullOrEmpty(v))
                                str = oldurl.Replace(s.ToLower() + "=" + v, s.ToLower() + "=");
                        }
                    }
                    break;
                case "modify":
                    if (currid.Equals("0"))
                        str = oldurl.Replace(this.KeyWord.ToLower() + "=0", this.KeyWord.ToLower() + "=" + this._recordid);
                    else
                        str = oldurl;
                    break;
            }
            return str;
        }
        #endregion    #region  创建客户端使用的object对象    /// <summary>
        /// 创建客户端使用的object对象
        /// </summary>
        /// <returns></returns>
        private string BuildClientObject()
        {
            
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("var MessageCollection = {{Title:\"{0}\",Tip:\"{1}\",Pic:\"{2}\",Buttons:[",this.sMsgTitle.ToString(),this._msg.ToString(),this.TipPic.ToString());
            switch(iMsgType)
            {
                default:
                case EnumMsgType.NewEditExit:
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"window.location.href = '{1}'\"}},", Strings.GetString("Add"), GetNewUrl("add"));
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"window.location.href = '{1}'\"}},", Strings.GetString("Modify"), GetNewUrl("modify"));
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"try{{closeFlag=true;}}catch(e){{}};top.close()\"}}", Strings.GetString("Exit"));
                    break;
                case EnumMsgType.OKOnly:
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"\"}}", Strings.GetString("ok"));
                    break;
                case EnumMsgType.OKOnlyClose:
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"try{{closeFlag=true;}}catch(e){{}};top.close()\"}}", Strings.GetString("ok"));
                    break;
                case EnumMsgType.No:
                    for (int i = 0; i < this._buttoncollection.Count; i++)
                    {
                        sb.AppendFormat("{{Name:\"{0}\",Event:\"{1}\"}},", this._buttoncollection.GetKey(i), this._buttoncollection.GetValues(i)[0]);
                    }                //移出多于的一个,号
                    if(sb.ToString().EndsWith(","))
                    sb.Remove(sb.Length - 1, 1);
                    break;
                case EnumMsgType.NewExit:
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"window.location.href = '{1}'\"}},", Strings.GetString("Add"), GetNewUrl("add"));
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"try{{closeFlag=true;}}catch(e){{}};top.close()\"}}", Strings.GetString("Exit"));
                    break;
                case EnumMsgType.Exit:
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"try{{closeFlag=true;}}catch(e){{}};top.close()\"}}", Strings.GetString("Exit"));
                    break;
                case EnumMsgType.EditExit:
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"window.location.href = '{1}'\"}},", Strings.GetString("Modify"), GetNewUrl("modify"));
                    sb.AppendFormat("{{Name:\"{0}\",Event:\"try{{closeFlag=true;}}catch(e){{}};top.close()\"}}", Strings.GetString("Exit"));
                    break;        }
      
            sb.Append("]}");
            return sb.ToString();

        }
        /// <summary>
        /// 格式化提示信息
        /// 把单引号替换成双引号
        /// </summary>
        /// <param name="msg">需要格式化的信息</param>
        /// <returns></returns>
        public static string FormatMessage(string msg)
        {
            return msg.Replace("'", "\"");
        }   
       
        #endregion    #region 枚举类型
        /// <summary>
        /// 提示信息类型
        /// </summary>
        public enum EnumMsgType
        {
            /// <summary>
            /// 自定义
            /// </summary>
            No,
            /// <summary>
            /// 新增,编辑,退出
            /// </summary>
            NewEditExit,
            /// <summary>
            /// 确定
            /// </summary>
            OKOnly,        /// <summary>
            /// OKOnly + 关闭窗口
            /// </summary>
            OKOnlyClose,
            /// <summary>
            /// 新增,退出
            /// </summary>
            NewExit,
            /// <summary>
            /// 修改,退出
            /// </summary>
            EditExit,
            /// <summary>
            /// 退出
            /// </summary>
            Exit    }
        /// <summary>
        /// 提示信息图片类型
        /// </summary>
        public enum EnumTipPicType
        {
            /// <summary>
            /// 成功
            /// </summary>
            Successful,
            /// <summary>
            /// 失败 如果设置图片为警告或者失败时候,只有确定按钮
            /// </summary>
            Falsed,
            /// <summary>
            /// 警告 如果设置图片为警告或者失败时候,只有确定按钮
            /// </summary>
            Warn,
            /// <summary>
            /// 提醒
            /// </summary>
            Remind,
            /// <summary>
            /// 等待
            /// </summary>
            Wait
            
            
        }
        /// <summary>
        /// 信息显示位置
        /// 只对客户端调用有效
        /// </summary>
        public enum TargetType
        {
            /// <summary>
            /// 默认,如果AutoClientShow=false,则客户端调用时,不能设置为self
            /// </summary>
            self,
            /// <summary>
            /// 父页面调用
            /// </summary>
            parent,
            /// <summary>
            /// 关联页面的父页面
            /// </summary>
            opener
        }
        #endregion}
      

  4.   

    function MessageShow(MessageCollection,callsrc)
    {       
          if(callsrc!=null && callsrc.toLowerCase()=="self")
            {
            var oldonload = window.onload;
            window.onload = function()
            {
              if(typeof oldonload=="function") oldonload();
               if(typeof MessageCollection == "object")
                 BuildMessage(MessageCollection);
             }
           }else
           {
             BuildMessage(MessageCollection);
           }
           
     }
     function BuildMessage(MessageCollection)
     {
          var strHtml = "<iframe id=\"MessageIframe\" class=\"TipIframe\" frameborder=\"0\"></iframe>";
    strHtml += "<div id=\"MessageDiv\" align=\"center\" class=\"TipDIV\">";
    strHtml += "<table width=\"100%\" height=\"100%\" id=\"tb_msg\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse:collapse;\"><tr><td colspan=2 class=\"TipTitle\" >"+MessageCollection.Title+"</td></tr>";
    strHtml +="<tr valign=\"middle\"><td style='width:100px;BORDER-RIGHT-STYLE: none;' class=\"TipPic"+MessageCollection.Pic+"\"></td><td align=\"left\" style=\"BORDER-LEFT-STYLE: none;Padding:2px;\"><div id='TipDivChild' class='TipDivChild'>"+MessageCollection.Tip+"</div></td></tr>";
            
    strHtml +="<tr><td colspan=2 style=\"background-color: #f7f7f7;\" height=\"30\" align=\"center\" >";
                for(var i=0;i<MessageCollection.Buttons.length;i++)
    {
                    strHtml+="<input style=\"border-width:1px;padding : 3px;Font-Family:tahoma;Font-Size:8pt;height:20px  \" type=\"button\" value='"+MessageCollection.Buttons[i].Name+"' onclick=\";MessageClose();"+ MessageCollection.Buttons[i].Event+"\"/>&nbsp;&nbsp;&nbsp;";
    }
    strHtml +="</td></tr></table></div>";
    document.body.insertAdjacentHTML("beforeEnd",strHtml);
    if(document.all.TipDivChild.clientHeight>100)
       document.all.TipDivChild.style.height="100px";
    window.focus();
     }
      

  5.   


    <script>
    function openWin() {
        var w1 = window.open();
        w1.focus();
        w1.attachEvent('onblur', function() {w1.focus();});
    }</script><input type="button" value="Open a window." onclick="openWin();">
    建议还是使用虚拟的弹出窗口的好,因为有很多人使用弹出窗口过滤,这样脚本无法创建对象,就会报错了。
    虚拟弹出窗口的方法你可以到蓝色或者无忧脚本里面找找。