翻译自孟宪会之精彩世界的VB.NET代码
http://dotnet.aspx.cc/ShowDetail.aspx?id=49ML4AO8-5PB3-4KNY-NJZD-LJOIOXV4M1X4
**Webform1.aspx
1@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm1"
2``` ** ```
3@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm1"
1<html>
2<head>
3<title>WebForm1</title>
4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
5<meta content="C#" name="CODE_LANGUAGE"/>
6<meta content="JavaScript" name="vs_defaultClientScript"/>
7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
8</head>
9<body ms_positioning="GridLayout">
10<div align="center">
11<form id="Form1" method="post" runat="server">
12<asp:label font-bold="true" id="Label1" runat="server">
13从当前页面打开新窗口,并把变量传递到新窗口的例子,可以多次打开提交。
14</asp:label><br/>
15<br/>
16<asp:textbox id="TextBox1" runat="server" width="600px">这是初始值,将被传递到新窗口。</asp:textbox><br/>
17<br/>
18<asp:button id="Button1" runat="server" text="打开窗口2" width="96px"></asp:button>
19<asp:button id="Button2" runat="server" text="打开窗口4" width="96px"></asp:button></form>
20</div>
21</body>
22</html>
Webform1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ShowModalTest
{
///
1<summary>
2/// WebForm1 的摘要说明。
3/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
// 在此处放置用户代码以初始化页面
string strScript= "\n";
if (!IsClientScriptBlockRegistered("clientScript"))
{
strScript = "
1<script>\n";
2strScript += "function OpenWin(){\n";
3strScript += "var str=window.showModalDialog('WebForm2.aspx',document.Form1.TextBox1.value,'help:no')\n";
4strScript += "if(str!=null) document.Form1.TextBox1.value=str\n";
5strScript += "}\n";
6strScript += "</script>
\n";
RegisterClientScriptBlock("clientScript", strScript);
}
if (!IsClientScriptBlockRegistered("clientScript2"))
{
strScript = "
1<script>\n";
2strScript += "function OpenWin2(){\n";
3strScript += "var str=window.showModalDialog('WebForm4.aspx',document.Form1.TextBox1.value,'help:no')\n";
4strScript += "if(str!=null) document.Form1.TextBox1.value=str\n";
5strScript += "}\n";
6strScript += "</script>
\n";
RegisterClientScriptBlock("clientScript2", strScript);
}
Button1.Attributes.Add("onclick", "OpenWin()");
Button2.Attributes.Add("onclick", "OpenWin2()");
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
1<summary>
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改
3/// 此方法的内容。
4/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Webform2.aspx
1@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm2"
2``` ```
3@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm2"
1<html>
2<head>
3<title>WebForm2</title>
4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
5<meta content="C#" name="CODE_LANGUAGE"/>
6<meta content="JavaScript" name="vs_defaultClientScript"/>
7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
8</head>
9<frameset rows="0,*">
10<frame src="about:blank"/>
11<frame src="WebForm3.aspx"/>
12</frameset>
13</html>
**WebForm3.aspx
** ```
@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm3"
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ShowModalTest
{
///
1<summary>
2/// WebForm3 的摘要说明。
3/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlControl MyBody;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(IsPostBack)
{
string strScript = "
1<script>\n";
2strScript += "window.parent.returnValue='" + TextBox1.Text.Replace("'", "\'") + "'\n";
3strScript += "window.parent.close()\n";
4strScript += "</script>
\n";
if (!IsClientScriptBlockRegistered("clientScript"))
RegisterClientScriptBlock("clientScript", strScript);
}
else
{
MyBody.Attributes.Add("onload", "document.Form1.TextBox1.value=window.parent.dialogArguments");
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
1<summary>
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改
3/// 此方法的内容。
4/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
** Webform4.aspx
1@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm4"
1<html>
2<head>
3<title>WebForm4</title>
4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
5<meta content="C#" name="CODE_LANGUAGE"/>
6<meta content="JavaScript" name="vs_defaultClientScript"/>
7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
8</head>
9<body ms_positioning="GridLayout">
10<form id="Form1" method="post" runat="server">
11<iframe frameborder="no" src="WebForm3.aspx" style="WIDTH: 368px; HEIGHT: 192px"></iframe>
12</form>
13</body>
14</html>
**