首页 > 解决方案 > 我想在我的 PDF 文件下载后显示一个模式弹出窗口

问题描述

我想在我的 pdf 成功下载后显示一个模式弹出窗口,一切正常,但在 response.end 之后,因为你知道没有代码被执行我已经尝试使用 scriptmanager 打开模式但没有成功。请帮助任何帮助是有益的。

我认为在 response.end() 之后没有代码被执行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HotelBAL;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Web.Services;
using System.Web.Script.Services;

namespace HotelReservation.UserView
        {
            public partial class CancelBooking : System.Web.UI.Page
            {
                static int uid = 0;

                protected void Page_Load(object sender, EventArgs e)
                {
                    if (Session["UName"] == null)
                    {
                        Response.Redirect("../Views/Login.aspx");
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "stepconfirm3", "$('#loginbtn').hide();", true);
                        uid = Convert.ToInt32(Session["UId"]);  
                    }
                }

                public  void BillDownloader(int uid, int bookingId, int totalPrice)
                {
                    try
                    {
                        //PdfPTable pdfTable = 
                        DataTable dt = FileUtilityBal.CancelgeneratePDF(uid, bookingId);
                        GridView objGV = new GridView();
                        objGV.AutoGenerateColumns = false;
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            BoundField boundField = new BoundField();
                            boundField.DataField = dt.Columns[i].ColumnName.ToString();
                            boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
                            objGV.Columns.Add(boundField);

                        }
                        objGV.DataSource = dt;
                        objGV.DataBind();
                        int columnsCount = objGV.Columns.Count;
                        PdfPTable pdfTable = new PdfPTable(columnsCount);

                        //Header
                        BaseFont btnColumnHeader = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                        Font fntColumnHeader = new Font(btnColumnHeader, 10, 1);
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            PdfPCell cell = new PdfPCell();
                            cell.AddElement(new Chunk(dt.Columns[i].ColumnName.ToUpper(), fntColumnHeader));
                            pdfTable.AddCell(cell);
                        }

                        //DataTable
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                pdfTable.AddCell(dt.Rows[i][j].ToString());
                            }
                        }

                        Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
                        PdfWriter.GetInstance(pdfDocument, HttpContext.Current.ApplicationInstance.Response.OutputStream);
                        pdfDocument.Open();

                        var FontColour = new BaseColor(192, 192, 192);
                        var Calibri8 = FontFactory.GetFont("CALIBRI_BOLD", 35, FontColour);
                        Paragraph paragraph = new Paragraph("Cancellation Details", Calibri8);
                        paragraph.Alignment = Element.ALIGN_CENTER;
                    paragraph.SpacingAfter = 10f;
                    iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(HttpContext.Current.ApplicationInstance.Server.MapPath("../") + "/images/h-logo.jpg");
                    jpg.ScaleToFit(150, 150);
                    jpg.Alignment = Element.ALIGN_CENTER;
                    pdfDocument.Add(jpg);
                    pdfDocument.Add(paragraph);

                    pdfDocument.Add(pdfTable);
                    var FontColour1 = new BaseColor(192, 192, 192);
                    var Calibri9 = FontFactory.GetFont("CALIBRI_BOLD", 35, FontColour1);
                    Paragraph p7 = new Paragraph("Invoice", Calibri9);
                    p7.Alignment = Element.ALIGN_CENTER;
                    p7.SpacingAfter = 30f;
                    pdfDocument.Add(p7);
                    var FontColour2 = new BaseColor(255, 87, 51);
                    var Calibri10 = FontFactory.GetFont("CALIBRI_BOLD", 15);
                    var Calibri11 = FontFactory.GetFont("CALIBRI_BOLD", 15, FontColour2);
                    Paragraph p2 = new Paragraph("       Refund Price (50% of Total)     Rs." + (totalPrice * 0.50) + "                     ", Calibri10);
                    Paragraph p5 = new Paragraph("------------------------------------------------------------------------------------------------", Calibri10);
                    Paragraph p6 = new Paragraph("                       TOTAL           Rs." + ((totalPrice * 0.50)) + "                     ", Calibri11);
                    p2.Alignment = Element.ALIGN_CENTER;
                    p2.SpacingBefore = 30f;
                    pdfDocument.Add(p2);
                    p5.Alignment = Element.ALIGN_CENTER;
                    p5.SpacingBefore = 30f;
                    pdfDocument.Add(p5);
                    p6.Alignment = Element.ALIGN_CENTER;
                    p6.SpacingBefore = 10f;
                    pdfDocument.Add(p6);
                    Paragraph p8 = new Paragraph("CANCELLATION POLICY\n• Reservations made over 11-16 Sep 2018, 28-31 Dec 2018, 1-4 Jan 2019, 8-10 Aug 2019 and 28 Dec 2019 - 4 Jan 2020 require full pre-payment and are non-cancellable, non-amendable and non-refundable. The entire period of your stay, inclusive of nights before and after the dates listed above, will be charged to your credit card upon reservation.\n• Cancellation of and/or amendments to your reservation must be made 48 hours (i.e., by 4pm Singapore time) prior to your arrival date.\n• Cancellation or amendment made within 48 hours of arrival will incur a cancellation fee of one night's room charge (inclusive of any applicable prevailing government tax).\n• In the event of no-show, a fee of one night's room charge (inclusive of any applicable prevailing government tax) will be charged to your credit card provided at the time of reservation.");
                    p8.Alignment = Element.ALIGN_LEFT;
                    p8.SpacingBefore = 20f;
                    pdfDocument.Add(p8);
                    pdfDocument.Close();
                    //.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", " $('#MyModal').modal('show') ;", true);
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("content-disposition", "attachment;filename=hotel.pdf");
                    Response.Cookies["cookie"].Value = "cookie value";
                    Response.Cookies["cookie"].Expires = DateTime.Now.AddMinutes(1); 
                    Response.Write(pdfDocument);
                    Response.Flush();
                    Response.End();
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
            }

            protected void btnSubmit_Click1(object sender, EventArgs e)
            {
                CancelBookingBal bal = new CancelBookingBal();

                int BookingID = Convert.ToInt32(bid.Value);
                int uId = Convert.ToInt32(uid);
                int amount = 0;
                if (bal.updateUser(BookingID, uId))
                {
                    amount = UserBookingBal.getBookingAmt(BookingID);

                    BillDownloader(uId, BookingID, amount);
                }
            }
        }
    }

aspx页面如下

    <%@ Page Title="" Language="C#" MasterPageFile="~/UserView/UserMasterPage.Master" AutoEventWireup="true" CodeBehind="CancelBooking.aspx.cs" Inherits="HotelReservation.UserView.CancelBooking" ClientIDMode="Static" EnableViewState="true" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">

        <style>
            .hidden { display: none; }
        </style>

        <div class="container">
            <div class="row">
                <div class="form-control">
                    <br />
                    <br />
                    <h1>Cancel Booking</h1>
                    <br />
                    <br />

                    <div class="form-group">
                        <label for="bno">Enter the booking number to cancel:</label>
                        <input type="text" class="form-control" id="bid" placeholder="Enter Number" name="bno" runat="server" />
                    </div>

                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btn btn-primary" OnClick="btnSubmit_Click1"   />
                   <%-- <button type="submit" id="cancelbtn" class="btn btn-primary" data-toggle="modal" runat="server">Submit</button>--%>
                </div>

                <div class="modal" id="MyModal">
                    <div class="modal-dialog">
                        <div class="modal-content">

                            <!-- Modal Header -->
                            <div class="modal-header">
                                <h4 class="modal-title">Modal Heading</h4>
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                            </div>

                            <!-- Modal body -->
                            <div class="modal-body">
                                You have successfully cancelled the Reservation !!!
                            </div>

                            <!-- Modal footer -->
                            <div class="modal-footer">
                                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                            </div>

                        </div>
                    </div>
                </div>

            </div>
        </div>

    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    </asp:Content>

我尝试使用脚本管理器打开模式

 ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", " $('#MyModal').modal('show') ;", true);

我想在 pdf 下载后显示弹出模式。

标签: c#asp.netitext

解决方案


如果您的引导程序的 javescript 完整并包含到页面中,则必须这样做:

<script type="text/javascript">

        function openModal() {
            $('#myModal').modal('show');
        }
</script>

.cs 代码

 ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", openModal(), true);

如果这不起作用,请检查您的引导 javascript。

这段代码:

 protected void btnSubmit_Click1(object sender, EventArgs e)
        {
            CancelBookingBal bal = new CancelBookingBal();

            int BookingID = Convert.ToInt32(bid.Value);
            int uId = Convert.ToInt32(uid);
            int amount = 0;
            if (bal.updateUser(BookingID, uId))
            {
                amount = UserBookingBal.getBookingAmt(BookingID);


               ScriptManager.RegisterStartupScript(this, GetType(), 
              "YourUniqueScriptKey", " $('#MyModal').modal('show') ;", 
              true);
            }
        }

模态按钮单击确定

protected Modalbutton_onClick(object sender, EventArgs e)
{
   BillDownloader(uId, BookingID, amount);//Provide parameter on top 
}

在这之前:

Modalbutton_onClick

您已经检查该文件是否存在。如果文件存在与否,模式必须首先抛出一条消息。


推荐阅读