如何在页面中打印div并使用一个ASP按钮调用代码隐藏 [英] How to print a div in page and call codebehind with one ASP button

查看:95
本文介绍了如何在页面中打印div并使用一个ASP按钮调用代码隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp页面包含一个div和asp按钮

按钮应该打印div内容并调用代码后面





我的代码中的问题是我用来打印div内容的方式。它更改页面代码然后打印然后返回页面代码。

所以它打印div内容并且不要调用BTNPrint_Click事件。


$ b $我怎么能这样做?



我尝试过:



这是一个用于打印div内容的javascript代码

< script language =javascripttype =text / javascript> 
函数printDiv(divID){
//获取div的HTML
var divElements = document.getElementById(divID).innerHTML;
//获取整页的HTML
var oldPage = document.body.innerHTML;

//仅使用div的HTML重置页面的HTML
document.body.innerHTML =
< html>< head>< title>< / title> < /头><身体>中+
divElements +< / body>;

//打印页面
window.print();

//恢复orignal HTML
document.body.innerHTML = oldPage;


}
< / script>







这是asp按钮

< asp:按钮ID =   BTNPrint runat =   server 
Text = 打印 OnClientClick = < span class =code-string> javascript:printDiv('div1') onclick = BTNPrint_Click />





这里是div

< div id = DIV1 > 
//一些标签和gridview
< / div>

解决方案

请参阅下面的解决方案。

javascript - 打印DIV的内容 - 堆栈溢出 [ ^ ]

I have an asp page contains a div and asp button
the button should print the div content and call code behind


the problem in my code is the way I use to print the div content. it changes the page code then print then return the page code back.
so it prints the div content and do NOT call BTNPrint_Click event.

how could I do that?

What I have tried:

this is a javascript code to print div content

<script language="javascript" type="text/javascript">
         function printDiv(divID) {
             //Get the HTML of div
             var divElements = document.getElementById(divID).innerHTML;
             //Get the HTML of whole page
             var oldPage = document.body.innerHTML;

             //Reset the page's HTML with div's HTML only
             document.body.innerHTML =
              "<html><head><title></title></head><body>" +
              divElements + "</body>";

             //Print Page
             window.print();

             //Restore orignal HTML
             document.body.innerHTML = oldPage;


         }
    </script>




this is the asp button

<asp:Button ID="BTNPrint" runat="server"
           Text="Print" OnClientClick="javascript:printDiv('div1')" onclick="BTNPrint_Click" />



and here is the div

<div id="div1">
//some labels and gridview
</div>

解决方案

Refer below solution.
javascript - Print the contents of a DIV - Stack Overflow[^]


这篇关于如何在页面中打印div并使用一个ASP按钮调用代码隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆