简体   繁体   English

如何防止用户单击ASP.NET Webforms的gridview中多次启动文件下载的图像按钮?

[英]How to prevent a user from clicking an imagebutton that's starting a file download multiple times in a gridview in ASP.NET Webforms?

I have something like this in a gridview: 我在gridview中有这样的东西:

<asp:TemplateField>
<ItemStyle />
<ItemTemplate>
    <asp:ImageButton ID="btnDownloadHidden" Enabled="false" runat="server" 
        CommandName="ScaricaDocHidden" style="display:none" CommandArgument='<%#Eval("ID_MODULO") %>' />
    <asp:ImageButton ID="btnDownload" Enabled="true" runat="server" 
        CommandName="ScaricaDoc" OnClientClick="disable()" CommandArgument='<%#Eval("ID_MODULO") %>' />
</ItemTemplate>

and this in the code-behind: 而这在后面的代码中:

protected void gridViewa_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
        case "ScaricaDoc":
            {
                string id_modulo = e.CommandArgument.ToString();
                int? id_contratto = contratto.idContratto;

                byte[] pdfBytes = recuperaPDF();

                Response.ClearContent();
                Response.ClearHeaders();
                Response.Clear();

                MemoryStream ms = new MemoryStream(pdfBytes);
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=doc.pdf");
                Response.Buffer = true;
                ms.WriteTo(Response.OutputStream);
                Response.Flush();
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "testsOnPostback", "$(function() { tests(); })", true);
                //GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
                //ImageButton img = (ImageButton)gvRow.FindControl("btnScaricaDoc");
                //img.Attributes.Clear();
                //img.Attributes.Add("display", "block");
                Response.End();
                Response.Write("<script>window.close();</script>");

               break;
            }

and this javascript: 和这个JavaScript:

function disable() {
    alert('disable');
    var btn = $("input[type='image']");
    var hdn = $("input[name$='DownloadHidden']");
    var btn = $("input[name$='btnDownload']");
    btn.css('display', 'none');
    hdn.css('display', 'block');
}

function enable() {
    alert('enable');
    var btn = $("input[type='image']");
    var hdn = $("input[name$='DownloadHidden']");
    var btn = $("input[name$='btnDownload']");
    btn.css('display', 'block');
    hdn.css('display', 'none');

}

Now, where I have to execute the enable function to get it work? 现在,我必须在哪里执行启用功能才能使其正常工作? I have already see post like this: 我已经看到这样的帖子:

How do I prevent users clicking a button twice? 如何防止用户两次单击按钮?

But I've tried and not working, also using return false; 但是我已经尝试过并且没有工作,也使用return false;。 or UseSubmitBehavior="false", so I decide to use another button already disabled, as I see in another post. 或UseSubmitBehavior =“ false”,因此我决定使用已经禁用的另一个按钮,正如我在另一篇文章中看到的那样。 But I just don't know how to re-enable it. 但是我只是不知道如何重新启用它。

UPDATE: 更新:

I've seen this: file download by calling .ashx page so now all my download code is on the ashx and I don't need anymore a postback to download the file, and that's really good because the entire page is quite heavy. 我已经看到了这一点: 通过调用.ashx页面进行文件下载,所以现在我所有的下载代码都在ashx上了,我不再需要回发来下载文件了,这真的很好,因为整个页面都很繁琐。 So now my javascript is like: 所以现在我的JavaScript就像:

function disable() {
    var imageBtn = $('.js-image-button');
    //WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(imageBtn.attr('name'), "", true, "", "", false, true));
    alert('disable');
    imageBtn.attr('disabled', true);
    window.location = 'DownloadPDF.ashx';
    //alert('enable');
    //imageBtn.removeAttr('disabled') 
}

But the problem still remain, there's a way to enable the button with javascript when the download is completed? 但是问题仍然存在,有一种方法可以在下载完成后启用带有javascript的按钮?

UPDATE 2: 更新2:

I've seen this: 我已经看到了:

http://forums.hexus.net/programming-web-development/91831-javascript-wait-until-page-loaded-proceed-next-action.html http://forums.hexus.net/programming-web-development/91831-javascript-wait-until-page-loaded-proceed-next-action.html

and this: 和这个:

Javascript - How to detect if document has loaded (IE 7/Firefox 3) Javascript-如何检测文档是否已加载(IE 7 / Firefox 3)

so: 所以:

function disable() {
    var imageBtn = $('.js-image-button');
    //WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(imageBtn.attr('name'), "", true, "", "", false, true));
    //alert('disable');
    imageBtn.attr('disabled', true);
    window.location = 'Components/TabCheckList/DownloadPDF.ashx';
    checkLoad(); 
}

function checkLoad() {
    if (document.readyState === "complete") {
        var imageBtn = $('.js-image-button');
        //alert('enable');
        imageBtn.removeAttr('disabled')
    } else {
        setTimeout('checkLoad()', 1000);
    }
}

That's working also withouth .ashx using again the WebForm_DoPostBackWithOptions instead of window.location. 在没有.ashx的情况下,再次使用WebForm_DoPostBackWithOptions而不是window.location也可以正常工作。 The button is enabling when the file is generated and the broweser popup is showed, not when the download is completed, but it's good enough! 当生成文件并显示浏览器弹出窗口时,该按钮启用,而不是在下载完成时启用,但这已经足够了!

UPDATE 3 更新3

It's not working on firefox, it always give me readyState = 'interactive' 它不能在Firefox上运行,它总是让我readyState ='interactive'

I assume you're doing a post back and want to prevent further clicks while the document is downloading? 我假设您正在回发邮件,并希望避免在下载文档时再次点击?

You could try keeping it a little more simple. 您可以尝试使其更简单一些。 Leave one button, give it a class name which you can select by, I assume being in a grid, there will be multiple download buttons. 留下一个按钮,给它一个可以选择的类名,我假设它位于网格中,会有多个下载按钮。

You should be able to disable the button on click by setting the disabled attribute. 您应该能够通过设置禁用属性来禁用单击按钮。

Example Here (try clicking the button multiple times): http://jsbin.com/niziko/1/edit?html,js,output 此处的示例(尝试多次单击按钮): http : //jsbin.com/niziko/1/edit?html,js,output

Still assuming this is a post-back scenario, once the server responds back to the client, the button should be reset to it's normal state. 仍然假设这是一种回发方案,一旦服务器对客户端做出响应,则应将按钮重置为其正常状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM