在父页面Parent.aspx点击弹出第二个页面,将会弹出Child.aspx子页面,在子页面中输入需要传入到Parent.aspx的值点击确认,则可以把Child.aspx子页面输入的值传到Parent.aspx上的控件上。
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
"
>
![None.gif](/Images/OutliningIndicators/None.gif)
//
------------------弹出window.open窗口--------------------
function openwin(fileName,type,window_width,window_height,property)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var top = (window.screen.availHeight-window_height)/2;
var left = (window.screen.availWidth-window_width)/2;
var newwindow;
if (window_width=="" && window_height=="")
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (type=="0")
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
newwindow=window.open(fileName,"","left="+left+",top="+top+","+property);
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
newwindow=window.open(fileName,"","left=0,top=0,"+property);
}
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (type=="0")
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
newwindow=window.open(fileName,"","left=0,top=0,height="+window_height+",width="+window_width+","+property);
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
newwindow=window.open(fileName,"","left="+left+",top="+top+",height="+window_height+",width="+window_width+","+property);
}
}
return newwindow;
}
![None.gif](/Images/OutliningIndicators/None.gif)
function openwinsimp(fileName,window_width,window_height)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//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;
}
![None.gif](/Images/OutliningIndicators/None.gif)
</
script
>
<
script
>
function getid()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
var id = new String(window.location.href);
//alert(id);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if( id.indexOf("id=", 1) ==-1 )
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
id=-1;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
>
预览如下:
![0528.JPG](https://images.cnblogs.com/cnblogs_com/gjahead/0528.JPG)
<%
@ 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
>
.CS代码:
private
void
Page_Load(
object
sender, System.EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 在此处放置用户代码以初始化页面
if (Request.Params["ctrl1ID"] != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ViewState["ctrl1ID"] = Request.Params["ctrl1ID"].Trim();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
}
![None.gif](/Images/OutliningIndicators/None.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
Web 窗体设计器生成的代码
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// 生成script脚本,完成对父窗口控件的赋值
/// </summary>
/// <param name="value1">控件1的值</param>
/// <param name="value2">控件2的值</param>
/// <returns></returns>
private
string
MakeScriptStr(
string
text)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//value= " [" + value + "]";
string result = "<script>";
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
result += "window.opener.document.all('" + ViewState["ctrl1ID"] + "',0).value='" + text + "';";
result += "window.close();";
result += "</script>";
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
return result;
}
private
void
Button1_Click(
object
sender, System.EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string strScript = this.MakeScriptStr(this.TextBox1.Text);
Page.RegisterStartupScript("return1", strScript);
}