我们有时候会需要在页面间进行传值,将在某一个页面所做的操作的影响传递到另一个页面.
通常有两种情况,一种是页面跳转时传值,通过querystring,session等都可以达到这种效果.
这里说说第二种,即在当前页面打开一个新页面,做一些操作后,将这些结果统一的在当前页面来提交.
代码如下:
Parent.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Parent.aspx.cs" Inherits="TwoPage_Parent" %>
<!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>
<body>
<form id="form1" runat="server">
<div>
<a href = "javascript:w = window.open('Child.aspx?param=paramValue ','w','width=500,height=500'); w.focus();">打开子页面</a>
<asp:Label ID="Label1" runat="server" Width="181px"></asp:Label></div>
</form>
</body>
</html>
Child.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Child.aspx.cs" Inherits="TwoPage_Child" %>
<!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>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>
Child.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script language='javascript'>opener.Label1.innerText = '" + this.TextBox1.Text + "';</script>");
Response.Write("<script language='javascript'>alert('提交成功')</script>");
Response.Write("<script language='javascript'>window.close();</script>");
}
运行上面的代码,在子页面中的值可以传递到父页面,来一起提交.
除了window.open外还一种更好的方法-window.showModalDialog.以后我们再补充.