C# 在打印GridView时隐藏GridView的两列Asp.net 函数printform(){ var printContent=document.getElementById(“”); var windowUrl=“关于:空白”; var uniqueName=新日期(); var windowName=“Print”+uniqueName.getTime(); var printWindow=window.open(windowUrl,windowName,“左=50000,顶=50000,宽=0,高=0”); printWindow.document.write(printContent.innerHTML); printWindow.document.close(); printWindow.focus(); printWindow.print(); printWindow.close(); } 函数HidColumn(){ //col_num=document.getElementById(“按钮”).value; 行=document.getElementById(“”)行; 对于(i=0;i

C# 在打印GridView时隐藏GridView的两列Asp.net 函数printform(){ var printContent=document.getElementById(“”); var windowUrl=“关于:空白”; var uniqueName=新日期(); var windowName=“Print”+uniqueName.getTime(); var printWindow=window.open(windowUrl,windowName,“左=50000,顶=50000,宽=0,高=0”); printWindow.document.write(printContent.innerHTML); printWindow.document.close(); printWindow.focus(); printWindow.print(); printWindow.close(); } 函数HidColumn(){ //col_num=document.getElementById(“按钮”).value; 行=document.getElementById(“”)行; 对于(i=0;i,c#,asp.net,gridview,C#,Asp.net,Gridview,但是,当我单击ImageButton时,什么也没有发生。这行没有意义:var b=HidColumn() 函数HidColumn不返回任何内容。正如我所说,我同意steve的回答,您应该修改函数HidColumn以返回true或false。 还有一点我想说的是,如果您从clicked()返回false,则不会发生postback,否则它将调用服务器上的ImageButton2\u Click事件 function printform() { var printContent =

但是,当我单击ImageButton时,什么也没有发生。这行没有意义:
var b=HidColumn()


函数HidColumn不返回任何内容。

正如我所说,我同意steve的回答,您应该修改函数HidColumn以返回true或false。 还有一点我想说的是,如果您从clicked()返回false,则不会发生
postback
,否则它将调用服务器上的
ImageButton2\u Click
事件

 function printform() {
        var printContent = document.getElementById("<%= PrintPanelID.ClientID %>");

        var windowUrl = "about:blank";
        var uniqueName = new Date();
        var windowName = "Print" + uniqueName.getTime();
        var printWindow = window.open(windowUrl, windowName, "left=50000,top=50000,width=0,height=0");
        printWindow.document.write(printContent.innerHTML);
        printWindow.document.close();
        printWindow.focus();
        printWindow.print();
        printWindow.close();
    }

    function HidColumn() {

        // col_num = document.getElementById("Button").value;
        rows = document.getElementById("<%= GV1.ClientID %>").rows;
        for (i = 0; i < rows.length; i++) {
            rows[i].cells[8].style.display = "none";
        }

        for (j = 0; j < rows.length; j++) {
            rows[j].cells[9].style.display = "none";
        }
    }

    // change logic to suit taste
    function clicked() {
        var b = HidColumn();
        if (b)
            printform()
        return b;
    }


  <asp:ImageButton ID="ImageButton2" runat="server" ImageAlign="Right" ImageUrl="images/printer.jpg"
                    Style="margin-left: 5px; margin-right: 15px" OnClick="ImageButton2_Click" Width="36px"
                    OnClientClick="return clicked()" Visible="false" />
函数HidColumn(){
//col_num=document.getElementById(“按钮”).value;
行=document.getElementById(“”)行;
对于(i=0;i
更新:- 您已将控件可见性设置为False,因此将不会渲染该控件。 因此,您无法在javascript中获取该元素,因为该控件没有HTML。 如果要隐藏控件,请使用Javascript:-

function HidColumn() {

        // col_num = document.getElementById("Button").value;
        rows = document.getElementById("<%= GV1.ClientID %>").rows;
        for (i = 0; i < rows.length; i++) {
            rows[i].cells[8].style.display = "none";
        }

        for (j = 0; j < rows.length; j++) {
            rows[j].cells[9].style.display = "none";
        }
        if(someCondition)return true;
         else return false;
    }

只需将隐藏列代码放入如下打印功能中:

<asp:somecontrol id="ctrl" style="display:none" />
函数PrintPage(){
行=document.getElementById(“”)行;
对于(i=0;i
我同意steve的回答,您应该修改函数HidColumn()以返回true或false。。
function PrintPage() {

        rows = document.getElementById("<%= Gv1.ClientID %>").rows;
        for (i = 0; i < rows.length; i++) {
            rows[i].cells[8].style.display = "none";
            rows[i].cells[9].style.display = "none";
        }
        var printContent = document.getElementById('<%= pnlDtls.ClientID %>');
        var printWindow = window.open("All Records", "Print Panel", 'left=50000,top=50000,width=0,height=0');
        printWindow.document.write(printContent.innerHTML);
        printWindow.document.close();
        printWindow.focus();
        printWindow.print();
    }