Webform1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm1" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<div align="center">
<form id="Form1" method="post" runat="server">
<asp:label id="Label1" runat="server" Font-Bold="true">
從當前頁面打開新窗口,並把變量傳遞到新窗口的例子,可以多次打開提交。
</asp:label><br>
<br>
<asp:textbox id="TextBox1" runat="server" Width="600px">這是初始值,將被傳遞到新窗口。</asp:textbox><br>
<br>
<asp:button id="Button1" runat="server" Width="96px" Text="打開窗口2"></asp:button>
<asp:button id="Button2" runat="server" Width="96px" Text="打開窗口4"></asp:button></form>
</div>
</body>
</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
{
/// <summary>
/// WebForm1 的摘要說明。
/// </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 = "<script>/n";
strScript += "function OpenWin(){/n";
strScript += "var str=window.showModalDialog('WebForm2.aspx',document.Form1.TextBox1.value,'help:no')/n";
strScript += "if(str!=null) document.Form1.TextBox1.value=str/n";
strScript += "}/n";
strScript += "</script>/n";
RegisterClientScriptBlock("clientScript", strScript);
}
if (!IsClientScriptBlockRegistered("clientScript2"))
{
strScript = "<script>/n";
strScript += "function OpenWin2(){/n";
strScript += "var str=window.showModalDialog('WebForm4.aspx',document.Form1.TextBox1.value,'help:no')/n";
strScript += "if(str!=null) document.Form1.TextBox1.value=str/n";
strScript += "}/n";
strScript += "</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);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Webform2.aspx
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm2" %> <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >