如何点击按钮弹出新窗口,输入数据后返回并刷新页面? |
作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年7月8日 4点0分13秒
|
在一些.NET论坛中有人经常会问:如何在页面中点击按钮打开新页面,输入数据,然后返回到初始页面并进行更新?要解决这个问题,应该弄清楚window.showModalDialog()的用法,本人在http://www.csdn.net/Develop/read_article.asp?id=15113已经做过介绍。下面就用例子介绍如何在ASP.NET中实现这个功能。 本例子共3个页面,其中WebForm2.aspx是过渡页面,是为了防止提交时打开新页面。 WebForm1.aspx <FONT size=3><FONT size=4><%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="ShowModalDialog.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>WebForm1</title> <meta content="Microsoft Visual Studio .NET 7.0" name="GENERATOR"> <meta content="Visual Basic 7.0" 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" Text="打开窗口" Width="96px"></asp:button></form> </div> </body> </HTML> </FONT></FONT> WebForm1.aspx.vb <FONT size=3><FONT size=4>Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Button1 As System.Web.UI.WebControls.Button</FONT> #Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 If (Not IsClientScriptBlockRegistered("clientScript")) Then Dim strScript As String = "<script>" + vbCrLf strScript += "function OpenWin(){" + vbCrLf strScript += "var str=window.showModalDialog('WebForm2.aspx',document.Form1.TextBox1.value)" + vbCrLf strScript += "if(str!=null) document.Form1.TextBox1.value=str" + vbCrLf strScript += "}" + vbCrLf strScript += "</script>" + vbCrLf RegisterClientScriptBlock("clientScript", strScript) End If Button1.Attributes.Add("onclick", "OpenWin()") End Sub End Class </FONT> WebForm2.aspx <FONT size=4><%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="aspxWeb.mengxianhui.com.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.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <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> </FONT> WebForm2.aspx.vb <FONT size=4>Public Class WebForm2 Inherits System.Web.UI.Page #Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 End Sub End Class </FONT> WebForm3.aspx <FONT size=4><%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="ShowModalDialog.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.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <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="558"></asp:TextBox><br> <br> <asp:Button id="Button1" runat="server" Text=" 提 交 "></asp:Button> </form> </body> </HTML> </FONT> WebForm3.aspx.vb <FONT size=4>Public Class WebForm3 Inherits System.Web.UI.Page Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected MyBody As System.Web.UI.HtmlControls.HtmlControl #Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 If IsPostBack Then Dim strScript As String = "<script>" + vbCrLf strScript += "window.parent.returnValue='" + TextBox1.Text.Replace("'", "/'") + "'" + vbCrLf strScript += "window.parent.close()" + vbCrLf strScript += "</script>" + vbCrLf If (Not IsClientScriptBlockRegistered("clientScript")) Then RegisterClientScriptBlock("clientScript", strScript) End If End If If Not IsPostBack Then MyBody.Attributes.Add("onload", "document.Form1.TextBox1.value=window.parent.dialogArguments") End If End Sub End Class</FONT> |
本文评论(Comments):为了保护您的电子邮件不被骚扰,地址中的个别符号转换成了全角字符! | |||
评论人:huoqiming | 电子邮件:huoqiming@sohu.com | 评论日期:2004年09月09日 09:40:07 | |
孟老师: 如何把子窗口中的值传到父窗口中用户控件中的文本框中? | |||
评论人:wxk666 | 电子邮件:wxiangk666@yahoo.com.cn | 评论日期:2004年08月21日 12:00:24 | |
好,vb代码我喜欢 | |||
评论人:大木鸟 | 电子邮件:aspx888@163.com | 评论日期:2004年08月10日 02:55:41 | |
vb.net好啊支持! | |||
评论人:andfor | 电子邮件:andfor007@126.com | 评论日期:2004年08月03日 07:47:17 | |
study | |||
评论人:and | 电子邮件:a | 评论日期:2004年08月03日 07:46:43 | |
study | |||
评论人:xi_wi | 电子邮件:xi_wi◎lycos.com | 评论日期:2004年07月30日 09:37:37 | |
点击第三个页面,关闭不了它,不会向第一页面返回值。(C#) | |||
评论人:好 | 电子邮件:photoandimagelin@163.com | 评论日期:2004年07月22日 03:11:55 | |
好哦 | |||
评论人: | 电子邮件: | 评论日期:2004年07月12日 10:04:04 | |
private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 string vbCrLf="/n"; if (IsPostBack) { string strScript= " <script>" + vbCrLf; strScript += "window.parent.returnValue='" + TextBox1.Text.Replace("'", "/'") + "'" + vbCrLf; strScript += "window.parent.close()" + vbCrLf; strScript += "</script> " + vbCrLf; if (! IsClientScriptBlockRegistered("clientScript")) RegisterClientScriptBlock("clientScript", strScript); if (!IsPostBack) MyBody.Attributes.Add("onload", "document.Form1.TextBox1.value=window.parent.dialogArguments"); } } | |||
评论人:九品 | 电子邮件: | 评论日期:2004年07月12日 10:02:51 | |
public class getpass : 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.WebControls.Button Button2; protected System.Web.UI.HtmlControls.HtmlControl MyBody; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 string vbCrLf="/n"; if (IsPostBack) { string strScript= " <script>" + vbCrLf; strScript += "window.parent.returnValue='" + TextBox1.Text.Replace("'", "/'") + "'" + vbCrLf; strScript += "window.parent.close()" + vbCrLf; strScript += "</script> " + vbCrLf; if (! IsClientScriptBlockRegistered("clientScript")) RegisterClientScriptBlock("clientScript", strScript); if (!IsPostBack) 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); } /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } | |||
评论人: | 电子邮件: | 评论日期:2004年07月10日 09:30:03 | |
http://dotnet.aspx.cc/ShowDetail.aspx?id=49ML4AO8-5PB3-4KNY-NJZD-LJOIOXV4M1X4 | |||
评论人:hsb | 电子邮件:hsb0307@msn.com | 评论日期:2004年07月07日 04:49:38 | |
谁成功使用过,我怎么点击WebForm3的Button1关不了这个弹出窗口,我写了如下代码: Button1.Attributes.Add("onclick", "javascript:parent.close();"); 或者 Button1.Attributes.Add("onclick", "javascript:close();"); 也不行,不能返回值给WebForm1上的控件,请成功使用过的,给解决一下。 | |||
评论人:hsb | 电子邮件:黑死病 | 评论日期:2004年07月07日 04:49:13 | |
谁成功使用过,我怎么点击WebForm3的Button1关不了这个弹出窗口,我写了如下代码: Button1.Attributes.Add("onclick", "javascript:parent.close();"); 或者 Button1.Attributes.Add("onclick", "javascript:close();"); 也不行,不能返回值给WebForm1上的控件,请成功使用过的,给解决一下。 | |||
评论人:gpyzy | 电子邮件:gpyzy@hotmail.com | 评论日期:2004年06月19日 01:49:10 | |
大力支持孟老大多多发布vb.net的代码:) | |||
评论人:dulker | 电子邮件:dulker@sina.com | 评论日期:2004年06月16日 12:00:30 | |
?好象VB的代码居多,C#的呢 | |||
评论人:czh | 电子邮件:pzczhhw@163.com | 评论日期:2004年06月06日 07:10:05 | |
孟老师,能不能麻烦将这段程序的运行机制说一下?主要是页面之间的传递。 | |||
评论人:本站管理员 | 电子邮件: | 评论日期:2004年05月27日 05:25:12 | |
注意:<base target=_self>在IE5下无效。 | |||
评论人:本站管理员 | 电子邮件: | 评论日期:2004年05月27日 05:24:31 | |
注意: 在IE5下无效。 | |||
评论人:ssphoenix | 电子邮件:s_phoenix@tom.com | 评论日期:2004年04月02日 07:07:14 | |
sorry,看见了. | |||
评论人:ssphoenix | 电子邮件:s_phoenix@tom.com | 评论日期:2004年04月02日 07:05:24 | |
有错吧? Button1.Attributes.Add("onclick", "OpenWin()") 可是页面上并没有定义OpenWin()这个函数呀? | |||
评论人:liuyu202 | 电子邮件:liuyu202@etang.com | 评论日期:2004年03月29日 08:58:31 | |
Inherits="aspxWeb.mengxianhui.com.WebForm2" 是什么? WebForm2.aspx有错误!如下: 未能加载类型:"VbWebApp.Global" VbWebApp为应用程序名称! | |||
评论人: | 电子邮件: | 评论日期:2004年03月19日 09:17:44 | |
可以用事件@ | |||
评论人:bruce_wang | 电子邮件:bruce_wang@163.com | 评论日期:2004年03月16日 11:17:59 | |
我已经转为C#了。VB.Net转C#很容易的。vbCrLf = /n。 | |||
评论人:dsjfkf | 电子邮件: | 评论日期:2004年03月12日 02:54:16 | |
用的vbScript | |||
评论人: | 电子邮件: | 评论日期:2004年03月12日 02:48:55 | |
用的vbScript。 | |||
评论人:lswallow | 电子邮件:lswallow_163@163.net | 评论日期:2004年03月03日 10:24:07 | |
这些代码我试了一下,好像有问题。 c#的代码有吗? | |||
评论人:nowfox | 电子邮件:nowfox@tom.com | 评论日期:2004年02月27日 01:09:18 | |
更希望要C#的代码 | |||
评论人:yzwuxin | 电子邮件:yzwx007@sina.com | 评论日期:2004年02月24日 10:37:05 | |
换行 | |||
评论人:chy | 电子邮件:chenhaoying@sohu.com | 评论日期:2004年02月09日 12:01:27 | |
代码中的vbCrlf是什么啊? 有c#代码吗? | |||
评论人:melancholy | 电子邮件:aking83@msn.com | 评论日期:2004年01月05日 01:27:08 | |
Easy.... | |||