如何点击按钮弹出新窗口,输入数据后返回并刷新页面?

如何点击按钮弹出新窗口,输入数据后返回并刷新页面?

作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期: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

1@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="ShowModalDialog.WebForm1" 

从当前页面打开新窗口,并把变量传递到新窗口的例子,可以多次打开提交。

这是初始值,将被传递到新窗口。

WebForm1.aspx.vb

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 #Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 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 = " " + vbCrLf RegisterClientScriptBlock("clientScript", strScript) End If Button1.Attributes.Add("onclick", "OpenWin()") End Sub End Class

WebForm2.aspx

1@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="aspxWeb.mengxianhui.com.WebForm2"

WebForm2.aspx.vb

Public Class WebForm2 Inherits System.Web.UI.Page #Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 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

WebForm3.aspx

 1@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="ShowModalDialog.WebForm3" 
 2``` 请输入您的大名:    
 3  
 4  
 5  
 6
 7
 8**WebForm3.aspx.vb**
 9
10Public 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 窗体设计器所必需的。  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 = "  " + 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   
11  
12**本文评论(Comments):** 为了保护您的电子邮件不被骚扰,地址中的个别符号转换成了全角字符!   
13---  
14|  评论人: **lll** |  电子邮件: **cool_night@sina.com** |  评论日期:2004年10月21日 11:37:33   
15|  最简单的办法还是用javascript,把下面保存成一个test.htm文件即可测试   
16  
17  
18  
19  
20function DoCyj()   
21{   
22window.onfocus = RefreshOnNextFocus;   
23window.open("test.htm");   
24}   
25function RefreshOnNextFocus()   
26{   
27alert("ok");   
28window.location.href=window.location.href;   
29window.onfocus = null;   
30}
Published At
Categories with Web编程
Tagged with
comments powered by Disqus