发送文件后关闭模态弹出窗口

时间:2013-07-30 15:40:43

标签: c# asp.net ajaxcontroltoolkit modalpopupextender

我正在寻找某种行为,其中ModalPopup为要下载的文件建议(业务规则驱动的)文件名。

enter image description here

如果用户单击“确定”,则会下载文件,并且应关闭弹出窗口。取消只是关闭它。这完全由后面的代码驱动:

protected void ExportPromptOkButton_Clicked(object sender, EventArgs e)
{
    string path = MapPath(ExportPromptPanelFileName.Text);
    WriteExport(path);
    ExportPromptModalPopupExtender.Hide();
    this.SendFile(path, "text/plain");
}

//...

public static void SendFile(this Page webPage, string filepath, string contenttype)
{
    webPage.Response.AddHeader("Content-disposition", "attachment; filename=" + Path.GetFileName(filepath));
    webPage.Response.ContentType = contenttype;
    webPage.Response.WriteFile(filepath);;
    webPage.Response.End();
}

问题是Response.End();会杀死所有动作(好吧,应该如此),因此模态窗口本身不会关闭。

什么是正确的方法?我正在考虑使用iframe并调用其Response来发送文件,但我想确认这是否合适或是否有更好的东西。

ASPX声明:

<ajaxToolkit:ModalPopupExtender 
    runat="server" 
    ID="ExportPromptModalPopupExtender" 
    BackgroundCssClass="modalBackground"
    TargetControlID="ExportButton" 
    PopupControlID="ExportPromptPanel" 
    PopupDragHandleControlID="ExportPromptPanelHeader"
    CancelControlID="ExportPromptCancelButton"
    Drag="true">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="ExportPromptPanel" runat="server" CssClass="popupConfirmation" style="display: none;">
    <div class="popupContainer" style="width:300px">
        <div class="popupHeader" id="ExportPromptPanelHeader">
            <div class="popupHeaderLeft">Export</div><div class="popupHeaderRight"></div>
        </div>
        <div class="popupBody">Export under the following name:<br />
        <asp:TextBox ID="ExportPromptPanelFileName" runat="server" MaxLength="60" Width="230px"></asp:TextBox></div><div class="popupButtons">
            <asp:Button ID="ExportPromptOkButton" runat="server" Text="Ok" OnClick="ExportPromptOkButton_Clicked" />
            <asp:Button ID="ExportPromptCancelButton" runat="server" Text="Cancel" />
        </div>
    </div>
</asp:Panel>

3 个答案:

答案 0 :(得分:2)

您可以设置扩展器的OkControlID属性以隐藏确定按钮上的弹出窗口以及OnOkScript属性以获取自定义javascript功能,这将强制从Ok按钮回发(模式弹出扩展器)阻止从Ok和Cancel控件回发。)

<script type="text/javascript">
    function doExport() {
        __doPostBack("<%= ExportPromptOkButton.UniqueID %>", "");
    }
</script>


<ajaxToolkit:ModalPopupExtender
    runat="server"
    ID="ExportPromptModalPopupExtender"
    BackgroundCssClass="modalBackground"
    TargetControlID="ExportButton"
    PopupControlID="ExportPromptPanel"
    PopupDragHandleControlID="ExportPromptPanelHeader"
    CancelControlID="ExportPromptCancelButton"
    OkControlID="ExportPromptOkButton"
    OnOkScript="doExport"
    Drag="true">
</ajaxToolkit:ModalPopupExtender>

答案 1 :(得分:1)

您需要创建一个单独的http处理程序来处理请求 在Modal Popup中调用HTTP HANDLER,如图所示

ModalPopup中的

protected void Click(object sender, EventArgs e)
    {
        Response.Redirect("Handler1.ashx", true);
    }
Handler1.ashx中的

public void ProcessRequest(HttpContext context)
    {
        context.Response.Clear();
        context.Response.ContentType = "application/vnd-ms.excel";
        context.Response.AddHeader("content-disposition", "attachment;Filename=test.xls");
        context.Response.WriteFile(filepath);
        context.Response.Flush();
        context.Response.End();
    }

答案 2 :(得分:0)

您是否尝试使用ModalPopupExtender.hide()!