zoukankan      html  css  js  c++  java
  • [转]使用showModalDialog打开模态窗口添加数据后刷新原窗口

    经常有页面列出一个数据列表,点击添加、修改的时候呢,就弹出一个窗口,在弹出的窗口里进行数据管理,然后呢,返回,返回后,原来的窗口里的数据列表要重新刷新

    这里说的就是这个东西

    要解决的问题三个:

    1、是使用window.open还是使用window.showModalDialog?

    2、如果使用showModalDialog,怎么提交数据到服务器?

    3、刷新原窗口

    解决问题:

    1、使用模态窗口在任务栏不会有新的窗口出现,他是属于原窗口的,在操作模态窗口的时候,原窗口不可以被操作,嗯,相当于在pb里用打开一个response!的窗口,所以选window.showModalDialog是最好的

    2、使用window.showModalDialog的时候,提交数据时有点怪,问题在这里:http://www.tiantiansoft.com/bbs/dispbbs.asp?boardID=40&ID=144093,就是没办法提交数据回服务器

    我不知道showModalDialog的运行机制,在http://www.tiantiansoft.com/bbs/dispbbs.asp?BoardID=40&id=94373我查到过这一段,这一段是哪里的,我不知道了,在dhtml.chm里,有这一段的英文版,我先记下来

    Javascript有许多内建的方法来产生对话框,如:window.alert(), window.confirm(),window.prompt().等。 然而IE提供更多的方法支持对话框。如:

      showModalDialog() (IE 4+ 支持)
      showModelessDialog() (IE 5+ 支持)


    window.showModalDialog()方法用来创建一个显示HTML内容的模态对话框,由于是对话框,因此它并没有一般用window.open()打开的窗口的所有属性。
    window.showModelessDialog()方法用来创建一个显示HTML内容的非模态对话框。

    当我们用showModelessDialog()打开窗口时,不必用window.close()去关闭它,当以非模态方式[IE5]打开时, 打开对话框的窗口仍可以进行其他的操作,即对话框不总是最上面的焦点,当打开它的窗口URL改变时,它自动关闭。而模态[IE4]方式的对话框始终有焦点(焦点不可移走,直到它关闭)。模态对话框和打开它的窗口相联系,因此我们打开另外的窗口时,他们的链接关系依然保存,并且隐藏在活动窗口的下面。

    使用方法如下:
    vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures])
    vReturnValue = window.showModelessDialog(sURL [, vArguments] [, sFeatures])
    参数说明:
    sURL
    必选参数,类型:字符串。用来指定对话框要显示的文档的URL。
    vArguments
    可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。
    sFeatures
    可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。
    dialogHeight 对话框高度,不小于100px,IE4中dialogHeight 和 dialogWidth 默认的单位是em,而IE5中是px,为方便其见,在定义modal方式的对话框时,用px做单位。
      dialogWidth: 对话框宽度。
      dialogLeft: 距离桌面左的距离。
      dialogTop: 离桌面上的距离。
      center: {yes | no | 1 | 0 }:窗口是否居中,默认yes,但仍可以指定高度和宽度。
      help: {yes | no | 1 | 0 }:是否显示帮助按钮,默认yes。
      resizable: {yes | no | 1 | 0 } [IE5+]:是否可被改变大小。默认no。
      status: {yes | no | 1 | 0 } [IE5+]:是否显示状态栏。默认为yes[ Modeless]或no[Modal]。
    scroll:{ yes | no | 1 | 0 | on | off }:指明对话框是否显示滚动条。默认为yes。

    还有几个属性是用在HTA中的,在一般的网页中一般不使用。
    dialogHide:{ yes | no | 1 | 0 | on | off }:在打印或者打印预览时对话框是否隐藏。默认为no。
    edge:{ sunken | raised }:指明对话框的边框样式。默认为raised。
    unadorned:{ yes | no | 1 | 0 | on | off }:默认为no。

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

    test1.htm
    ====================
    <script>
    var mxh1 = new Array("mxh","net_lover","孟子E章")
    var mxh2 = window.open("about:blank","window_mxh")
    // 向对话框传递数组
    window.showModalDialog("test2.htm",mxh1)
    // 向对话框传递window对象
    window.showModalDialog("test3.htm",mxh2)
    </script>

    test2.htm
    ====================
    <script>
    var a = window.dialogArguments
    alert("您传递的参数为:" + a)
    </script>

    test3.htm
    ====================
    <script>
    var a = window.dialogArguments
    alert("您传递的参数为window对象,名称:" + a.name)
    </script>

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

    test4.htm
    ===================
    <script>
    var a = window.showModalDialog("test5.htm")
    for(i=0;i<a.length;i++) alert(a[i])
    </script>

    test5.htm
    ===================
    <script>
    function sendTo()
    {
    var a=new Array("a","b")
    window.returnValue = a
    window.close()
    }
    </script>
    <body>
    <form>
    <input value="返回" type=button onclick="sendTo()">
    </form>

    下面是我解决使用showModalDialog没办法提交数据回服务器的办法:

    ①给模态窗口加上窗口名:

    <script language="JavaScript">
            window.name="MyModalDialog";
        </script>

    ②设置模态窗口提交数据回服务器的时候,提交回本窗口

    <form id="form1" runat="server" target="MyModalDialog">

    两步,就可以解决这个问题了

    3、处理完数据返回时,如果要返回数据到原窗口,那就使用上面我引回来的一大段里的例子,如果不用返回数据到原窗口,就直接刷新原窗口,使用js:window.location.reload();

    我是这样打开窗口,刷新数据的:

    Literal1.Text = "<p>尚无分类,请<a href=\"#\" onclick=\"window.showModalDialog('ClassAdd.aspx?classid=0&down=0&add=1');window.location.reload();\">添加</a>";

    我给出我的两个页面的完整测试代码,在测试中,我测试了直接放在页面上的DropDownList和放在FormView里的DropDownList:

    dropdownlist.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dropdownlist.aspx.cs" Inherits="test_dropdownlist" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>使用模态窗口返回数据,刷新DropDownList</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
        
    <script type="text/javascript">
            function openwin()
            
    {
                var win 
    = showModalDialog("dropdownlist2.aspx""""dialogWidth:350px; dialogHeight:300px; status:0; help:0");
             
    if (win != null)
             
    {
              alert(win);
              var controlID, control;
              controlID
    ="<% =this.hfAddListItem.ClientID %>";
              control
    =document.getElementById(controlID);
              control.value
    =win;
              form1.submit();
             }

         }

        
    </script>
            
    <input id="Button1" type="button" value="打开模态窗口" onclick="openwin();" />
            
    <asp:Label ID="Label1" runat="server"></asp:Label>
            
    <br />
            数据:
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
                ErrorMessage
    ="请输入数据"></asp:RequiredFieldValidator><br />
            服务器端列表:
    <asp:DropDownList ID="DropDownList1" runat="server">
                
    <asp:ListItem>第一项</asp:ListItem>
            
    </asp:DropDownList>
            
    <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="刷新" /><br />
            
    <asp:Button ID="Button2" runat="server" Text="提交数据到服务器" OnClick="Button2_Click" />
            
    <asp:HiddenField ID="hfAddListItem" runat="server" />
            
    <asp:SqlDataSource ID="sdsAdd" runat="server" ConnectionString="<%$ ConnectionStrings:TTOAConnectionString %>"
                InsertCommand
    ="insert into t_table (bb) values (@bb)" SelectCommand="select tid,bb from t_table where tid=0">
                
    <InsertParameters>
                    
    <asp:Parameter Name="bb" />
                
    </InsertParameters>
            
    </asp:SqlDataSource>
            
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="tid" DataSourceID="sdsAdd"
                DefaultMode
    ="Insert">
                
    <InsertItemTemplate>
                    数据:
    <asp:TextBox ID="bbTextBox" runat="server" Text='<%# Bind("bb") %>'></asp:TextBox>
                    
    <br />
                    列表:
    <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
                    
    <br />
                    
    <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="插入"></asp:Button>
                
    </InsertItemTemplate>
            
    </asp:FormView>
        
    </div>
        
    </form>
    </body>
    </html>

    dropdownlist.aspx.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class test_dropdownlist : System.Web.UI.Page
    {
        
    protected void Page_Load(object sender, EventArgs e)
        
    {
            
    if (IsPostBack)
            
    {
                
    string tvStr;
                tvStr 
    = hfAddListItem.Value;
                
    if (tvStr.Length > 0)
                
    {
                    
    string[] tv;
                    
    string[] text;
                    
    string[] value;
                    tv 
    = tvStr.Split('*');
                    text 
    = tv[0].Split('|');
                    value 
    = tv[1].Split('|');
                    
    int i, n;
                    n 
    = text.Length;
                    
    for (i = 0; i < n; i++)
                    
    {
                        DropDownList1.Items.Add(
    new ListItem(text[i], value[i]));
                    }

                    
    //刷新FormView里的DropDownList
                    DropDownList ddl = new DropDownList();
                    ddl 
    = (DropDownList)FormView1.FindControl("DropDownList2");
                    
    if (ddl != null)
                    
    {
                        
    for (i = 0; i < n; i++)
                        
    {
                            ddl.Items.Add(
    new ListItem(text[i], value[i]));
                        }

                    }

                    
    //把hidden控件置空
                    hfAddListItem.Value = "";
                }

            }

        }

        
    protected void Button2_Click(object sender, EventArgs e)
        
    {
            
    //提交数据
            string s;
            s 
    = TextBox1.Text;
            s 
    += "<br>共有列表:" + DropDownList1.Items.Count.ToString();
            Label1.Text
    =s;
        }

        
    protected void Button3_Click(object sender, EventArgs e)
        
    {
            
    string tvStr;
            tvStr 
    = TextBox1.Text;
            
    if (tvStr.Length > 0)
            
    {
                
    string[] tv;
                
    string[] text;
                
    string[] value;
                tv 
    = tvStr.Split('*');
                text 
    = tv[0].Split('|');
                value 
    = tv[1].Split('|');
                
    int i, n;
                n 
    = text.Length;
                
    for (i = 0; i < n; i++)
                
    {
                    DropDownList1.Items.Add(
    new ListItem(text[i], value[i]));
                }

            }

        }

    }


    dropdownlist2.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dropdownlist2.aspx.cs" Inherits="test_dropdownlist2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>dropdownlist的模态窗口</title>
        
    <script language="JavaScript">
            window.name
    ="MyModalDialog";
        
    </script> 
    </head>
    <body>
        
    <form id="form1" runat="server" target="MyModalDialog">
        
    <div>
            
    &nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加" />
            
    <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="确认返回" />
            
    <input id="Button5" type="button" value="关闭" onclick="if(confirm('确认放弃吗?')) window.close();" /><br />
            
    <asp:ListBox ID="ListBox1" runat="server" Rows="10" Width="300px"></asp:ListBox>
            
    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="删除选择" />
            
    <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="全部删除" /></div>
        
    </form>
    </body>
    </html>

    dropdownlist2.aspx.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class test_dropdownlist2 : System.Web.UI.Page
    {
        
    protected void Page_Load(object sender, EventArgs e)
        
    {

        }

        
    protected void Button1_Click(object sender, EventArgs e)
        
    {
            
    //添加
            string s;
            s 
    = TextBox1.Text;
            ListBox1.Items.Add(
    new ListItem(s, s));
        }

        
    protected void Button2_Click(object sender, EventArgs e)
        
    {
            
    //删除选择
            ListItem li = new ListItem();
            li 
    = ListBox1.SelectedItem;
            
    if (li != null)
            
    {
                ListBox1.Items.Remove(li);
            }

        }

        
    protected void Button3_Click(object sender, EventArgs e)
        
    {
            
    //删除全部
            ListBox1.Items.Clear();
        }

        
    protected void Button4_Click(object sender, EventArgs e)
        
    {
            
    //确认返回
            
    //window.returnValue = ""; window.close();
            string text = "", value = "";
            
    foreach (ListItem li in ListBox1.Items)
            
    {
                text 
    += li.Text + "|";
                value 
    += li.Value + "|";
            }

            
    if (text.Length > 0)
            
    {
                text 
    = text.Remove(text.Length - 1);
                value 
    = value.Remove(value.Length - 1);
                text 
    = text + "*" + value;
            }

            Response.Write(
    "<script type='text/javascript'>window.returnValue = '" + text + "'; window.close();</script>");
        }

    }


  • 相关阅读:
    jmeter的beanshell脚本编写
    基于 Flask 的简易 Mock 平台
    mock-server
    linux环境jmeter- java环境安装配置
    服务器监控环境搭建 telegraf+influxdb+grafana框架
    聚类算法:凝聚层次聚类
    python中cursor操作数据库
    python中if __name__ == '__main__': 的解析
    python连接mysql数据库
    Python3.5,flask在命令行执行 [python] view plain copy import pymysql 连接mysql数据库MySQLdb
  • 原文地址:https://www.cnblogs.com/wubiyu/p/1081703.html
Copyright © 2011-2022 走看看