Как предотвратить исчезновение модальных Bootstrap при использовании FileUpload и UpdatePanel - PullRequest
0 голосов
/ 25 февраля 2020

Я написал код, в котором я пытаюсь использовать FileUpload и UpdatePanel внутри Bootstrap 4.4 Popup Modal, в любом случае все работает, за исключением того, что я не могу получить имя файла, когда я используя свойство .HasFile. Я всегда получаю false. Мне нужно загрузить ссылку на файл в базу данных, а также загрузить файл на сервер, поскольку FileUpload не работает, я также не могу загрузить файл с помощью FileUpload.SaveAs(...)

Я пробовал это но у меня это не сработало.

ASPX

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

<script type="text/javascript">
    function ShowPopup() {
        $("#modalPayments").modal("show");
    }
</script>


<asp:Button ID="btnLuncher" Text="Add Payment" runat="server" CssClass="btn btn-primary" OnClick="btnLuncher_Click" />

<!-- Bootstrap modal -->
<div id="modalPayments" class="modal fade bd-example-modal-xl" tabindex="-1" role="dialog" aria-labelledby="myExtraLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title h4" id="myExtraLargeModalLabel">Add Payments</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="form-row">
                    <div class="form-group col-auto">
                        <label for="txtPayment">Payment</label>
                        <asp:TextBox ID="txtPayment" class="form-control mb-2" TextMode="Number" runat="server"></asp:TextBox>
                    </div>
                    <div class="form-group col-auto">
                        <label for="txtDate">Payment date</label>
                        <asp:TextBox ID="txtDate" class="form-control mb-2" TextMode="Date" runat="server"></asp:TextBox>
                    </div>
                    <div class="form-group col">
                        <label for="fuPayment">Attach a copy of document</label>
                        <asp:FileUpload ID="fuPayment" class="form-control mb-2" runat="server" />
                    </div>
                </div>

                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <p>
                        <asp:Label ID="StatusLabel" runat="server" ></asp:Label>
                        </p>
                        <p>
                            <asp:Button ID="btnAddNewPayment" class="btn btn-dark" runat="server" Text="Add New" OnClick="btnAddNewPayment_Click" />
                        </p>
                        <asp:GridView ID="gvPayments" CssClass="table table-hover" Style="border-color: #fff;" runat="server" AutoGenerateColumns="False" DataKeyNames="CID,PSN" DataSourceID="sdsPayments">
                            <Columns>
                                <asp:BoundField DataField="CID" HeaderText="Contract" ReadOnly="True" SortExpression="CID" />
                                <asp:BoundField DataField="PSN" HeaderText="No." InsertVisible="False" ReadOnly="True" SortExpression="PSN" />
                                <asp:BoundField DataField="Payment" HeaderText="Payment" SortExpression="Payment" />
                                <asp:BoundField DataField="DID" HeaderText="PaymentDate" SortExpression="DID" />
                                <asp:BoundField DataField="Attachment" HeaderText="Attachment" SortExpression="Attachment" />
                                <asp:BoundField DataField="Post" HeaderText="Post Date" SortExpression="Post" />
                            </Columns>
                        </asp:GridView>

                        <asp:SqlDataSource ID="sdsPayments" runat="server"
                            ConnectionString="<%$ ConnectionStrings:RentaliumConnectionString %>"
                            InsertCommand="INSERT INTO [tblPayment] ([CID], [Payment], [DID], [Attachment], [Post]) VALUES (@CID, @Payment, @DID, @Attachment, @Post)"
                            SelectCommand="SELECT * FROM [tblPayment]">
                            <InsertParameters>
                                <asp:SessionParameter Name="CID" SessionField="XDC" Type="String" />
                                <asp:ControlParameter ControlID="txtPayment" Name="Payment" PropertyName="Text" Type="String" />
                                <asp:ControlParameter ControlID="txtDate" Name="DID" PropertyName="Text" Type="String" />
                                <asp:SessionParameter Name="Attachment" SessionField="PaymentDocumentPath" Type="String" />
                                <asp:SessionParameter Name="Post" SessionField="PostDate" Type="String" />
                            </InsertParameters>
                        </asp:SqlDataSource>

                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnAddNewPayment" EventName="Click" />
                        <asp:AsyncPostBackTrigger ControlID="gvPayments" EventName="DataBound" />
                    </Triggers>
                </asp:UpdatePanel>
            </div>
        </div>
    </div>
</div>

Код позади

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["Contract"] = "5448";
    }

    protected void btnAddNewPayment_Click(object sender, EventArgs e)
    {

        if (fuPayment.HasFile)
        {
            try
            {
                if (fuPayment.PostedFile.ContentType == "application/pdf")
                {
                    if (fuPayment.PostedFile.ContentLength < 102400)
                    {
                        string filename = Path.GetFileName(fuPayment.FileName);
                        Session["PaymentDocumentPath"] = Server.MapPath("~/Files/Payments/") + filename;
                        StatusLabel.Text = "Upload status: File uploaded!";
                        Session["PostDate"] = DateTime.Now.ToShortDateString();
                        sdsPayments.Insert();
                        gvPayments.DataBind();
                    }
                    else
                    {
                        StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                    }
                }
                else
                {
                    StatusLabel.Text = "Upload status: Only PDF files are accepted!";
                }
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }else
            StatusLabel.Text = "Upload status: You have not selected any files";

    }

    protected void btnLuncher_Click(object sender, EventArgs e)
    {
        gvPayments.DataBind();
        ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup();", true);

    }
}

SQlServer Table (tblPayment)

CID         nvarchar(50)
PSN         int      
Payment     nvarchar(50)    
DID         nvarchar(10)    
Attachment  nvarchar(MAX)   
Post        nvarchar(10)
...