cannot maintain values of the file upload control after post back

Last post 09-30-2009 12:53 AM by ronald_yoh. 6 replies.

Sort Posts:

  • cannot maintain values of the file upload control after post back

    10-24-2007, 5:01 AM
    • Member
      2 point Member
    • ameydesai
    • Member since 10-24-2007, 8:50 AM
    • Posts 17

    I have a user control and have 2 drop downs in it... when i select one of it ....on the indexchange of the first dropdown i populate the other drop down...and on the post back the file path tat i have selected disappears after post back.. 

  • Re: cannot maintain values of the file upload control after post back

    10-24-2007, 8:55 AM
    • Participant
      1,244 point Participant
    • pradeep7n
    • Member since 10-24-2007, 6:07 AM
    • Coimbatore, India
    • Posts 212

    It is not possible to maintain values of the file upload control after post back.

    And even its not possible to rebind the value for codebehind on pageload. 

    Please remember to click "Mark as Answer" on this post if it helped you.
    Happy Coding Yes
    Pradeep
  • Re: cannot maintain values of the file upload control after post back

    10-24-2007, 9:42 AM
    • Participant
      1,044 point Participant
    • mustakim
    • Member since 05-17-2007, 6:43 AM
    • Posts 163

    HI,

    we can't set value of file upload control after postback with codebehind and javascript. there is no such property to bind file upload control after postback.

    Regards

    Regards,

    Mustakim Mansuri (MCTS, MCPD)
  • Re: cannot maintain values of the file upload control after post back

    10-24-2007, 10:45 AM
    Answer
    • All-Star
      58,226 point All-Star
    • anas
    • Member since 09-21-2006, 4:31 AM
    • Palestinian Territory, Occupied
    • Posts 6,667
    • Moderator

    hi

    there is no direct solutions as mentioned .. until you use the ajax ..

    also you cant set the value of FileUpload control even from javascript

    and the following didnt worked even its rendered in html  :

    Me.FileUpload1.Attributes("value") ="...."
     solution:

    but you can use:

    •  label control to display the last selected file .
    • hiddenField Control to transfer the selected file from the client to server.

    but you need also to remeber the last posted file

    because in case you dont select a file from FileUpload control  after a postback and then submit ,

    the FileUpload.postedFile data will gone , so you have to save the posted file to the session ...

    this is how :

    asp.net page  Code:

     <form id="form1" runat="server">
             
            <asp:FileUpload ID="FileUpload1" runat="server" /> <br />
             <asp:Label ID="lblCurrentFile" runat="server"></asp:Label><br />
            <br />
            <asp:Button ID="BtnSubmit" runat="server" Text="postBack" />
            <br />
            <asp:HiddenField ID="HiddenField1" runat="server" />
        </form>

     codeBehind: 

    Private Property PostedFile() As HttpPostedFile
            Get
                If Me.Session("postedFile") IsNot Nothing Then
                    Return Session("postedFile")
                End If
                Return Nothing
            End Get
            Set(ByVal value As HttpPostedFile)
                Session("postedFile") = value
            End Set
        End Property
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                Me.BtnSubmit.Attributes("onclick") = _
                String.Format("document.getElementById('{0}').value= document.getElementById('{1}').value;", _
                Me.HiddenField1.ClientID, Me.FileUpload1.ClientID)
            End If
    
    
            If Me.FileUpload1.PostedFile IsNot Nothing _
            AndAlso Me.FileUpload1.PostedFile.ContentLength > 0 Then
                Me.PostedFile = Me.FileUpload1.PostedFile
            End If
    
            If Me.HiddenField1.Value <> "" Then
                Me.lblCurrentFile.Text = Me.HiddenField1.Value
            End If
    
        End Sub
     
    regards,
    Regards,

    Anas Ghanem | Blog

  • Re: cannot maintain values of the file upload control after post back

    10-25-2007, 11:57 PM
    • Member
      2 point Member
    • ameydesai
    • Member since 10-24-2007, 8:50 AM
    • Posts 17

    Thanks alot for the help.

    Regards.

  • Re: cannot maintain values of the file upload control after post back

    01-30-2008, 4:42 AM
    • Member
      4 point Member
    • mbm2006
    • Member since 01-28-2008, 6:15 AM
    • Posts 2

    what if i what to save that file using SaveAs....

    pls reply...

  • Re: cannot maintain values of the file upload control after post back

    09-30-2009, 12:53 AM
    • Member
      76 point Member
    • ronald_yoh
    • Member since 10-26-2008, 10:37 PM
    • Posts 266

    it is not working with user control... can you help me out?

Page 1 of 1 (7 items)