利用AjaxControlToolkit的ModalPopupExtender做一個下載檔案出現版權說明的同意不同意視窗

利用AjaxControlToolkit的ModalPopupExtender做一個下載檔案出現版權說明的同意不同意視窗

好久沒玩ajax control toolkit了..

今天小弟介紹如何用ModalPopupExtender做出一個下載結連點撃後出現下面畫面的樣子

參考圖片網址(點AjaxControlToolkit.zip):
http://www.codeplex.com/AtlasControlToolkit/Release/ProjectReleases.aspx?ReleaseId=11121

c#範例

主程式
Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
    .background
    {
        background-color:Gray;
        filter:alpha(opacity=60);
        opacity:0.6;
    }    
    </style>

    <script type="text/javascript">
    function download()
    {
      var iframe = document.createElement("iframe");

      iframe.src = "GenerateFile.aspx";

      iframe.style.display = "none";

      document.body.appendChild(iframe);
    }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server">download</asp:LinkButton>
                    <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="Panel1"
                        TargetControlID="LinkButton1" BackgroundCssClass="background" OkControlID="btnOk"
                        CancelControlID="btnCancel" OnOkScript="download()">
                    </ajaxToolkit:ModalPopupExtender>
                    <asp:Panel ID="Panel1" runat="server" Width="500px" BorderWidth="1px">
                        <div style="background-color: Gray; color: Black; text-align: center;">
                            To download the file you must agree to the following license.
                            <asp:TextBox ID="TextBox1" runat="server" Height="200px" TextMode="MultiLine" Width="490px">Microsoft Public License (Ms-PL)

This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.

1. Definitions

The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.

A "contribution" is the original software, or any additions or changes to the software.

A "contributor" is any person that distributes its contribution under this license.

"Licensed patents" are a contributor's patent claims that read directly on its contribution.

2. Grant of Rights

(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.

(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.

3. Conditions and Limitations

(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.

(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.

(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.

(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.

(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.</asp:TextBox>
                            <asp:Button ID="btnOk" runat="server" Text="I Agree" OnClientClick="return false;" />
                            <asp:Button ID="btnCancel" runat="server" Text="I Disagree" OnClientClick="return false;" />
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

點撃下載後會透會透過這支程式下載檔案

GenerateFile.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GenerateFile.aspx.cs" Inherits="GenerateFile" %>

<!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">
    <div>
    
    </div>
    </form>
</body>
</html>

GenerateFile.aspx.cs

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 GenerateFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.AddHeader("Content-disposition", "attachment; filename=test.txt");
        Response.ContentType = "application/octet-stream";
        Response.WriteFile("test.txt");
        Response.End();
    }

}

執行結果:

參考網址:
http://encosia.com/2007/02/23/ajax-file-downloads-and-iframes/
http://www.codeplex.com/AtlasControlToolkit/Release/ProjectReleases.aspx?ReleaseId=11121