为什么弹出窗口不返回值?
程序是这样的,一窗口有病人编号,病历编号等,点击选择弹出一个窗口,从中选择一个病历,通过js将这个病历的病人编号与病历编号返回给上一个窗口,为什么,返回去的是空值呢?
第一个窗口
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultAdd.aspx.cs" Inherits="ExamResult_ResultAdd" %>
<!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>
<script language="javascript" type="text/javascript">
function newpage(htmlurl,nh,nw) {
window.open(htmlurl,"newWin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top=20,left=200,width="+nw+",height="+nh);
}
</script>
<body>
<form id="form1" runat="server">
<div>
<table border=0 cellpadding=0 cellspacing=0 style="width: 773px">
<tr>
<td style="width: 100px; height: 24px;">
<asp:Label ID="Label1" runat="server" Text="病历摘要编号" Font-Size="10pt"></asp:Label></td>
<td style="width: 45px; height: 24px;">
<asp:TextBox ID="TxtDocID" runat="server"></asp:TextBox></td>
<td style="width: 45px; height: 24px;">
<span style="color: #0000ff; text-decoration: underline"><a href="#" onclick="return newpage('../Pub/SelectDoc.aspx',500,800);">选择</a></span></td>
<td style="width: 100px; height: 24px;">
<asp:Label ID="Label2" runat="server" Text="病人编号" Font-Size="10pt"></asp:Label></td>
<td style="width: 69px; height: 24px;">
<asp:TextBox ID="TxtPID" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="Label12" runat="server" Font-Size="10pt" Text="病人姓名"></asp:Label></td>
<td style="width: 45px">
<asp:TextBox ID="TxtPName" runat="server"></asp:TextBox></td>
<td style="width: 45px">
</td>
<td style="width: 100px">
<asp:Label ID="Label11" runat="server" Font-Size="10pt" Text="病人权限"></asp:Label></td>
<td style="width: 69px">
<asp:CheckBox ID="CheckBox1" runat="server" Width="119px" /></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="Label3" runat="server" Text="检查日期" Font-Size="10pt"></asp:Label></td>
<td style="width: 45px">
<input id="Text5" type="text" /> </td>
<td style="width: 45px">
</td>
<td style="width: 100px">
<asp:Label ID="Label4" runat="server" Text="检查项目" Font-Size="10pt"></asp:Label></td>
<td style="width: 69px">
<input id="Text3" type="text" /></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="Label5" runat="server" Text="检查报告" Font-Size="10pt"></asp:Label></td>
<td style="width: 45px">
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
<td style="width: 45px">
</td>
<td style="width: 100px">
<asp:Label ID="Label6" runat="server" Text="检查类型" Font-Size="10pt"></asp:Label></td>
<td style="width: 69px"><input id="Text4" type="text" /></td>
</tr>
<tr>
<td style="width: 100px; height: 22px;">
<asp:Label ID="Label7" runat="server" Text="检查结果" Font-Size="10pt"></asp:Label></td>
<td style="width: 45px; height: 22px;">
<asp:DropDownList ID="DropDownList3" runat="server" Width="156px" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
<asp:ListItem>正常</asp:ListItem>
<asp:ListItem>不正常</asp:ListItem>
</asp:DropDownList></td>
<td style="width: 45px; height: 22px">
</td>
<td style="width: 100px; height: 22px;">
</td>
<td style="width: 69px; height: 22px;">
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="Label8" runat="server" Text="文档内容" Font-Size="10pt"></asp:Label></td>
<td colspan="4">
<asp:TextBox ID="TextBox2" runat="server" Height="135px" TextMode="MultiLine" Width="533px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px; height: 136px">
<asp:Label ID="Label9" runat="server" Text="医生建议" Font-Size="10pt"></asp:Label></td>
<td colspan="4" style="height: 136px">
<asp:TextBox ID="TextBox3" runat="server" Height="144px" TextMode="MultiLine" Width="534px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="Label10" runat="server" Text="短信内容" Font-Size="10pt"></asp:Label></td>
<td colspan="4">
<asp:TextBox ID="TextBox4" runat="server" Height="142px" TextMode="MultiLine" Width="534px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td colspan="4">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Button" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
[解决办法]
用 showModalDialog 或者 showModelessDialog
===讲解=====
五、showModalDialog和showModelessDialog数据传递技巧。
(作者语:本来想用一问一答形式来写的,但是我想不出这个怎么问,所以只好这样了。)
这个东西比较麻烦,我改了好几次了不是没办法说明白(语文水平越来越差了),只好用个例子说明了。
例子:
现在需要在一个showModalDialog(或showModelessDialog)里读取或设置一个变量var_name
一般的传递方式:
window.showModalDialog("filename.htm",var_name)
//传递var_name变量
在showModalDialog(或showModelessDialog)读取和设置时:
alert(window.dialogArguments)//读取var_name变量
window.dialogArguments="oyiboy"//设置var_name变量
这种方式是可以满足的,但是当你想在操作var_name同时再操作第二个变理var_id时呢?就无法再进行操作了。这就是这种传递方式的局限性。
以下是我建议使用的传递方式:
window.showModalDialog("filename.htm",window)
//不管要操作什么变量,只直传递主窗口的window对象
在showModalDialog(或showModelessDialog)读取和设置时:
alert(window.dialogArguments.var_name)//读取var_name变量
window.dialogArguments.var_name="oyiboy"//设置var_name变量
同时我也可以操作var_id变量
alert(window.dialogArguments.var_id)//读取var_id变量
window.dialogArguments.var_id="001"//设置var_id变量
同样还可以对主窗口的任何对象进行操作,如form对象里的元素。
window.dialogArguments.form1.index1.value="这是在设置index1元素的值"
六、多个showModelessDialog的相互操作。
因为光说很费劲,我就偷点懒,直接用代码来说了,如果不明白的话那就直接来信(oyiboy#163.net(使用时请将#改成@))问我吧。
以下代码的主要作用是在一个showModelessDialog里移动别一个showModelessDialog的位置。
主文件的部份js代码。
var s1=showModelessDialog('控制.htm',window,"dialogTop:1px;dialogLeft:1px") //打开控制窗口
var s2=showModelessDialog('about:blank',window,"dialogTop:200px;dialogLeft:300px") //打开被控制窗口
======例子=========
function vv()
{
kv=window.showModalDialog("test2.aspx",window);
/* 1
var newoption=document.createElement("Option")
newoption.text=kv;
document.getElementById("ee").options.add(newoption);
*/
/*2*/
var option=new Option(kv,kv);
document.getElementById("ee").options.add(option);
}
function cc()
{
/*一种方案:子窗体直接给父窗体赋值,事件全部在子窗体中处理*/
window.dialogArguments.document.Form1.Text1.value=document.getElementById('s2').value;
/* 正确赋值*/
var newoption=window.dialogArguments.document.createElement("Option")
newoption.text=document.getElementById('s2').value;
window.dialogArguments.document.getElementById("ss").options.add(newoption);
/*第二种方案:子窗体将值传回父窗体,由父窗体处理*/
window.returnValue= document.getElementById('s2').value;
window.close();
}
====参考==========
http://www.cnblogs.com/Csharp-net/archive/2008/04/01/1132823.html
http://www.cnblogs.com/hjs1573/archive/2008/03/25/1121292.html