0

我有一个 javascript 函数,可以在 tabcontainer 上打印出活动网格(取决于活动选项卡)

当我只使用客户端调用我的打印功能时,我可以打印。

相反,现在我想使用我的 asp.net 菜单并添加了打印功能。

我想按下这个按钮来调用我的javascript。

正在调用 c# 代码,但 javascript 只是显示尚不存在网格。

页面完全加载后是否必须调用javascript?如果是这样怎么办?

谢谢达摩

Javascript

<script type="text/javascript">
    function PrintGridData() {
        //var Tab_Name = $(".ajax__tab_active").first().attr('id');
        alert($(".ajax__tab_active").first().attr('id'));
        switch ($(".ajax__tab_active").first().attr('id')) // Switch based on the on active Tab ID
        {
            case 'TabContainerMain_Tab_Monitor_tab':
                var printContent = document.getElementById('<%= GridViewMain.ClientID %>');
                break;
            default:
                alert('Printing not supported');
                return;
        }
        if (printContent)  // See if the Grid Exists First
        {
            if (printContent.rows.length > 0) { // See if the Grid contains any rows

                var windowUrl = 'about:blank';
                var UserLoggedIn = $("#lblUser").text()
                var now = new Date();
                var strDateTime = [[AddZero(now.getDate()), AddZero(now.getMonth() + 1), now.getFullYear()].join("/"), [AddZero(now.getHours()), AddZero(now.getMinutes())].join(":"), now.getHours() >= 12 ? "PM" : "AM"].join(" ");
                var windowName = 'Report';
                var AuditPrintDetail = 'Report ' + UserLoggedIn + " " + strDateTime;
                var WinPrint = window.open(windowUrl, windowName, 'left=300,top=300,right=500,bottom=500,width=1000,height=500');
                WinPrint.document.write('<' + 'html' + '><head><link href="assets/css/Main.css" rel="stylesheet" type="text/css" />     <title>' + AuditPrintDetail + '</title> </head><' + 'body  style="background:none !important"' + '>');
                WinPrint.document.write(printContent.outerHTML);
                WinPrint.document.write('<' + '/body' + '><' + '/html' + '>');
                WinPrint.document.close();
                WinPrint.focus();
                WinPrint.print();
                WinPrint.close();
            }
            else {alert("No Results to print!");}
        }
        else {alert("No Grid to print!");}
    }

    function AddZero(num) {return (num >= 0 && num < 10) ? "0" + num : num + "";}
</script>

标记

<div id="DivMenu">
                    <asp:Menu CssClass="Menu" ID="MenuMain" runat="server" Orientation="Horizontal" OnMenuItemClick="MenuMain_MenuItemClick">
                        <Items>
                            <asp:MenuItem Text="System" Value="System">
                                <asp:MenuItem Text="Logout" Value="Logout"></asp:MenuItem>
                                <asp:MenuItem Text="Print" Value="Print"></asp:MenuItem>
                            </asp:MenuItem>
                            <asp:MenuItem Text="Help" Value="Help">
                                <asp:MenuItem Text="About" Value="About"></asp:MenuItem>
                            </asp:MenuItem>
                        </Items>
                        <StaticHoverStyle BackColor="White" ForeColor="Black" />
                        <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
                        <StaticSelectedStyle BackColor="White" />
                    </asp:Menu>
                </div>

C#

  protected void MenuMain_MenuItemClick(object sender, MenuEventArgs e)
        {

                if (e.Item.Text == "About")
                {
                    AboutModal.Show();


                }


                if (e.Item.Text == "Logout")
                {
                    //Log the user out

                    FormsAuthentication.SignOut();
                    FormsAuthentication.RedirectToLoginPage();
                }


                if (e.Item.Text == "Print")
                {


                    ScriptManager.RegisterStartupScript(Page, GetType(), "JsStatus", "PrintGridData();", true);


                }
            }
4

0 回答 0