主要原理:是通过js的方法showModalDialog来实现的,2个窗体页面之间使用的是Session传递,在主窗体页面上放置一个隐藏的按钮。
<
asp:LinkButton
ID
="LinkButton1"
runat
="server"
style
="display:none"
>
刷新
</
asp:LinkButton
>
在后台CS文件调用方法打开模式窗体,所以写一个简单的类,如下:
public
class
ModalPage
{
private
static
string
ModalWindow(
string
openAspxPage,
int
width,
int
height)
{
string
js
=
string
.Format(
"
javascript:window.showModalDialog(\
"
{
0
}\
"
,window,\
"
status:
false
;dialogWidth:{
1
}px;dialogHeight:{
2
}px\
"
);javascript:__doPostBack('LinkButton1','');
"
, openAspxPage, width, height);
//
window.location.href=window.location.href
return
js;
}
private
static
void
RegisterJavaScript(Page p,
string
js)
{
p.RegisterStartupScript(
""
,
"
<script>
"
+
js
+
"
</script>
"
);
}
private
static
void
RegisterJavaScript(UpdatePanel up,
string
js,
bool
b)
{
ScriptManager.RegisterStartupScript(up, up.GetType(),
""
, js,
true
);
}
///
<summary>
///
显示模态窗体
///
</summary>
///
<param name="page">
包含的模态页面
</param>
///
<param name="width"></param>
///
<param name="height"></param>
///
<param name="p">
主窗体
</param>
public
static
void
ShowDialog(
string
page,
int
width,
int
height, Page p)
{
string
js
=
ModalWindow(page, width, height);
RegisterJavaScript(p, js);
}
public
static
void
ShowDialog(
string
page,
int
width,
int
height, UpdatePanel up)
{
string
js
=
ModalWindow(page, width, height);
RegisterJavaScript(up, js,
true
);
}
}
在模式窗体页面做一些你想要的操作,比如:输入一些值,通过session传递对象,例如:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
frame_modal : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
IsPostBack)
{
Title
=
Server.UrlDecode( Request.QueryString[
"
t
"
].ToString());
Label1.Text
=
Server.UrlDecode(Request.QueryString[
"
s
"
].ToString());
string
s
=
Request.QueryString[
"
c
"
].ToString();
UserControl user
=
(UserControl)LoadControl(
"
../UserControls/
"
+
s);
Page.Controls.Add(user);
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
Save s
=
new
Save();
s.Value1
=
TextBox1.Text;
s.Value2
=
TextBox2.Text;
Session[
"
s
"
]
=
(
object
)s;
Response.Write(
"
<script>window.opener=null;window.close();</script>
"
);
}
}
然后是在主窗体页面,因为是通过javascript:__doPostBack('LinkButton1','');这句实现的刷新方法,所以在主窗体load事件中处理传递来的值,如下:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
frame_main : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
IsPostBack)
{
}
else
{
if
(Session[
"
s
"
]
!=
null
)
{
Save s
=
(Save)Session[
"
s
"
];
TextBox2.Text
=
s.Value1;
TextBox3.Text
=
s.Value2;
}
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
string
i1
=
Server.UrlEncode(
"
这是窗体传递的字符串
"
);
string
i2
=
Server.UrlEncode(
"
标题:
"
+
drp.Items[drp.SelectedIndex].Text);
string
s
=
string
.Format(
"
modal.aspx?t={0}&s={1}&c={2}
"
,i2 , i1, drp.SelectedValue);
//
ModalPage.ShowDialog(s, 600, 400, this);
ModalPage.ShowDialog(s,
600
,
400
, UpdatePanel1);
}
}
最后就是对象的代码:
///
<summary>
///
Save 保存值传递的对象
///
</summary>
public
class
Save
{
private
string
_value1;
private
string
_value2;
public
string
Value1
{
get
{
return
_value1; }
set
{ _value1
=
value; }
}
public
string
Value2
{
get
{
return
_value2; }
set
{ _value2
=
value; }
}
public
Save()
{
}
}
不过实验过程中,遇到了一个问题:如果把LinkButton1放到UpdatePanel1里的区域内,就无法达到刷新的效果了,再网上也没找到相应的解决方法,下面是主窗体前台代码,希望高手能出手帮忙解决下,我想实现关闭模式窗体后让UpdatePanel1里的区域内局部刷新,好像UpdatePanel1阻止了自己写的JS代码:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
main.aspx.cs
"
Inherits
=
"
frame_main
"
%>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
></
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
asp:ScriptManager
ID
="ScriptManager1"
runat
="server"
>
</
asp:ScriptManager
>
<
asp:UpdatePanel
ID
="UpdatePanel1"
runat
="server"
>
<
ContentTemplate
>
选择要打开的用户控件
<
asp:DropDownList
ID
="drp"
runat
="server"
Width
="120px"
>
<
asp:ListItem
Value
="a.ascx"
>
测试控件a
</
asp:ListItem
>
<
asp:ListItem
Value
="b.ascx"
>
测试控件b
</
asp:ListItem
>
</
asp:DropDownList
>
<
asp:Button
ID
="Button1"
runat
="server"
Text
="打 开"
Width
="80px"
ToolTip
="服务器Button控件"
OnClick
="Button1_Click"
/>
<
asp:TextBox
ID
="TextBox1"
runat
="server"
Text
="原来的数据"
></
asp:TextBox
>
<
asp:TextBox
ID
="TextBox2"
runat
="server"
></
asp:TextBox
>
<
asp:TextBox
ID
="TextBox3"
runat
="server"
></
asp:TextBox
>
</
ContentTemplate
>
</
asp:UpdatePanel
>
<
div
>
<
asp:LinkButton
ID
="LinkButton1"
runat
="server"
style
="display:none"
>
刷新
</
asp:LinkButton
>
</
div
>
</
form
>
</
body
>
</
html
>
源码地址:右键另存为