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

时间:2016-06-21 22:27:15

标签: javascript c# asp.net button updatepanel

我的UpdatePanel页面上的ASP.NET内有一个按钮。这是我的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>

当我尝试停用按钮OnClientClick时,它不会PostBack

以下是我的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>

这是我背后的代码。我需要在背面做一些东西,并在完成后打开新窗口:

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

请告知我如何禁用我的按钮并在后面找到代码。

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用setTimeout异步禁用该按钮:

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

通过在document.getElementById中使用PDFClick关键字传递对按钮元素的引用,可以从this移除对OnClientClick的调用:

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

答案 1 :(得分:0)

在按钮上添加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" />