在父页面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">
// ------------------弹出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>
.CS代码:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (Request.Params["ctrl1ID"] != null)
{
ViewState["ctrl1ID"] = Request.Params["ctrl1ID"].Trim();
}
}
Web 窗体设计器生成的代码
第二个页面预览如下:

输入:guojiang,点击按钮。出现如下结果:

本文介绍了一个简单的页面间传值示例,通过打开一个新窗口并在其中输入数据后返回到父窗口,实现数据传递。父页面包含一个链接和一个用于显示结果的文本框,子页面包含一个文本框和一个按钮。

1071

被折叠的 条评论
为什么被折叠?



