我的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>");
}
请告知我如何禁用我的按钮并在后面找到代码。
谢谢!
答案 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" />