在aspx.CS 中执行 javascript 函数的问题

tiger8000 2012-02-08 03:44:28

在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函数


这是怎么回事呢,请大家帮忙 !!万分感激
...全文
580 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
看看页面的html是怎么样的,就知道哪里出错了。我怀疑可能是对<%=txbChild_sendToFather.ClientID%>的替换发生在RegisterStartupScript之后,因此没有替换成功
tiger8000 2012-02-09
  • 打赏
  • 举报
回复
试过了,还是不行

提示:

当前脚本发生错误,缺少对象
  • 打赏
  • 举报
回复
RegisterClientScriptBlock改成ScriptManager.RegisterStartupScript试试
tiger8000 2012-02-09
  • 打赏
  • 举报
回复
神啊,救救我吧
tiger8000 2012-02-09
  • 打赏
  • 举报
回复
十分感谢 net_lover 老大出手相助! 惭愧,我接触了 .net 多年,可是技术上还是长进不大!

net_lover 老大,在 论坛上也帮助了我很多年,再次向老大表示感谢!!

--

老大,我按你的指引,把页面嵌入了一个 iframe 内,可是一切都做好后,传统的 htmlinput 取值一切正常,一点问题都没有,就是用 服务器控件 Button1 ,就不能正常关闭子窗,并返值。我把完整页面发上来,还请老大百忙中,再帮一下!! 感谢!!

首先是 Father.aspx


<%@ 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.aspx

MainIframe.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]
tiger8000 2012-02-09
  • 打赏
  • 举报
回复
总结:

我试过了

不用这样做


你可以把你的这个页面放到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
就解决了。




tiger8000 2012-02-09
  • 打赏
  • 举报
回复
稍后,我在结贴,我把整个功能,再整理一下,把问题在总结一下发上为,也给以后有此问题的同学做个参考!
tiger8000 2012-02-09
  • 打赏
  • 举报
回复
hjsmliang2008

你真厉害,居然通过了,一切正常了!
折磨我3天3夜的难题被 你解决了,我真是太高兴了!
虽然你只有一个小绿三角,但是你的能量还是相当大的!

万分感谢!!万分感激 !

同时感谢楼上所有热心回复我的各位大侠们,感谢你们都在各自的百忙中热心回答我的问题,尤其是

net_lover (【孟子E章】) ,更是百问不厌的态度,真的感谢你们!!



hjsmliang2008 2012-02-09
  • 打赏
  • 举报
回复

你可以试试这样子

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()));
}
tiger8000 2012-02-09
  • 打赏
  • 举报
回复
老大,我也这样改了

this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), Guid.NewGuid().ToString(), "retrunValue();", true);

按你的意思,也改成了


this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), Guid.NewGuid().ToString(), "retrunValue();", true);

但是还是报同样的错

我就很奇怪,不管怎么样改,htmlinput 这个按钮,哪次都一切正常,就是这个 button1 在 cs 后台里实现是真麻烦啊,就是不成功!
tiger8000 2012-02-09
  • 打赏
  • 举报
回复
谢谢老大回复

不过,我刚试过了,

var k = window.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 中实现不了这个功能,还是报同样的错

"当前页脚本发生错误,缺少对象"


孟子E章 2012-02-09
  • 打赏
  • 举报
回复
另外,模式窗口缓存很很厉害,
var k = window.showModalDialog("MainIframe.aspx?" + (new Date()).valueOf(), window, "dialogWidth:600px;status:no;dialogHeight:500px")

禁止缓存

另外,采用RegisterStartupScript,应该就没问题了。
注意服务器控件的id
k.document.getElementById("txbFather").value;
确保你的父窗口的txbFather这个id存在
孟子E章 2012-02-09
  • 打赏
  • 举报
回复
var k = window.dialogArguments;
应该也改成
var k = window.parent.dialogArguments;
tiger8000 2012-02-09
  • 打赏
  • 举报
回复
恩,我也有点感觉是这方面的原因,但是我实在不知道应该再怎么做了
孟子E章 2012-02-08
  • 打赏
  • 举报
回复
你可以把你的这个页面放到iframe中,然后
window.parent.returnValue = s;
window.parent.close();

另外,txbChild_sendToFather控件必须是当前页面的控件
tiger8000 2012-02-08
  • 打赏
  • 举报
回复


我把问题再总结一下就是如何在 aspx.cs 后台中执行前台的一段 js 函数。

前台的 js 函数如下:



<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>


我参照网上的方法自己先写的:

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;

这样的话,就一切正常了!! 真是弄不懂了,恳请各位大侠出手相救,万分感谢!!


tiger8000 2012-02-08
  • 打赏
  • 举报
回复


我把问题再总结一下就是如何在 aspx.cs 后台中执行前台的一段 js 函数。

前台的 js 函数如下:



<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>


我参照网上的方法自己先写的:

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;

这样的话,就一切正常了!! 真是弄不懂了,恳请各位大侠出手相救,万分感谢!!


62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧