【问题标题】:asp.net c# force page reload on browser print hotkey [duplicate]asp.net c#在浏览器打印热键上强制页面重新加载[重复]
【发布时间】:2020-10-01 07:33:03
【问题描述】:

如果用户按下打印热键 (ctrl+P),有什么方法可以在我的 asp.net c# web 应用程序中运行代码?

我有一个多行 asp:TextBox,我需要计算行数以生成文档文本框的正确高度。 Atm 我有一个打印按钮,效果很好,但如果用户使用浏览器打印功能,我想运行相同的代码。

提前谢谢你

【问题讨论】:

  • 为什么不使用 Javascript 来计算行呢?那么你就不需要向服务器发出请求了。
  • 这也是可能的,但我更喜欢带有服务器请求的解决方案,因为我可以一步完成一些额外的事情。无论如何,谢谢你,感谢你的帮助。

标签: c# asp.net browser printing


【解决方案1】:

我会在 Webforms 中做这样的事情。制作一个虚拟的 ButtonLinkButton 并在按下 Control+P 时使用 jQuery 触发点击。然后在后面的代码中将window.print() 写入带有ScriptManager 的页面。

LinkBut​​ton 中的<span> 元素是注册点击所必需的。

<script>
    $(window).bind('keydown', function (e) {
        if ((e.ctrlKey || e.metaKey) && String.fromCharCode(e.which).toUpperCase() === 'P') {
            $('#LinkButton1')[0].click(); 
            e.preventDefault();
        }
    });
</script>

<asp:LinkButton ID="LinkButton1" runat="server" 
    ClientIDMode="Static" OnClick="LinkButton1_Click"><span></span></asp:LinkButton>

然后是代码

protected void LinkButton1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "PrintWindow", "window.print()", true);
}

【讨论】:

  • 非常感谢,我喜欢这个解决方案
猜你喜欢
  • 2012-11-04
  • 2015-01-05
  • 2013-01-24
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 2013-07-27
  • 2017-12-05
  • 1970-01-01
相关资源
最近更新 更多