【ASP.net技巧】asp.net弹出窗口 返回值

本文介绍两种ASPX页面间的数据交互方法:一种利用showModalDialog与returnValue属性实现模态窗口向主窗口传递数据;另一种展示如何使用window.showModalDialog及window.returnValue完成相同功能。

刚刚 看到这么一个问题,这里也做个标记:http://topic.youkuaiyun.com/u/20080411/14/7b0f9da5-0413-4149-91e9-72c3df3018a3.html?seed=327251592

 

 

第一种方式:

 

 

//在Visual Studio 2008中调试通过


testPop_Page.aspx:主页面ASPX代码

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   
    <script type="text/javascript" >
        function Pop()
        {
            var result = showModalDialog('testPop_downs.aspx', 'subpage', 'dialogWidth:800px;dialogHeight:600px;center:yes;help:no;resizable:yes;status:no'); //打开模态子窗体,并获取返回值
        document.getElementById("txt_id").value=result.split("'")[0]; //返回值分别赋值给相关文本框
        document.getElementById("txt_name").value=result.split("'")[1];
        document.getElementById("txt_pwd").value=result.split("'")[2];
        }
    </script>


</head>
<body>
    <form id="form1" runat="server">
    <div>
 
            <asp:TextBox ID="txt_id" runat="server" ></asp:TextBox>
            <asp:TextBox ID="txt_name" runat="server" ></asp:TextBox>
            <asp:TextBox ID="txt_pwd" runat="server" ></asp:TextBox>
            <br />

            <asp:Button ID="btnPop" runat="server" Text="PoPWindows"  OnClientClick ="Pop()"
                 />

    </div>
    </form>
</body>
</html>

//后台无须CS代码

 

 

testPop_downs.aspx: 弹出页面ASPX代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   
    <script type="text/javascript" >
        function cc(infor_id,infor_name,infor_psw) //参数分别为id,name和password
        {
            window.returnValue= infor_id+"'"+infor_name+"'"+infor_psw; //返回值
            window.close();
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvshow" runat="server" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="3"  onrowdatabound="gvshow_RowDataBound"
>
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>

    </div>
    </form>
</body>
</html>

 

 

 

testPop_downs.cs:弹出页面后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class testPop_downs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            {
                SetBind();
            }
    }


    public void SetBind()
    {

 

//以下读取数据库表,绑定至页面显示的Griview的代码请自己根据实际情况进行修改


        string ConnString = ConfigurationManager.ConnectionStrings["SidriTenNewsConnectionString"].ToString();
        using (SqlConnection conn = new SqlConnection(ConnString))
        {
            conn.Open();
            string sql = "SELECT [RealName],[UserCode],[UserPWD] FROM [SidriUser]";
            SqlDataAdapter ada = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            gvshow.DataSource = ds.Tables[0];
            this.gvshow.DataBind();
        }

//读取数据库并绑定Griview显示代码结束


    }
    protected void gvshow_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "cc('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[1].Text + "','" + e.Row.Cells[2].Text + "')");
        }

    }

 


}

 

 

//DreamExpress 2010年3月19日修改测试成功!

 

 

 

第二种方式:

  

 

 

 

returnValue是javascript中html的window对象的属性,目的是返回窗口值,当用
window.showModalDialog函数打开一个IE的模式窗口(模式窗口知道吧,就是打开后不能操作父窗口,只能等模式窗口关闭时才能操作)时,用于返回窗口的值,下面举个例子:

------------------------------------------------------------------------------
//father.html
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language="javascript">

function showmodal(){
var ret = window.showModalDialog("child.htm",null,"dialogWidth:350px;dialogHeight:350px;help:no;status:no");
if (ret){alert('子窗口返回真!');
}else{
alert('子窗口返回假!');
}

}

</script>
</HEAD>
<BODY>
<INPUT id=button1 type=button value=Button name=button1 onclick="showmodal();">

</BODY>
</HTML>

------------------------------------------------------------------------------
//child.html
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language="javascript">
function trans(tag){

   if (tag==0){
     window.returnValue=false;
   } else{
     window.returnValue =true;
   }
   window.close();

}


</script>
</HEAD>
<BODY>

<INPUT id=button1 type=button value="返回真" name=button1 onclick="trans(1)">
<INPUT id=button2 type=button value="返回假" name=button2 onclick="trans(0)">

</BODY>
</HTML>
-----------------------------------------------------------------------------
这样一来可以实现从模式窗口向父窗口传递值的作用,
这个returnValue除了可以是布尔值,整型值等以外还可以是个js数组,用来传递大量数据。
具体showModalDialog等的用法,可以参考msdn。

 


注意下面的有opener的都只能是用在window.open()这种情况而不能是上面.的showModel...等形式否则的话.会报undetife错误....


也可以这样子的改变父窗口中的值. 下面的这个..可以动态改变父窗口中多个值.而不是简单的把弹出窗口中的一个选中以后.马上就传回去给父窗口.


opener.document.getElementById('txt_Phone').value = Number;
        opener.document.getElementById('hdn_ID').value = ID;
        opener.document.getElementById('hdn_Phone').value = Number;
        window.close();

 

加上这句.我们还可以.刷新父窗口
window.opener.location.href=window.opener.location.href
window.opener.location.reload()

如果还要调用父窗口中的方法.也可以用下面的这种..如下
     opener.函数名(xxx,xxx)  
不过函数内变量的作用域仍为父窗体.
这样子我们.就可以直接调用这个函数..如果这个函数是异步请求的那就更爽了..
也就是说我们.在子窗口中可以向服务器发送请求..关闭子窗口后..我们父窗口又立即向服务器发送异步请求.又窗口双请求.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值