以下是一個 ModalPopupExtender 的完整範例,示範如何點擊 LinkButton 彈出一個可拖曳、帶陰影的模式對話框,並在其中選擇頁面風格後套用。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Ajax_Default7" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title>無標題頁</title>
<style type="text/css">
.modalBackground { background-color: Gray; filter: alpha(opacity=70); opacity: 0.7; }
.modalPopup { background-color: #ffffdd; border-width: 3px; border-style: solid;
border-color: Gray; padding: 3px; width: 250px; }
.sampleStyleA { background-color: #FFF; }
.sampleStyleB { background-color: #FFF; font-family: monospace; font-size: 10pt; font-weight: bold; }
.sampleStyleC { background-color: #ddffdd; font-family: sans-serif; font-size: 10pt; font-style: italic; }
.sampleStyleD { background-color: Blue; color: White; font-family: Arial; font-size: 10pt; }
</style>
<script type="text/javascript">
var styleToSelect;
function onOk() {
$get('Paragraph1').className = styleToSelect;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
<asp:Panel ID="Panel1" runat="server" Style="display: none">
<asp:Panel ID="Panel2" runat="server" CssClass="modalPopup" Width="309px">
<p>請選擇頁面風格:</p>
<p><input type="radio" name="Radio" id="RadioA" checked="checked"
onclick="styleToSelect = 'sampleStyleA';" />
<label for="RadioA" class="sampleStyleA">風格樣式一</label></p>
<p><input type="radio" name="Radio" id="RadioB"
onclick="styleToSelect = 'sampleStyleB';" />
<label for="RadioB" class="sampleStyleB">風格樣式二</label></p>
<p><input type="radio" name="Radio" id="RadioC"
onclick="styleToSelect = 'sampleStyleC';" />
<label for="RadioC" class="sampleStyleC">風格樣式三</label></p>
<p><input type="radio" name="Radio" id="RadioD"
onclick="styleToSelect = 'sampleStyleD';" />
<label for="RadioD" class="sampleStyleD">風格樣式四</label></p>
<p>
<asp:Button ID="OkButton" runat="server" Text="確定" />
<asp:Button ID="CancelButton" runat="server" Text="取消" />
</p>
</asp:Panel>
</asp:Panel>
<cc1:ModalPopupExtender id="ModalPopupExtender" runat="server"
TargetControlID="LinkButton1"
PopupControlID="Panel1"
BackgroundCssClass="modalBackground"
OkControlID="OkButton" onOkScript="onOk()"
CancelControlID="CancelButton"
DropShadow="true" Drag="true"
PopupDragHandleControlID="Panel2" />
<p id="Paragraph1">
今天天氣不錯,挺風和日麗的<br />
我們下午沒有課,這天氣挺爽的<br />
我一大中午早早跑去上自習,心裡嘀咕著大學生活是多麼美好……
</p>
</form>
</body>
</html>
這個範例的幾個重點:Panel1 用來做為彈出的容器並預設隱藏;Panel2 是實際顯示的內容,透過 PopupDragHandleControlID 指向 Panel2,即可用拖曳的方式移動視窗;DropShadow="true" 加上陰影,Drag="true" 開啟拖曳。點擊「確定」時會觸發 onOk(),把段落 Paragraph1 的樣式換成使用者所選的風格。