添加其他C#时,动态文件上载控件会删除所选文件

时间:2016-10-06 22:00:58

标签: c# asp.net multifile-uploader

我有一个表单,用户可以通过按" Add Attachment"添加附件"来添加他们想要的附件。按钮。他们应该在每次上传新文件时选择附件类型。但是,如果我选择一个文件并选择类型,然后按"添加附件"按钮,它从文件上传和文件类型下拉列表中删除我的第一个选择。我甚至使用UpdatePanel但仍然相同。 这是图片:

Upload Control Pic

这是我的C#代码:

    protected void btnAddAttachment_Click(object sender, EventArgs e)
    {
        if (ViewState["AdditionalFilesTable"] != null)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)ViewState["AdditionalFilesTable"];

            DataRow drCurrentRow = null;
            drCurrentRow = dt.NewRow();

            drCurrentRow[0] = "";

            dt.Rows.Add(drCurrentRow); // add grid values in to row and add row to the blank table
            ViewState["AdditionalFilesTable"] = dt;

            GridView1.DataSource = dt; // bind new datatable to grid
            GridView1.DataBind();

        }
        else
        {
            CreateFileUploadtable();
        }
    }

这是我的HTML代码:

   <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
   <ContentTemplate>
        <tr>
             <td colspan="3" style="text-align:center; padding-top:25px; font-size:medium;text-align:center">                      
                     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="margin:0px auto;">
                         <Columns>
                             <asp:TemplateField>
                                 <HeaderTemplate>
                                   Upload Files
                                 </HeaderTemplate>

                                 <ItemTemplate>
                                     <table style="width:100%" >
                                         <tr>
                                             <td>
                                                 File:
                                             </td>
                                             <td>
                                                 <asp:FileUpload ID="fUpControl" runat="server" Width="300px" />
                                                 <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("FileContent") %>' />
                                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Upload File!" ControlToValidate="fUpControl" Display="Dynamic" ValidationGroup="WireTransfer" Font-Bold="True" Font-Size="Smaller" ForeColor="Red"></asp:RequiredFieldValidator>
                                         </td>
                                         <td>
                                                 File type:
                                         </td>
                                         <td>
                                                 <asp:DropDownList ID="ddlFileType" runat="server" Width="200px">
                                                     <asp:ListItem Value="0">-- Select Type --</asp:ListItem>
                                                     <asp:ListItem Value="Invoice">Invoice</asp:ListItem>
                                                     <asp:ListItem Value="Email">Email</asp:ListItem>
                                                     <asp:ListItem Value="WireDetails">Wire Details</asp:ListItem>
                                                     <asp:ListItem Value="WireConfirmation">Wire Confirmation</asp:ListItem>
                                                     <asp:ListItem Value="BankStatement">Bank Statement</asp:ListItem>
                                                     <asp:ListItem Value="ClosingDocument">Closing Document</asp:ListItem>
                                                 </asp:DropDownList>
                                                 <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Eval("FileType") %>' />
                                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Select File Type!" ControlToValidate="ddlFileType" Display="Dynamic" InitialValue="0" ValidationGroup="WireTransfer" Font-Bold="True" Font-Size="Smaller" ForeColor="Red"></asp:RequiredFieldValidator>
                                             </td>
                                             <td>
                                                 <asp:Button ID="btnDeleteAttachment" runat="server" Text="Delete" Width="80px" OnClick="btnDeleteAttachment_Click"/>
                                             </td>
                                         </tr>
                                     </table>
                                 </ItemTemplate>
                             </asp:TemplateField>
                         </Columns>
                     </asp:GridView>
                 </td>
             </tr>
             <tr>
                 <td style="text-align:center; font-size:medium; font-style:oblique; padding-bottom:5px;" colspan="3">
                                    <asp:Button ID="btnAddAttachment" runat="server" Text="Add Attachment" width="150px" OnClick="btnAddAttachment_Click"  />

             <tr>
                 <td style="text-align:center; font-size:medium; font-style:oblique; padding-bottom:5px;" colspan="3">
                     &nbsp;</td>
             </tr>
             <tr>
                 <td style="text-align:center; font-size:medium; font-style:oblique; padding-bottom:5px;" colspan="3">
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" width="150px" OnClick="btnSubmit_Click" ValidationGroup="WireTransfer"/>                        
                 </td>
             </tr>
         </ContentTemplate>
         <Triggers>
                <asp:PostBackTrigger ControlID="btnSubmit" />
         </Triggers>
         </asp:UpdatePanel>     

0 个答案:

没有答案