最近做了一个子窗体给父窗体赋值的问题。做的很成功,虽然功能过于简单,心理有点喜悦
接下来看下例子:我有两个窗体,一个MainForm 父窗体 一个ChirdleFrom 子窗体
这个是父窗体的代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainForm.aspx.cs" Inherits="TestWeB.窗体赋值.MainForm" %>
<!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>
<script type="text/javascript">
//打开新的窗体函数
function OpenDialog(url, height, width) {
window.showModalDialog(url + "&rdm=" + Math.random(), document, "dialogHeight: " + height + "px; dialogWidth: " + width + "px;edge: Raised; center: Yes; help: No; resizable: No; scrollbars:No;");
}
var resger = function () {
//获取服务端控件的value
var textvalue = document.getElementById("<%=TextBox1.ClientID%>").value;
if (textvalue != "") {
OpenDialog("ChildrenFrom.aspx?id=1&name="+textvalue+"&contorl=TextBox2", "598", "715"); //window.showModalDialog(");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="resger();" />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
这个是子窗体的代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChildrenFrom.aspx.cs" Inherits="TestWeB.窗体赋值.ChildrenFrom" %>
<!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>
<script type="text/javascript">
function result() {
//获取父窗体的对象
var e = (window.parent.opener == undefined) ? dialogArguments : window.parent.opener.document;
var id = '<%=Request.QueryString["id"]%>';//获取url中的Id参数
var name = '<%=Request.QueryString["name"]%>';
var contorl = '<%=Request.QueryString["contorl"]%>';
//找到父窗体的dom对象,然后赋值(textbox2的text)
e.getElementById(contorl).value = id + " -" + name + " " + document.getElementById('<%=t1.ClientID %>').value;
//关闭窗体
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
子窗体
<asp:TextBox runat="server" ID="t1"></asp:TextBox>
<a href="#" οnclick="result()">确定</a>
<%-- 注销是因为他会产生刷新的功能,所以采用了客户端控件--%>
<%-- <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="result()" />--%>
</div>
</form>
</body>
</html>