[ASP.NET]如何点击按钮弹出新窗口,输入数据后返回并刷新页面?(C#代码)
2004-11-17 23:45
1476 查看
翻译自孟宪会之精彩世界的VB.NET代码
http://dotnet.aspx.cc/ShowDetail.aspx?id=49ML4AO8-5PB3-4KNY-NJZD-LJOIOXV4M1X4
Webform1.aspx
<!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" %>
<!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>
<frameset rows="0,*">
<frame src="about:blank">
<frame src="WebForm3.aspx">
</frameset>
</HTML>
WebForm3.aspx
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</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" id="MyBody" runat="server">
<form id="Form1" method="post" runat="server">
<asp:Label id="Label1" runat="server">请输入您的大名:</asp:Label><br>
<br>
<asp:TextBox id="TextBox1" runat="server" Width="320px"></asp:TextBox><br>
<br>
<asp:Button id="Button1" runat="server" Text=" 提 交 "></asp:Button>
</form>
</body>
</HTML>
WebForm3.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>
/// WebForm3 的摘要说明。
/// </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 = "<script>\n";
strScript += "window.parent.returnValue='" + TextBox1.Text.Replace("'", "\'") + "'\n";
strScript += "window.parent.close()\n";
strScript += "</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);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Webform4.aspx
<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm4" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm4</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">
<iframe frameborder="no" src='WebForm3.aspx' style="WIDTH: 368px; HEIGHT: 192px"></iframe>
</form>
</body>
</HTML>
http://dotnet.aspx.cc/ShowDetail.aspx?id=49ML4AO8-5PB3-4KNY-NJZD-LJOIOXV4M1X4
Webform1.aspx
<!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" %>
<!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>
<frameset rows="0,*">
<frame src="about:blank">
<frame src="WebForm3.aspx">
</frameset>
</HTML>
WebForm3.aspx
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</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" id="MyBody" runat="server">
<form id="Form1" method="post" runat="server">
<asp:Label id="Label1" runat="server">请输入您的大名:</asp:Label><br>
<br>
<asp:TextBox id="TextBox1" runat="server" Width="320px"></asp:TextBox><br>
<br>
<asp:Button id="Button1" runat="server" Text=" 提 交 "></asp:Button>
</form>
</body>
</HTML>
WebForm3.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>
/// WebForm3 的摘要说明。
/// </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 = "<script>\n";
strScript += "window.parent.returnValue='" + TextBox1.Text.Replace("'", "\'") + "'\n";
strScript += "window.parent.close()\n";
strScript += "</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);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Webform4.aspx
<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="ShowModalTest.WebForm4" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm4</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">
<iframe frameborder="no" src='WebForm3.aspx' style="WIDTH: 368px; HEIGHT: 192px"></iframe>
</form>
</body>
</HTML>
相关文章推荐
- [ASP.NET][原创]如何在客户端调用服务端代码
- 我的web+asp攻击
- C#中做的一个windows控件放入ASP.NET页面
- Asp之Request对象(1)
- 使用系统数据源配置IIS+InterDev+ASP步骤
- 浅谈在ASP.NET中数据有效性校验的方法
- asp.net DataGrid 多行编辑出现问题
- Asp.net动态生成html页面
- ASP.NET开发闲谈
- 如何建立VB、ASP等应用程序的OCX图表
- 不知道asp.net 1.1中怎么设置默认button
- Asp.net无法从数库中读取存在数据库中的TextBox中的回车与空格的解决方案!
- 存储过程介绍及asp存储过程的使用
- [ASP.net]从数据库中读取数据自动生成XML
- 在 ASP.NET 中执行 URL 重写
- asp.net Datagrid 资源
- 在ASP文件中调用DLL
- 如何把ASP编写成DLL
- 存储过程介绍及asp存储过程的使用
- 用ASP实现分级权限控制