62,244
社区成员




<script type="text/javascript" language="javascript">
<!--
var k = window.dialogArguments;
//获得父窗口传递来的值
//关闭窗口返回是否刷新的参数.
function winClose() {
//var s = document.getElementById("<%=txbChild_sendToFather.ClientID%>").value;
//我先把上面为变量 s 定义的代码注释掉,下面只为 s 赋了一个静态数字 100
var s = 100;
window.returnValue = s;
window.close();
}
//-->
</script>
<input type ="button" value="关闭且返回值" onclick="winClose()" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="关闭且返回值" />
protected void Button1_Click(object sender, EventArgs e)
{
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), Guid.NewGuid().ToString(), "winClose();", true);
}
<script type="text/javascript" language="javascript">
<!--
var k = window.dialogArguments;
//获得父窗口传递来的值
//关闭窗口返回是否刷新的参数.
function winClose() {
//var s = 100;
//我把 var s 定义由静态数字 100 改为 取得 textbox 值
var s = document.getElementById("<%=txbChild_sendToFather.ClientID%>").value;
window.returnValue = s;
window.close();
}
//-->
</script>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Father.aspx.cs" Inherits="WebUI.Temp.Father" %>
<!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>我是父窗口 Father.aspx ,我用 showModalDialog 方法弹出子窗口 Child.aspx</title>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Expires" content="no-cache"/>
<script type="text/javascript" language="javascript">
<!--
function openChild() {
var k = window.showModalDialog("MainIframe.aspx", window, "dialogWidth:600px;status:no;dialogHeight:500px");
if (k != null)
document.getElementById("txbFather").value = k;
}
//-->
</script>
</head>
<body>
<form id="form1" runat="server">
<table border="1" cellpadding="0" cellspacing="0" width="600">
<tr bgcolor="#ccccff">
<td>
1、传递到父窗口的值:<br />
<br />
2、并能取得子窗口传递进来的值</td>
<td>
<asp:TextBox ID="txbFather" runat="server" Width="200px">我是父窗的初始值</asp:TextBox>
</td>
</tr>
<tr bgcolor="#66cc66">
<td height="40">
<input type ="button" value="弹出子窗口" onclick="openChild()" />
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainIframe.aspx.cs" Inherits="WebUI.Temp.MainIframe" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<iframe src="Child.aspx" width="100%" height="100%" frameborder="0" ></iframe>
</form>
</body>
</html>
上面这个 iframe 中嵌套了子窗 Child.aspx ,所有功能都在这个 Child.aspx 页面中.
Child.aspx 页面
[code=HTML]
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Child.aspx.cs" Inherits="WebUI.Temp.Child" %>
<!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>我是子窗口 Child.aspx ,用showModalDialog弹出的子窗口</title>
<base target="_self"/>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Expires" content="no-cache"/>
</head>
<body>
<form id="form1" runat="server">
<table border="1" cellpadding="0" cellspacing="0" width="600">
<tr bgcolor="#ff66cc">
<td height="50">
<input type ="button" value="一、用传统的 input 关闭且返回值" onclick="retrunValue()" />
</td>
<td>
取得父窗口传递进来的值
<asp:TextBox ID="txbChild" runat="server"></asp:TextBox>
</td>
</tr>
<tr bgcolor="#3399ff">
<td height="50">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="二、用服务器控件 Button1 关闭且返回值" />
</td>
<td>
可以改变上面 TextBox 的值,返回给父窗口
</td>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript" language="javascript">
<!--
var k = window.dialogArguments;
//当前页中 txbChild 获得父窗口 txbFather 传递来的值
if (k != null) {
document.getElementById("txbChild").value = k.document.getElementById("txbFather").value;
}
//将当前页 txbChild 中的值 返回到父窗口中,并关闭当前页
function retrunValue() {
var s = document.getElementById("txbChild").value;
window.parent.returnValue = s;
window.parent.close();
}
//-->
</script>
以上完整功能,当子窗Child.aspx中 用传统的 htmlinput 关闭窗口并返回值以父窗中,一切正常。
就是当我用 服务器控件 Button1 就实现不了关闭窗口并返回值以父窗中,报错,脚本出错,缺少对象。
Child.aspx.CS 后台我是这样调用前台的 js 脚末的
Child.aspx.CS 代码 :
[code=C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebUI.Temp
{
public partial class Child : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), Guid.NewGuid().ToString(), "retrunValue();", true);
}
}
}
以上是完整全部页面代码。
还请老大出手相救,万分感激!!
protected void Button1_Click(object sender, EventArgs e)
{
System.Text.StringBuilder cstext2 = new System.Text.StringBuilder();
cstext2.Append(string.Format("<script type=text/javascript>var s ='{0}'; ",txbChild_sendToFather.Text.ToString()));
cstext2.Append("window.returnValue = s;window.close();</");
cstext2.Append("script>");
Response.Write(string.Format("{0}",cstext2.ToString()));
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Text.StringBuilder cstext2 = new System.Text.StringBuilder();
cstext2.Append(string.Format("<script type=text/javascript>var s ='{0}'; ",txbChild_sendToFather.Text.ToString()));
cstext2.Append("window.returnValue = s;window.close();</");
cstext2.Append("script>");
Response.Write(string.Format("{0}",cstext2.ToString()));
}
<script type="text/javascript" language="javascript">
<!--
var k = window.dialogArguments;
//获得父窗口传递来的值
//关闭窗口,同时把 某一个 textbox 值返回给父窗口
function winClose() {
var s = document.getElementById("<%=txbChild_sendToFather.ClientID%>").value;
//var s = 100;
window.returnValue = s;
window.close();
}
//-->
</script>
<script type="text/javascript" language="javascript">
<!--
var k = window.dialogArguments;
//获得父窗口传递来的值
//关闭窗口,同时把 某一个 textbox 值返回给父窗口
function winClose() {
var s = document.getElementById("<%=txbChild_sendToFather.ClientID%>").value;
//var s = 100;
window.returnValue = s;
window.close();
}
//-->
</script>