Javascript不会打印我的iFrame

时间:2013-11-14 08:03:22

标签: c# javascript asp.net linq

我正在运行一个C#asp.net网站。收银员通过在销售页面上的文本框中输入金额来生成凭证。

当单击生成按钮时,它会调用DB来输入和检索信息,一旦完成所有数据库插入,这个信息就会传递给iFrame,只要iFrame visability = true,应打印iFrame的内容。我正在使用javascript打印聚焦的iFrame。它曾经工作,但现在只是停止工作没有明显的原因。

请有人帮我找到问题。

销售按钮点击

rotected void btnVoucherGenerate_Click(object sender, EventArgs e)
    {
        if (Session["LoggedMemberID"] == null)
        {
            Session.Clear();
            Response.Redirect("~/Login.aspx");
        }
        else
        {
            //Session.Remove("VoucherInfo");
            Cashiers _cashier = (Cashiers)Session["CashierInfo"];
            if (tbGeneratePIN.Text == string.Empty)
            {
                lblVoucherPIN.ForeColor = System.Drawing.Color.Red;
                lblVoucherPIN.Text = "Please insert a amount for the voucher";
            }
            else
            {
                if (Convert.ToInt32(tbGeneratePIN.Text) < 5)
                {
                    lblVoucherPIN.ForeColor = System.Drawing.Color.Red;
                    lblVoucherPIN.Text = "Minimum amount is " + _cashier.currencySymbol + " 5.00";
                }
                else
                {
                    iThNkContext.Prepaid pre = new iThNkContext.Prepaid();

                    String newID = "";
                    newID = Guid.NewGuid().ToString().Replace("-", "");
                    newID = newID.Substring(newID.Length - 16);
                    newID = newID.ToUpper();

                    // Add PIN to Prepaid DB
                    try
                    {
                        btnVoucherGenerate.Visible = false;
                        btnGenerateAnother.Visible = true;
                        tbGeneratePIN.BackColor = Color.LightYellow;
                        tbGeneratePIN.Enabled = false;


                        pre.CurrencyID = _cashier.currencyID;
                        pre.AccountID = _cashier.accountID;
                        pre.Amount = Convert.ToDecimal(tbGeneratePIN.Text);
                        pre.SerialNumber = ("_");
                        pre.Pin = newID;
                        pre.TransactionTypeID = Convert.ToInt32(ConfigurationManager.AppSettings["Voucher"]);
                        pre.SalesUserID = _cashier.userID;
                        pre.SiteID = _cashier.siteID;
                        pre.SalesDate = DateTime.Now;
                        pre.IsCancelled = false;
                        pre.IsPrinted = false;
                        pre.IsActivated = true;
                        pre.IsRedeemed = false;
                        pre.IsActive = true;

                        db.Prepaids.InsertOnSubmit(pre);
                        db.SubmitChanges();

                        pre.SerialNumber = Convert.ToString(pre.PrepaidID).PadLeft(9, '0');
                        db.SubmitChanges();

                        String SegmentedOTP = newID;
                        SegmentedOTP = SegmentedOTP.Substring(0, 4) + "-" +
                                       SegmentedOTP.Substring(4, 4) + "-" +
                                       SegmentedOTP.Substring(8, 4) + "-" +
                                       SegmentedOTP.Substring(12, 4);

                        // Log successful voucher sale.
                        _logs.successfulVoucherSales(_cashier.accountID, _cashier.siteID, _cashier.userID, pre.SerialNumber, pre.Pin, pre.Amount, _cashier.username);
                        Voucher _voucher = new Voucher();

                        _voucher.prepaidID = pre.PrepaidID;
                        _voucher.amount = pre.Amount;
                        _voucher.serialNumber = pre.SerialNumber;
                        _voucher.segmentedOTP = SegmentedOTP;
                        _voucher.accountTerms = pre.Account.AccountTerms.ToString();
                        _voucher.isPrinted = false;

                        Session["VoucherInfo"] = _voucher;

                        // update the database and set isPrinted = 1
                        db.SubmitChanges();
                        tbGeneratePIN.Text = string.Empty;

                        myIFrame.Visible = true;
                        pre.IsPrinted = true;
                        db.SubmitChanges();

                        lblVoucherPIN.ForeColor = System.Drawing.Color.Green;
                        lblVoucherPIN.Text = "Successful";
                    }
                    catch (Exception ex)
                    {
                        lblVoucherPIN.ForeColor = System.Drawing.Color.Red;
                        lblVoucherPIN.Text = ex.Message;
                    }
                }
            }
        }
    }

APSX

<body id="Body">

<form id="form1" runat="server">

<link href="styles/Voucher.css" rel="stylesheet" type="text/css" />

 <script type="text/javascript" >

     function PrintVoucher() {
         window.focus();
         window.print();
         return;
     }

</script>
    <table id="PrintTable" runat="server">
        <tr align="center" > 
            <td colspan="2" style="border-bottom:0.1px solid black;padding-bottom:5px;margin-bottom:5px;" >
                <asp:Image ID="Logo" runat="server" Height="41px" ImageUrl="~/images/Logo.png" 
                    Width="247px" />
            </td> 
       </tr>
       <tr>
            <td colspan="2" style="line-height:8px;padding:5px 0;" >
                <asp:Label ID="label1" runat="server" Text="SERIAL NUMBER:" Font-Names="Consolas" Font-Size="14px"></asp:Label>
            </td>
        </tr>
        <tr>
             <td colspan="2" style="line-height:12px;padding-bottom:12px;">
                <asp:Label ID="lblSerialNumber" runat="server" Font-Names="Consolas" Font-Size="24px"></asp:Label>
            </td>
       </tr>
       <tr>
            <td colspan="2" style="line-height:8px;padding-bottom:5px;">
                <asp:Label ID="label2" runat="server" Text="PIN:" Font-Names="Consolas" Font-Size="14px"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="line-height:12px;padding-bottom:12px;">
                <asp:Label ID="lblPIN" runat="server" Font-Names="Consolas" Font-Size="24px"></asp:Label>
            </td>
       </tr>
       <tr> 
            <td colspan="2" style="line-height:8px;padding-bottom:5px;">
                <asp:Label ID="label3" runat="server" Text="AMOUNT:" Font-Names="Consolas" Font-Size="14px"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="border-bottom:0.7px solid black;line-height:12px;padding-bottom:8px;">
                <asp:Label ID="lblAmount" runat="server" Font-Names="Consolas" Font-Size="24px"></asp:Label>
            </td>
       </tr>
        <tr>
            <td style="line-height:8px;" class="style1">
                <asp:Label ID="label5" runat="server" Text="Account: " Font-Names="Arial" Font-Size="10px"></asp:Label>
            </td>
            <td style="line-height:8px;height:0px;">
                    <asp:Label ID="lblAccount" runat="server" Text="Supabets South Africa" Font-Names="Arial" Font-Size="10px"></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="line-height:8px;" class="style2">
                <asp:Label ID="label7" runat="server" Text="Site: " Font-Names="Arial" Font-Size="10px"></asp:Label>
            </td>
            <td style="line-height:8px;height:0px;">
                    <asp:Label ID="lblSite" runat="server" Font-Names="Arial" Font-Size="10px"></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="line-height:8px;" class="style2">
                <asp:Label ID="label6" runat="server" Text="Cashier: " Font-Names="Arial" Font-Size="10px"></asp:Label>
            </td>
            <td style="line-height:8px;height:0px;">
                    <asp:Label ID="lblCashier" runat="server" Font-Names="Arial" Font-Size="10px"></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="line-height:8px;padding-bottom:3px;" class="style1">
                <asp:Label ID="label8" runat="server" Text="Date: " Font-Names="Arial" Font-Size="10px"></asp:Label>
            </td>
            <td style="line-height:8px;padding-bottom:3px;">
                    <asp:Label ID="lblDate" runat="server" Font-Names="Arial" Font-Size="10px"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="border-bottom:1.2px solid black;font-weight:bold; font-family:Consolas;font-size:20px;line-height:12px">
            </td>
        </tr>
    </table>
    <%=Clients %>

</form>

凭证iFrame代码

public partial class VoucherInformation : System.Web.UI.Page
{

    public string Clients { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        Voucher _voucher = (Voucher)Session["VoucherInfo"];
        try
        {
            Cashiers _cashier = (Cashiers)Session["CashierInfo"];
            if (!_voucher.isPrinted)
            {
                if (!String.IsNullOrEmpty(_voucher.accountTerms))
                {
                    Clients = _voucher.accountTerms;
                }
                else
                {
                    Clients = "";
                }

                lblAmount.Text = _cashier.currencySymbol + " " + _voucher.amount + ".00";
                lblSerialNumber.Text = _voucher.serialNumber;
                lblPIN.Text = _voucher.segmentedOTP;
                lblSite.Text = _cashier.siteName;
                lblAccount.Text = _cashier.accountName;
                lblCashier.Text = _cashier.username;
                lblDate.Text = Convert.ToString(DateTime.Now);

                Page.ClientScript.RegisterStartupScript(this.GetType(), "Print", "PrintVoucher();", true);

                _voucher.isPrinted = true;
            }
        }
        catch (Exception)
        {
            _voucher.isPrinted = false;
        }
    }
}  

0 个答案:

没有答案