Asked by:
How to Keep the Contents of the FileUpload control After the Postback

Question
-
User-786564416 posted
I have 4 FileUpload controls. I have file type custom validators for each FileUpload control. What I want to do is when the user click the Submit button, I want to show the file type validators without losing the contents uploaded to the FileUpload controls.
I used the Ajax update panel but the issue is not solved. My code is as following:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPages/MyMasterPage.master" AutoEventWireup="false" CodeFile="UploadAttachments.aspx.vb" Inherits="NewPosting_UploadAttachments" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <style type="text/css"> .PanelStyle { margin-top:30px; background-color:#C9DCF2; opacity:0.8; Border-Color:Black; Border-Style:Solid; -moz-border-radius: 15px; -webkit-border-radius: 15px; border-radius: 15px; } .Buttonout { cursor:default; -moz-border-radius: 15px; -webkit-border-radius: 15px; border-radius: 15px; } .Buttonhover { cursor:pointer; -moz-border-radius: 15px; -webkit-border-radius: 15px; border-radius: 15px; } .LabelStyle { height:100px; width:400px; font-family:Fanan; font-size:36px; color:#0066CC; } .FooterErrorMessage { text-align: right; margin-top: 0px; font-size: 20px; } .auto-style22 { height: 54px; text-align: center; } .FooterErrorMessage { color: red; text-align: right; font-family: "Sakkal Majalla"; font-style: normal; font-size: 24px; margin-left: 0px; margin-top: 5px; } .LabelingCell { border: 1px solid Black; background-color: #96C2F2; width:20%; } .FileUploadStyle { direction:rtl; -moz-border-radius: 15px; -webkit-border-radius: 15px; border-radius: 15px; } .ErrorMessage { color: red; font-style: normal; font-size: 28px; } </style> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="CpMainContent" Runat="Server"> <script type="text/javascript"> function IsFileSelected1() { if (document.getElementById("<%=AttachmentsSelecter1.ClientID%>").value == "") { document.getElementById("<%=AttachmentsSelecter1.ClientID%>").style.color = "Transparent"; document.getElementById("<%=AttachmentsSelecter1.ClientID%>").style.backgroundColor = "White"; } else { document.getElementById("<%=AttachmentsSelecter1.ClientID%>").style.color = "black"; document.getElementById("<%=AttachmentsSelecter1.ClientID%>").style.backgroundColor = "Green"; } } function IsFileSelected2() { if (document.getElementById("<%=AttachmentsSelecter2.ClientID%>").value == "") { document.getElementById("<%=AttachmentsSelecter2.ClientID%>").style.color = "Transparent"; document.getElementById("<%=AttachmentsSelecter2.ClientID%>").style.backgroundColor = "White"; } else { document.getElementById("<%=AttachmentsSelecter2.ClientID%>").style.color = "black"; document.getElementById("<%=AttachmentsSelecter2.ClientID%>").style.backgroundColor = "Green"; } } function IsFileSelected3() { if (document.getElementById("<%=AttachmentsSelecter3.ClientID%>").value == "") { document.getElementById("<%=AttachmentsSelecter3.ClientID%>").style.color = "Transparent"; document.getElementById("<%=AttachmentsSelecter3.ClientID%>").style.backgroundColor = "White"; } else { document.getElementById("<%=AttachmentsSelecter3.ClientID%>").style.color = "black"; document.getElementById("<%=AttachmentsSelecter3.ClientID%>").style.backgroundColor = "Green"; } } function IsFileSelected4() { if (document.getElementById("<%=AttachmentsSelecter4.ClientID%>").value == "") { document.getElementById("<%=AttachmentsSelecter4.ClientID%>").style.color = "Transparent"; document.getElementById("<%=AttachmentsSelecter4.ClientID%>").style.backgroundColor = "White"; } else { document.getElementById("<%=AttachmentsSelecter4.ClientID%>").style.color = "black"; document.getElementById("<%=AttachmentsSelecter4.ClientID%>").style.backgroundColor = "Green"; } } </script> <asp:toolkitscriptmanager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Panel ID="Panel1" runat="server" CssClass="PanelStyle" Width="658px" > <table id="MessageTable" runat="server" style="width:100%;"> <tr align="center" style="height:60px;"> <td colspan="4"> <asp:Label ID="Label3" runat="server" style="font-family: 'sakkal Majalla'; font-size: 30px" Text="تحميل ملفات متعددة"></asp:Label> </td> </tr> <tr> <td width="7.5%" style="text-align: right"> <asp:CustomValidator ID="AttachmentFileType1" runat="server" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="File Type Is Not Correct" OnServerValidate="AttachmentValidator1" SetFocusOnError="True">*</asp:CustomValidator> </td> <td align="right" > <asp:FileUpload ID="AttachmentsSelecter1" runat="server" BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" cssclass="FileUploadStyle" Height="40px" onchange="IsFileSelected1()" style="font-size: 18px; font-family: 'sakkal Majalla'; -moz-border-radius: 15px;-webkit-border-radius: 15px;border-radius: 15px;" Width="400px" /> </td> <td align="right" class="LabelingCell"> <asp:Label ID="Label1" runat="server" style="text-align: right; font-size: 30px; font-family: sc_AMEEN; margin-left: 0px;" Text="First File" Width="130px" BorderStyle="None" ForeColor="Black" Height="37px"></asp:Label> </td> <td width="7.5%"></td> </tr> <tr> <td style="text-align: right"> <asp:CustomValidator ID="AttachmentFileType2" runat="server" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="File Type Is Not Correct" SetFocusOnError="True">*</asp:CustomValidator> </td> <td align="right" > <asp:FileUpload ID="AttachmentsSelecter2" runat="server" BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" cssclass="FileUploadStyle" Height="40px" onchange="IsFileSelected2()" style="font-size: 18px; font-family: 'sakkal Majalla'; -moz-border-radius: 15px;-webkit-border-radius: 15px;border-radius: 15px;" Width="400px" /> </td> <td align="right" class="LabelingCell"> <asp:Label ID="Label2" runat="server" style="text-align: right; font-size: 30px; font-family: sc_AMEEN; margin-left: 0px;" Text="Second File" Width="130px" BorderStyle="None" ForeColor="Black" Height="37px"></asp:Label> </td> <td ></td> </tr> <tr> <td style="text-align: right"> <asp:CustomValidator ID="AttachmentFileType3" runat="server" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="File Type Is Not Correct" SetFocusOnError="True">*</asp:CustomValidator> </td> <td align="right" > <asp:FileUpload ID="AttachmentsSelecter3" runat="server" BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" cssclass="FileUploadStyle" Height="40px" onchange="IsFileSelected3()" style="font-size: 18px; font-family: 'sakkal Majalla'; -moz-border-radius: 15px;-webkit-border-radius: 15px;border-radius: 15px;" Width="400px" /> </td> <td align="right" class="LabelingCell"> <asp:Label ID="Label4" runat="server" style="text-align: right; font-size: 30px; font-family: sc_AMEEN; margin-left: 0px;" Text="Third File" Width="130px" BorderStyle="None" ForeColor="Black" Height="37px"></asp:Label> </td> <td></td> </tr> <tr> <td style="text-align: right"> <asp:CustomValidator ID="AttachmentFileType4" runat="server" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="File Type Is Not Correct" SetFocusOnError="True">*</asp:CustomValidator> </td> <td align="right" > <asp:FileUpload ID="AttachmentsSelecter4" runat="server" BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" cssclass="FileUploadStyle" Height="40px" onchange="IsFileSelected4()" style="font-size: 18px; font-family: 'sakkal Majalla'; -moz-border-radius: 15px;-webkit-border-radius: 15px;border-radius: 15px;" Width="400px" /> </td> <td align="right" class="LabelingCell"> <asp:Label ID="Label5" runat="server" style="text-align: right; font-size: 30px; font-family: sc_AMEEN; margin-left: 0px;" Text="Fouth File" Width="130px" BorderStyle="None" ForeColor="Black" Height="37px"></asp:Label> </td> <td width="10%"></td> </tr> <tr> <td></td> <td> </td> <td></td> <td></td> </tr> <tr align="center"> <td align="center" colspan="4" class="auto-style22"> <asp:Button ID="SubmitBtn" runat="server" BackColor="#6262FF" ForeColor="White" Height="38px" style="text-align: center; font-size: 28px; font-family: sc_AMEEN; margin-top: 26px; margin-bottom: 5px; margin-left: 37px; -moz-border-radius: 15px;-webkit-border-radius: 15px;border-radius: 15px;" Text="استمرار" Width="200px" onMouseOver="this.className='Buttonhover'" onMouseOut="this.className='Buttonout'" /> </td> </tr> <tr> <td align="left"> <asp:ImageButton ID="CloseWindow" runat="server" Height="40px" ImageUrl="~/Images/CloseThisPage.png" Width="50px" /> </td> </tr> </table> <table> <tr> <td colspan="3" style="text-align: right" > <asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="FooterErrorMessage" DisplayMode="List" Height="120px" Width="544px" /> </td> </tr> </table> </asp:Panel> </ContentTemplate> </asp:UpdatePanel> </asp:Content>
What are the changes I can made so I keep the contents attached
Friday, March 9, 2018 2:36 PM
All replies
-
User632428103 posted
Hello alihusain_77,
you need to keept the value in session for example or in a hidden field
http://aspdotnetassist.blogspot.lu/2014/10/how-to-maintain-state-of-fileupload.html
but just a question, why use 4 fileUpload why don't you use one upload file but with multi selected file ?
Friday, March 9, 2018 2:50 PM