我正在撰寫一個使用 SelectPDF 庫加載 PDF 的頁面。我遇到的問題是渲染 PDF 的加載時間。從 HTML 呈現 PDF 大約需要 2-3 秒。
這不是問題,但我想禁用在加載期間用于創建 pdf 的按鈕。因為如果用戶不耐煩并再次單擊按鈕,則該程序將重新開始。
我認為使用腳本管理器和更新面板可以讓我做到這一點,但我似乎無法弄清楚。我有部分頁面呈現和更新模式設定為條件,但是當我從我的代碼呼叫 update() 方法時,沒有任何反應。在 PDF 完成渲染之前,該按鈕不會禁用。
我想強制按鈕啟用狀態在 PDF 開始渲染之前更新,而不是之后。
這是我的代碼
ASPX 頁面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<form id="frmMain" runat="server">
<asp:ScriptManager ID="smMain" EnablePartialRendering="true" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="upMain" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<table id="form">
<tr>
<td>Page Header: </td>
<td><asp:TextBox ID="txtPageHeader" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Page Content: </td>
<td><asp:TextBox ID="txtPageContent" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="cmdLoadPDF" Text="Create PDF" OnClientClick="doLoading();" runat="server" /></td>
</tr>
<tr>
<td><asp:Label ID="lblLoading" Text="Loading" Visible="false" runat="server"></asp:Label></td>
</tr>
</table>
<iframe id="ifPDF" visible="false" runat="server"></iframe>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
還有我的 C# 代碼隱藏檔案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Threading.Tasks;
using SelectPdf;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ifPDF.Attributes["class"] = "pdf_view";
cmdLoadPDF.Click = new EventHandler(Click_Event);
}
protected void Click_Event(object sender, EventArgs e)
{
cmdLoadPDF.Enabled = false;
lblLoading.Visible = true;
upMain.Update();
ifPDF.Visible = true;
string pdf_string = File.ReadAllText(Server.MapPath("template.html"));
pdf_string = pdf_string.Replace("{{page_header}}", txtPageHeader.Text);
pdf_string = pdf_string.Replace("{{page_content}}", txtPageContent.Text);
HtmlToPdf make_pdf = new HtmlToPdf();
PdfDocument my_doc = make_pdf.ConvertHtmlString(pdf_string);
byte[] doc_array = my_doc.Save();
my_doc.Close();
string b64_doc = Convert.ToBase64String(doc_array, 0, doc_array.Length);
string pdf_src = $"data:application/pdf;base64,{b64_doc}";
ifPDF.Attributes["Src"] = pdf_src;
}
}
如果你們能幫助我,將不勝感激。
uj5u.com熱心網友回復:
為什么不在單擊時隱藏或禁用該按鈕?
你有這個:
<td><asp:Button ID="cmdLoadPDF" Text="Create PDF"
OnClientClick="doLoading();" runat="server" /></td>
所以,這樣說:
<td><asp:Button ID="cmdLoadPDF" Text="Create PDF"
OnClientClick="doLoading(this);" runat="server" /></td>
進而
<script>
function doLoading(e) {
btn = $(e)
btn.hide()
// rest of your code here
}
</script>
因此,該更新面板的行為實際上就像整個頁面回傳一樣。單擊按鈕將隱藏按鈕 - 更新面板回發。Crunch Crunch Crnunch,然后完成后,更新面板將回傳到客戶端,并重新加載 - 然后重新加載也會重新顯示按鈕!!!
因此,只需將“this”(按鈕)傳遞給 js 例程。隱藏按鈕 - 它會一直隱藏,直到后面的代碼完成,當更新面板的新副本回來時 - 按鈕將再次顯示(按鈕的隱藏設定不會持續)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/373542.html
上一篇:什么代碼語言使用這些檔案和結構?
