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

作者:网络 来源:佚名 更新时间:2008-02-05 14:29:24 点击:
在一些.net论坛中有人经常会问:如何在页面中点击按钮打开新页面,输入数据,然后返回到初始页面并进行更新?要解决这个问题,应该弄清楚window.showmodaldialog()的用法,本人在http://www.csdn.net/develop/read_article.asp?id=15113已经做过介绍。下面就用例子介绍如何在asp.net中实现这个功能。

本例子共3个页面,其中webform2.aspx是过渡页面,是为了防止提交时打开新页面。

webform1.aspx

<%@ 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>

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 窗体设计器所必需的。
<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

webform2.aspx

<%@ 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>

webform2.aspx.vb

public class webform2
inherits system.web.ui.page

#region " web 窗体设计器生成的代码 "

'该调用是 web 窗体设计器所必需的。
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

商业源码热门下载www.html.org.cn

||| 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

<%@ 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>

webform3.aspx.vb

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