子页面赋值给父页面的控件方法
在父页面Parent.aspx点击弹出第二个页面,将会弹出Child.aspx子页面,在子页面中输入需要传入到Parent.aspx的值点击确认,则可以把Child.aspx子页面输入的值传到Parent.aspx上的控件上。
Parent.aspx的HTML为:
预览如下:
Parent.aspx的HTML为:
<%@ Page language="c#" Codebehind="Parent.aspx.cs" AutoEventWireup="false" Inherits="TestToParent.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
// ------------------弹出window.open窗口--------------------
function openwin(fileName,type,window_width,window_height,property)
{
var top = (window.screen.availHeight-window_height)/2;
var left = (window.screen.availWidth-window_width)/2;
var newwindow;
if (window_width=="" && window_height=="")
{
if (type=="0")
{
newwindow=window.open(fileName,"","left="+left+",top="+top+","+property);
}
else
{
newwindow=window.open(fileName,"","left=0,top=0,"+property);
}
}
else
{
if (type=="0")
{
newwindow=window.open(fileName,"","left=0,top=0,height="+window_height+",width="+window_width+","+property);
}
else
{
newwindow=window.open(fileName,"","left="+left+",top="+top+",height="+window_height+",width="+window_width+","+property);
}
}
return newwindow;
}
function openwinsimp(fileName,window_width,window_height)
{
//alert(fileName);
var newwindow=openwin(fileName,'1',window_width,window_height,'resizable=no,scrollbars=auto,status=yes,toolbar=no,menubar=no,location=no');
newwindow.focus();
return newwindow;
}
</script>
<script>
function getid()
{
var id = new String(window.location.href);
//alert(id);
if( id.indexOf("id=", 1) ==-1 )
{
id=-1;
}
else
{
id = parseInt( id.substr( id.indexOf("id=", 1) + 3 ) );
}
// alert(id);
return id;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<a href="#" onclick="openwinsimp('./Child.aspx?id='+getid()+'&type=d1&ctrl1ID=txt_Child',1024,768)">
弹出一个页面</a>
<asp:TextBox id="txt_Child" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 80px"
runat="server"></asp:TextBox>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 96px; POSITION: absolute; TOP: 32px" runat="server">第一个页面</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 88px" runat="server">你在第二个页面输入的是:</asp:Label>
</form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
// ------------------弹出window.open窗口--------------------
function openwin(fileName,type,window_width,window_height,property)
{
var top = (window.screen.availHeight-window_height)/2;
var left = (window.screen.availWidth-window_width)/2;
var newwindow;
if (window_width=="" && window_height=="")
{
if (type=="0")
{
newwindow=window.open(fileName,"","left="+left+",top="+top+","+property);
}
else
{
newwindow=window.open(fileName,"","left=0,top=0,"+property);
}
}
else
{
if (type=="0")
{
newwindow=window.open(fileName,"","left=0,top=0,height="+window_height+",width="+window_width+","+property);
}
else
{
newwindow=window.open(fileName,"","left="+left+",top="+top+",height="+window_height+",width="+window_width+","+property);
}
}
return newwindow;
}
function openwinsimp(fileName,window_width,window_height)
{
//alert(fileName);
var newwindow=openwin(fileName,'1',window_width,window_height,'resizable=no,scrollbars=auto,status=yes,toolbar=no,menubar=no,location=no');
newwindow.focus();
return newwindow;
}
</script>
<script>
function getid()
{
var id = new String(window.location.href);
//alert(id);
if( id.indexOf("id=", 1) ==-1 )
{
id=-1;
}
else
{
id = parseInt( id.substr( id.indexOf("id=", 1) + 3 ) );
}
// alert(id);
return id;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<a href="#" onclick="openwinsimp('./Child.aspx?id='+getid()+'&type=d1&ctrl1ID=txt_Child',1024,768)">
弹出一个页面</a>
<asp:TextBox id="txt_Child" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 80px"
runat="server"></asp:TextBox>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 96px; POSITION: absolute; TOP: 32px" runat="server">第一个页面</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 88px" runat="server">你在第二个页面输入的是:</asp:Label>
</form>
</body>
</HTML>
预览如下:
<%@ Page language="c#" Codebehind="Child.aspx.cs" AutoEventWireup="false" Inherits="TestToParent.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 128px" runat="server"
Text="Button"></asp:Button>
<asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 96px; POSITION: absolute; TOP: 88px" runat="server"></asp:TextBox>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 0px; POSITION: absolute; TOP: 96px" runat="server">第二个页面</asp:Label></FONT>
</form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 128px" runat="server"
Text="Button"></asp:Button>
<asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 96px; POSITION: absolute; TOP: 88px" runat="server"></asp:TextBox>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 0px; POSITION: absolute; TOP: 96px" runat="server">第二个页面</asp:Label></FONT>
</form>
</body>
</HTML>
.CS代码:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (Request.Params["ctrl1ID"] != null)
{
ViewState["ctrl1ID"] = Request.Params["ctrl1ID"].Trim();
}
}
Web 窗体设计器生成的代码
/// <summary>
/// 生成script脚本,完成对父窗口控件的赋值
/// </summary>
/// <param name="value1">控件1的值</param>
/// <param name="value2">控件2的值</param>
/// <returns></returns>
private string MakeScriptStr(string text)
{
//value= " [" + value + "]";
string result = "<script>";
result += "window.opener.document.all('" + ViewState["ctrl1ID"] + "',0).value='" + text + "';";
result += "window.close();";
result += "</script>";
return result;
}
private void Button1_Click(object sender, System.EventArgs e)
{
string strScript = this.MakeScriptStr(this.TextBox1.Text);
Page.RegisterStartupScript("return1", strScript);
}
{
// 在此处放置用户代码以初始化页面
if (Request.Params["ctrl1ID"] != null)
{
ViewState["ctrl1ID"] = Request.Params["ctrl1ID"].Trim();
}
}
Web 窗体设计器生成的代码
/// <summary>
/// 生成script脚本,完成对父窗口控件的赋值
/// </summary>
/// <param name="value1">控件1的值</param>
/// <param name="value2">控件2的值</param>
/// <returns></returns>
private string MakeScriptStr(string text)
{
//value= " [" + value + "]";
string result = "<script>";
result += "window.opener.document.all('" + ViewState["ctrl1ID"] + "',0).value='" + text + "';";
result += "window.close();";
result += "</script>";
return result;
}
private void Button1_Click(object sender, System.EventArgs e)
{
string strScript = this.MakeScriptStr(this.TextBox1.Text);
Page.RegisterStartupScript("return1", strScript);
}