简体   繁体   English

禁用按钮不会在 UpdatePanel 中提交回发

[英]Disabled button doesn't submit postback in UpdatePanel

I have a button inside UpdatePanel on my ASP.NET Page.我的ASP.NET页面上的UpdatePanel有一个按钮。 Here's my UpdatePanel这是我的更新UpdatePanel

<asp:UpdatePanel ID="UpdateToolbar" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <table>
            <tr>
                <td>
                    <div id="divPDFBtn">
                        <asp:Button ID="btnPrint" runat="server" OnClick="btnPrint_Click" ToolTip="Click to export report to PDF"
                        Width="100px" Text="Print to PDF" OnClientClick="if(PDFClick()) {return true;} else {alert('2');}" />
                    </div>
                </td>
            </tr>
        </table>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnPrint" />
    </Triggers>
</asp:UpdatePanel>

When I'm trying to disable button OnClientClick it doesn't do PostBack当我尝试禁用按钮OnClientClick它不会执行PostBack

Here's example of my PDFClick() function这是我的PDFClick()函数的示例

// Works (does post back and code behind executes))
<script type="text/javascript">
    function PDFClick() {
        document.getElementById("btnPrint").value = "Working...";
        return true;
    };
</script>

// Doesn't work (JS executes, but code behind didn't execute)
<script type="text/javascript">
    function PDFClick() {
        document.getElementById("btnPrint").value = "Working...";
        document.getElementById("btnPrint").disabled = true;
        return true;
    };
</script>

This is my code behind.这是我背后的代码。 I need to do some stuff on the back and open new window after it's done:我需要在后面做一些事情并在完成后打开新窗口:

protected void btnPrint_Click(object sender, EventArgs e)
{
    Response.Write("<script>");
    Response.Write(String.Format("window.open('{0}','_blank')", ResolveUrl("PrintPage.aspx")));
    Response.Write("</script>");
}

Please advise how I can disable my button and reach code behind.请告诉我如何禁用我的按钮并到达后面的代码。

You can disable the button asynchronously with setTimeout :您可以使用setTimeout异步禁用按钮:

<script type="text/javascript">
    function PDFClick(btnPrint) {
        btnPrint.value = "Working...";
        setTimeout(function() { btnPrint.disabled = true; }, 10);
        return true;
    };
</script>

The call to document.getElementById can be removed from PDFClick by passing a reference to the button element with the this keyword in OnClientClick :通过在OnClientClick使用this关键字传递对按钮元素的引用,可以从PDFClick删除对document.getElementById的调用:

OnClientClick="if (PDFClick(this)) { return true; } else { alert('2'); }"

在按钮上添加ClientIDMode="Static"

<asp:Button ID="btnPrint" runat="server" OnClick="btnPrint_Click" ToolTip="Click to export report to PDF" Width="100px" Text="Print to PDF" OnClientClick="if(PDFClick()) {return true;} else {alert('2');}" ClientIDMode="Static" />

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

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