在aspx 前台中的 js 函数
<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="关闭且返回值" />
在aspx.CS 后台中,为 Button1 加上 Button1_Click protected void Button1_Click(object sender, EventArgs e)
{
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), Guid.NewGuid().ToString(), "winClose();", true); }以上两个按钮,一个是htmlinput 一个是 服务器端 button ,均可以实现关闭且返回值,成功调用前台的 js winClose函数。
但是当我把前台的 js 函数变成这样
<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>
既把变量s 值由 100 变成了 var s = document.getElementById("<%=txbChild_sendToFather.ClientID%>").value;
以后,则 Button1_Click 就执行不了 这段 js 函数了,就是提示 js 有错,但是 <input type ="button" value="关闭且返回值" onclick="winClose()" />
这个 htmlinput 确可以正常执行这段 js函数
这是怎么回事呢,请大家帮忙 !!万分感激
<!--
var k = window.dialogArguments;
//获得父窗口传递来的值 //关闭窗口,同时把 某一个 textbox 值返回给父窗口
function winClose() {
var s = document.getElementById("<%=txbChild_sendToFather.ClientID%>").value;
//var s = 100;
window.returnValue = s;
window.close();
}
//-->
</script>
我参照网上的方法自己先写的:protected void Button1_Click(object sender, EventArgs e)
{
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), Guid.NewGuid().ToString(), "winClose();", true); }可是却是实现不了返回值, 提示 js 有错如果用 htmlinput 调用上面的 js 函数就一切功能都正常<input type ="button" value="关闭且返回值" onclick="winClose()" />这样的话,就可以调用 js 。
谁能给解释 一下,为什么在 后台的 aspx.CS 中,就调用不了前台的那段 js 函数呢
如果把var s = document.getElementById("<%=txbChild_sendToFather.ClientID%>").value;改成 一个静态值 var s =100;这样的话,就一切正常了!! 真是弄不懂了,恳请各位大侠出手相救,万分感谢!!
<!--
var k = window.dialogArguments;
//获得父窗口传递来的值 //关闭窗口,同时把 某一个 textbox 值返回给父窗口
function winClose() {
var s = document.getElementById("<%=txbChild_sendToFather.ClientID%>").value;
//var s = 100;
window.returnValue = s;
window.close();
}
//-->
</script>
我参照网上的方法自己先写的:protected void Button1_Click(object sender, EventArgs e)
{
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), Guid.NewGuid().ToString(), "winClose();", true); }可是却是实现不了返回值, 提示 js 有错如果用 htmlinput 调用上面的 js 函数就一切功能都正常<input type ="button" value="关闭且返回值" onclick="winClose()" />这样的话,就可以调用 js 。
谁能给解释 一下,为什么在 后台的 aspx.CS 中,就调用不了前台的那段 js 函数呢
如果把var s = document.getElementById("<%=txbChild_sendToFather.ClientID%>").value;改成 一个静态值 var s =100;这样的话,就一切正常了!! 真是弄不懂了,恳请各位大侠出手相救,万分感谢!!
window.parent.returnValue = s;
window.parent.close();另外,txbChild_sendToFather控件必须是当前页面的控件
<%@ 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>
由上面的父窗 Father.aspx 用 showModalDialog 方法弹出模态窗口 MainIframe.aspxMainIframe.aspx 代码
<%@ 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); }
}
}以上是完整全部页面代码。
还请老大出手相救,万分感激!![/code]
[/code]
应该也改成
var k = window.parent.dialogArguments;
改成
var k = window.parent.dialogArguments;并且也var k = window.showModalDialog("MainIframe.aspx?" + (new Date()).valueOf(), window, "dialogWidth:600px;status:no;dialogHeight:500px")禁止缓存,但是问题依然存在,就是 htmlinput 按钮可以正常关闭子窗并给父窗赋值但是就是这个Button1 在服务器端 .cs 中实现不了这个功能,还是报同样的错"当前页脚本发生错误,缺少对象"
按你的意思,也改成了
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), Guid.NewGuid().ToString(), "retrunValue();", true);但是还是报同样的错我就很奇怪,不管怎么样改,htmlinput 这个按钮,哪次都一切正常,就是这个 button1 在 cs 后台里实现是真麻烦啊,就是不成功!
你可以试试这样子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()));
}
折磨我3天3夜的难题被 你解决了,我真是太高兴了!
虽然你只有一个小绿三角,但是你的能量还是相当大的!万分感谢!!万分感激 !同时感谢楼上所有热心回复我的各位大侠们,感谢你们都在各自的百忙中热心回答我的问题,尤其是net_lover (【孟子E章】) ,更是百问不厌的态度,真的感谢你们!!
你可以把你的这个页面放到iframe中,然后
window.parent.returnValue = s;
window.parent.close();
”
--------我最早发的贴子,只需按照 hjsmliang2008 的方法做就可以
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()));
}
我分析原因 ,就是因为在 cs 后台中 ,直接调用前台的 js 函数,但是前台的 js 函数取 服务器端控件的方法,在 cs 中没有认可,导致 的像 hjsmliang2008 的方法,就是在cs 后台中把 js 重新写一下,然后,在声明这个服务器端控件TextBox
就解决了。