简体   繁体   English

window.print onclientclick防止代码背后的onclick

[英]window.print onclientclick preventing onclick in codebehind

I have a page that needs to do a postback via OnClick to the page after window.print() is called. 我有一个页面需要在调用window.print()之后通过OnClick回发到该页面。 I have spent a couple of hours looking for a solution, but so far haven't found the right combination. 我已经花了几个小时来寻找解决方案,但是到目前为止,还没有找到合适的组合。 I set up a test page to make sure there was no other javascript interfering. 我设置了一个测试页,以确保没有其他JavaScript干扰。 Here is the code 这是代码

<asp:Button ID="Print" runat="server" Text="Print" OnClientClick = "window.print();return true;" UseSubmitBehavior="false" OnClick="Print_Click" />

protected void Print_Click(object sender, EventArgs e)
{
   Response.Write("Clicked");
}

That is the rendered button as in the browser: 那是在浏览器中的渲染按钮:

<input id="Print" class="button" type="button" onclick="window.print();return true;__doPostBack('Print','')" value="Print" name="Print">

The postback is not happening and the print_click is not being called. 回发未发生,并且未调用print_click。 I have also tried adding the onclientclick in the code behind on page_load 我也尝试过在page_load后面的代码中添加onclientclick

I would appreciate any help- I think I have looked at 100+ search results. 我将不胜感激-我想我已经查看了100多个搜索结果。 Thanks, Judy 谢谢朱迪

<asp:Button ID="Print" runat="server" Text="Print" UseSubmitBehavior="false" OnClick="Print_Click" />

Have you tried removing the OnClickClick to see if the postback is triggered correctly without this? 您是否尝试过删除OnClickClick来查看是否正确触发了回发?

If this works then I would try adding the clientside print manually: 如果可行,那么我将尝试手动添加客户端打印:

var originalClick = $('#Print').click();

$('#Print').click(function(){
   window.print();
   originalClick();
});

I think the protected level is fine, but maybe try public just in case. 我认为protected级别还不错,但也许以防万一,请尝试public

I think part of the problem here was with Firefox. 我认为部分问题与Firefox有关。 A colleague gave me code that was a little different and it didn't work in FF, but it works in IE and Chrome. 一位同事给我的代码有些不同,它在FF中不起作用,但在IE和Chrome中有效。 Here is what he gave me- part of it involved hiding the function handlePrintClick() { window.print(); 这是他给我的-其中一部分涉及隐藏功能handlePrintClick(){window.print(); var buttonId = "<%= Print.ClientID %>"; var buttonId =“ <%= Print.ClientID%>”; var buttonElm = document.getElementById(buttonId); var buttonElm = document.getElementById(buttonId); buttonElm.click(); buttonElm.click(); } }

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

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