response 后刷新页面,点击按钮后,禁用该按钮

一,正常的点击按钮后,将其灰显,全部执行完毕再正常显示。

this.btnSave.Attributes.Add("onclick", "if (typeof(Page_ClientValidate)=='function' && Page_ClientValidate() == false)return false;document.all('" + btnSave.ClientID + "').disabled=true;if(typeof(__doPostBack)=='function')" + this.GetPostBackEventReference(btnSave) + ";");

btnSave为按钮ID。

二,

response 后刷新页面

先要求点按钮生成文件,同时按钮变灰,文件生成好直接打开该文件,按钮可用。

注意:internet安全设置,

下载——文件下载(启用)

文件下载——文件下载的自动提示(启用)

一、第一种方法,用iframe:

1,给按钮增加点击后变灰,页面刷新变可用属性。

打开文件代码如下:

 //写出文件
        System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
        HttpResponse response = HttpContext.Current.Response;
        response.HeaderEncoding = encoding;
        response.Charset = encoding.HeaderName;
        response.Clear();
        response.ContentEncoding = encoding;
        response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(pdfFile.Name));
        response.AddHeader("Content-Length", pdfFile.Length.ToString());
        response.ContentType = "application/pdf";
        response.WriteFile(pdfFile.FullName);
        HttpContext.Current.ApplicationInstance.CompleteRequest();
打开文件

要弹出文件,页面请求会立刻结束。所以这时你把按钮的任何控制都不会执行。

2,aspx页面:增加一个iframe

 <iframe id="ifrm_popMessage" src="" frameborder="0" scrolling="no" align="middle"
        style="position: absolute; z-index: 100; display: none; border: 1px #62A3D2 solid;">
    </iframe>

3,aspx页面:增加JS脚本:

////打开文件
        function OpenPdfFile() {
            document.getElementById('ifrm_popMessage').src = "OpenPdfFile.aspx?Rnd" + Math.random();
        }

 4,生成文件结束后增加如下代码:

   

 ScriptManager.RegisterStartupScript(this.Page, ClientScript.GetType(), "myscript", "<script>OpenPdfFile();</script>", false);       

5,OpenPdfFile 页面代码:

/// <summary>
/// 用来生成PDF文件
/// </summary>
public partial class Admin_IEData_OpenPdfFile : System.Web.UI.Page
{
    /// <summary>
    /// 页面加载
    /// </summary>   
    protected void Page_Load(object sender, EventArgs e)
    {     
            DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/File/"));
            string strFileName = string.Format("{0}({1}){2}.pdf", di.FullName, SessionOperate.getLoginName(), "ddpdf");            
            FileInfo pdfFile = new System.IO.FileInfo(strFileName);
            WriteOutFile(pdfFile);        
    }

    #region 写出文件
    /// <summary>
    /// 写出文件
    /// </summary>
    /// <param name="pdfFile">文件对象</param>
    /// <returns>返回是否成功</returns>
    protected bool WriteOutFile(FileInfo pdfFile)
    {
        //写出文件
        System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
        HttpResponse response = HttpContext.Current.Response;
        response.HeaderEncoding = encoding;
        response.Charset = encoding.HeaderName;
        response.Clear();
        response.ContentEncoding = encoding;
        response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(pdfFile.Name));
        response.AddHeader("Content-Length", pdfFile.Length.ToString());
        response.ContentType = "application/pdf";
        response.WriteFile(pdfFile.FullName);
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        return true;
    }
    #endregion
}
Iframe页面代码

 二、这个方法比较好,再加一个隐藏按钮,点打印,把按钮变灰(加按钮属性实现),然后显示一个隐藏的字(正在打印……)结束后按钮恢复,字隐藏(加按钮属性实现)在打印后台代码里增加JS触发隐藏按钮onclik事件,隐藏按钮事件里写加载打印文件代码。

1、前台代码

<div style="text-align: center; margin-top: 5px;">  
            <cc1:JETButton ID="btnOk" runat="server" Text="打印" OnClick="btnOk_Click" />
            <div id="exporting" style="display:none;"><br /><font color="#ff0000" size="4.5px">数据正在打印中,请稍等……</font></div>
  </div>
<div style="display:none;">
    <cc1:JETButton ID="btnDownload" runat="server" Text="下载" CssClass="hide" OnClick="btnDownload_Click" />
</div>            
前台代码

2、 void Page_Load(object sender, EventArgs e)

this.btnOk.Attributes.Add("onclick", "if (typeof(Page_ClientValidate)=='function' && Page_ClientValidate() == false)return false;document.getElementById('" + btnOk.ClientID + "').disabled=true;document.getElementById('exporting').style.display='block';if(typeof(__doPostBack)=='function'){" + this.GetPostBackEventReference(btnOk) + ";}");

3、打印按钮onclick代码

 //生成打印
ClientScript.RegisterStartupScript(this.GetType(), "exported", "document.getElementById('" + btnOk.ClientID + "').disabled=false;document.getElementById('exporting').style.display='none';document.getElementById('" + btnDownload.ClientID + "').click();", true);
打印按钮onclick代码

4、点击下载按钮

/// <summary>
    /// 点击下载按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        //TODO:此处写下载打印合并的PDF
        DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/File/ExportTemplate/ExcelTmp/"));
        string strFileName = string.Format("{0}({1}){2}.pdf", di.FullName, SessionOperate.getLoginName(), "pdf文件");
        FileInfo pdfFile = new System.IO.FileInfo(strFileName);
        WriteOutFile(pdfFile);
    }
点击下载按钮
原文地址:https://www.cnblogs.com/xbding/p/3816968.html