ModalPopupExtender是个非常好用的AJAX控件, 网上有很多教程,但大多没有提起它的一些使用技巧:
1.简单应用:
常用属性:
TargetControlID:用于触发弹出面板的控件。
OkControlID:弹出面板中的确认按钮,用于确认新的样式。
OnOkScript:当单击确认按钮后,关闭样式面板后,执行脚本。
CancelControlID:样式面板中的取消按钮,用于取消应用样式。
PopupDragHandleControlID:样式面板中用于触发面板的控件。
DropShadow:样式面板是否有阴影。值为True,则有阴影;值为False,则没有阴影。
BackgroundCssClass:样式面板中应用的css样式。
最简单的应用示例:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
<cc1:ModalPopupExtender ID="Button1_ModalPopupExtender" runat="server"
DynamicServicePath="" Enabled="True" TargetControlID="Button1" PopupControlID="Panel1">
</cc1:ModalPopupExtender>
</div>
<asp:Panel ID="Panel1" runat="server" Height="176px" Width="141px">
</asp:Panel>
</form>
</body>
</html>
2.半透明阴影遮罩
要为ModalPopupExtender添加半透明阴影,就像官方网站上的案例一样,需要定义一个CSS类,并指派到BackgroundCssClass属性上
半透明背景css可以如下:
Code
<style type="text/css">
.bg
{
position: absolute;
z-index: 100;
top: 0px;
left: 0px;
background-color: #000;
filter: alpha(opacity=60);
-moz-opacity: 0.6;
opacity: 0.6;
}
</style>
3.JavaScript 控制
如果我们需要用JavaScript控制弹出层的 show/hide ,使用以下代码即可:
$find('Panel1').hide()
$find('Panel1').show()
前提是,PopupControlID和BehaviorID都要是Panel1
完整代码如下:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="modalpopup.aspx.cs" Inherits="modalpopup" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!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>Untitled Page</title>
<style type="text/css">
.bg
{
position: absolute;
z-index: 100;
top: 0px;
left: 0px;
background-color: #000;
filter: alpha(opacity=60);
-moz-opacity: 0.6;
opacity: 0.6;
}
</style>
<script language="javascript" type="text/javascript">
// <!CDATA[
function Button2_onclick() {
// $find('Panel1').hide()
$find('Panel1').show()
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
</div>
<p>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Button" />
</p>
<asp:Panel ID="Panel1" runat="server" Height="156px" Width="127px">
<asp:Image ID="Image1" runat="server" Height="149px" ImageUrl="~/head_7953l61.jpg"
Width="123px" />
</asp:Panel>
<cc1:ModalPopupExtender ID="Panel1_ModalPopupExtender" runat="server" DynamicServicePath=""
BehaviorID="Panel1" BackgroundCssClass="bg" PopupControlID="Panel1" Enabled="True" TargetControlID="Button1">
</cc1:ModalPopupExtender>
</form>
<p>
<input id="Button2" type="button" value="button" onclick="return Button2_onclick()" /></p>
</body>
</html>