简体   繁体   English

隐藏Asp.net时隐藏两列GridView

[英]Hiding two columns of GridView while printing it Asp.net

 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" />

However nothing is happening when i click the ImageButton 但是当我单击ImageButton时没有任何反应

This line doesn't make sense: var b = HidColumn(); 这行没有意义: var b = HidColumn();

The function HidColumn doesn't return anything. 函数HidColumn不返回任何内容。

AS I said that I agree with the steve answer and you should modify your function HidColumn to return either true or false. 正如我说过的,我同意史蒂夫的回答,您应该修改函数HidColumn以返回true或false。 One more point i would like to mention that if you return false from clicked() Then postback will not happen otherwise it will call ImageButton2_Click event on server. 还有一点我想提一下,如果您从clicked()返回false,则不会发生postback否则它将在服务器上调用ImageButton2_Click事件。

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;
    }

UPDATE:- you have set the control visibility to False , hence the control will not be rendered. 更新:-您已将控件可见性设置为False,因此将不会呈现控件。 Hence you cannot get the Element in javascript because there will be no HTML for that control. 因此,您将无法在javascript中获得Element,因为该控件将没有HTML。 if you want hide the control just use Javascript :- 如果您想隐藏控件,只需使用Javascript:-

<asp:somecontrol id="ctrl" style="display:none" />

simply put your hide column code in a print function like this: 只需将您的隐藏列代码放在这样的打印函数中:

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();
    }

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

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