C#中window.showModalDialog的应用(传参与接收值)

 1.使用方法

vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures])

参数说明:
sURL              --   必选参数,类型:字符串。用来指定对话框要显示的文档的URL。
vArguments   --   可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。
sFeatures        --   可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。

1.   dialogHeight:   对话框高度,不小于100px
2.   dialogWidth:   对话框宽度。
3.   dialogLeft:    离屏幕左的距离。
4.   dialogTop:    离屏幕上的距离。
5.   center:         { yes | no | 1 | 0 } :             是否居中,默认yes,但仍可以指定高度和宽度。
6.   help:            {yes | no | 1 | 0 }:               是否显示帮助按钮,默认yes。
7.   resizable:      {yes | no | 1 | 0 } [IE5+]:    是否可被改变大小。默认no。
8.   status:         {yes | no | 1 | 0 } [IE5+]:     是否显示状态栏。默认为yes[ Modeless]或no[Modal]。
9.   scroll:           { yes | no | 1 | 0 | on | off }:是否显示滚动条。默认为yes。

参数传递:
(1) 要想对话框传递参数,是通过vArguments来进行传递的。类型不限制,对于字符串类型,最大为4096个字符。也可以传递对象,例如:
parent.htm

<script> var obj = new Object(); obj.name="51js"; window.showModalDialog("modal.htm",obj,"dialogWidth=200px;dialogHeight=100px"); </script> modal.htm <script> var obj = window.dialogArguments alert("您传递的参数为:" + obj.name) </script>

-------------------------------
(2) 可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象。例如:
parent.htm

<script>

         str 
=window.showModalDialog("modal.htm",,"dialogWidth=200px;dialogHeight=100px"); alert(str); </script> modal.htm <script>

         window.returnValue
="http://homepage.yesky.com"; </script>

 2.C#实例

前台JS:

function openwindow(url, iWidth, parameter, iHeight)
        {
            var iTop = (window.screen.availHeight - 30 - iHeight) / 2; //获得窗口的垂直位置;
            var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; //获得窗口的水平位置;
            var ret = window.showModalDialog(url, parameter, 'dialogHeight:' + iHeight + 'px;dialogWidth:' + iWidth + 'px;dialogTop:' + iTop + 'px;dialogLeft:' + iLeft + 'px;status:no;scroll:no;');
            if (ret == undefined)
            {
                return;
            }
            if (ret.DANWMCH != undefined)
            {
                document.getElementById("danwmch").value = ret.DANWMCH;
            }

            if (ret.DANWBM != undefined)
            {

                 document.getElementById("ctl00_ContentPlaceHolder1_chumrid").value = ret.DANWBM;

            }

         }

前台html“

<a href="#" onclick="javascript:openwindow('DanWSelect.aspx?operate=maishr‘,535,400);" style="color: blue; cursor: hand;">选择</a>

跳转DanWSelect.aspx页面

前台JS:

function
setPValue(danWMCh, danWID) { var Select = { 'DANWMCH': danWMCh, //交易商单位名称 'DANWBM': danWID, //交易商单位编码

            };
            window.returnValue 
= Select; window.opener = null; window.open('', '_self'
);
            window.close();
        }

后台cs:

protected void TPCSDataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string jiaoYShBM = e.Item.Cells[0].Text;
        string jiaoYShMC = e.Item.Cells[1].Text;
        string operate = Request.QueryString["operate"].ToString();

        if (e.CommandName == "Select")  //选择赋值
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ss", "setPValue('" + jiaoYShMC + "','" + jiaoYShBM + "');", true);
        }
    }
为易维护、易扩展、易复用、灵活多样而努力~~
原文地址:https://www.cnblogs.com/SpringDays/p/3129836.html