发送文件后,关闭ModelPopup [英] Closing a ModelPopup after sending a file

查看:138
本文介绍了发送文件后,关闭ModelPopup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果用户点击OK,文件下载,并且在弹出应该关闭。取消只是将其关闭。

:这是全部由code驱动背后

 保护无效ExportPromptOkButton_Clicked(对象发件人,EventArgs的发送)
{
    路径字符串=的MapPath(ExportPromptPanelFileName.Text);
    WriteExport(路径);
    ExportPromptModalPopupExtender.Hide();
    this.SendFile(路径,text / plain的);
}// ...公共静态无效SENDFILE(此页网页,文件路径字符串,字符串的contentType)
{
    webPage.Response.AddHeader(内容处置,附件;文件名=+ Path.GetFileName(文件路径));
    webPage.Response.ContentType的contentType =;
    webPage.Response.WriteFile(文件路径);;
    webPage.Response.End();
}

问题是,到Response.End(); 杀死所有的动作(当然,它应该),所以模态窗口本身不会关闭。

什么是正确的做法吗我想使用iframe并调用它的响应要发送的文件,但我想确认这是否是合适的,或者有更好的东西。

ASPX声明:

 < ajaxToolkit:ModalPopupExtender
    =服务器
    ID =ExportPromptModalPopupExtender
    BackgroundCssClass =modalBackground
    的TargetControlID =ExportButton
    PopupControlID =ExportPromptPanel
    PopupDragHandleControlID =ExportPromptPanelHeader
    CancelControlID =ExportPromptCancelButton
    拖动=真正的>
< / ajaxToolkit:ModalPopupExtender>
< ASP:面板ID =ExportPromptPanel=服务器的CssClass =popupConfirmation的风格=显示:无;>
    < D​​IV CLASS =popupContainer的风格=宽度:300像素>
        < D​​IV CLASS =popupHeaderID =ExportPromptPanelHeader>
            < D​​IV CLASS =popupHeaderLeft>出口与LT; / DIV>< D​​IV CLASS =popupHeaderRight>< / DIV>
        < / DIV>
        < D​​IV CLASS =popupBody>在下面的出口名称:LT; BR />
        < ASP:文本框ID =ExportPromptPanelFileName=服务器MAXLENGTH =60WIDTH =230px>< / ASP:文本框>< / DIV>< D​​IV CLASS =popupButtons>
            < ASP:按钮的ID =ExportPromptOkButton=服务器文本=确定的OnClick =ExportPromptOkButton_Clicked/>
            < ASP:按钮的ID =ExportPromptCancelButton=服务器文本=取消/>
        < / DIV>
    < / DIV>
< / ASP:面板>


解决方案

您可以设定扩展的 OkControlID 属性为隐藏确定按钮弹出点击 OnOkScript 自定义属性javascript函数,这将迫使回传从确定按钮(模式弹出扩展prevents从确定回传和取消控制)。

 <脚本类型=文/ JavaScript的>
    功能doExport(){
        __doPostBack(&下;%= ExportPromptOkButton.UniqueID%gt;中,);
    }
< / SCRIPT>
< ajaxToolkit:ModalPopupExtender
    =服务器
    ID =ExportPromptModalPopupExtender
    BackgroundCssClass =modalBackground
    的TargetControlID =ExportButton
    PopupControlID =ExportPromptPanel
    PopupDragHandleControlID =ExportPromptPanelHeader
    CancelControlID =ExportPromptCancelButton
    OkControlID =ExportPromptOkButton
    OnOkScript =doExport
    拖动=真正的>
< / ajaxToolkit:ModalPopupExtender>

I'm looking for a certain behaviour where a ModalPopup suggests a (business-rule driven) file name for a file to download.

If the user clicks OK, the file is downloaded, and the popup should close. Cancel just closes it. This is all driven by code behind:

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();
}

The problem is that Response.End(); kills all action (well, as it should), so the modal window itself doesn't close.

What would be the right approach? I was thinking of using an iframe and calling its Response to send the file, but I'd like to confirm if this is appropriate or if there is something better.

ASPX declaration:

<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>

解决方案

You can set OkControlID property of extender to hide popup on Ok button click and OnOkScript property for custom javascript function which will force postback from Ok button (modal popup extender prevents postback from Ok and Cancel controls).

<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>

这篇关于发送文件后,关闭ModelPopup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆