locked
Help keeping FileUpload FileName/PostedFile in Custom Controls ViewState RRS feed

  • Question

  • User1737692904 posted

    I ran into this problem and tried multiple ways to save an image to our server.

    I'm having trouble getting the file path of my image using the FileUpload control inside a custom control. After browsing to my image and clicking my "Upload" button, the FileUpload control is losing its value.

    However I was able to get the file name using JavaScript. But since it's not the whole path I can't write code to save the image to my "uploadedImages" directory.

     

    Here is my code:

    Html:
    <asp:FileUpload ID="FileUpload1" runat="server" EnableViewState="true" />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" OnClientClick="getfilepath()" EnableViewState="true" />
    
    Javascript:
    function getfilepath() {
    var fileupload = document.getElementById("ctl00_ContentPlaceHolder1_DropZone1_columnDisplay_ctl00_controlcolumn_ctl00_WidgetHost_WidgetHost_widget_FileUpload1");
    var hiddenvalue = document.getElementById("ctl00_ContentPlaceHolder1_DropZone1_columnDisplay_ctl00_controlcolumn_ctl00_WidgetHost_WidgetHost_widget_filepath");
    hiddenvalue.value = fileupload.value;
    }
    
    //Code Behind: protected void btnUpload_Click(object sender, EventArgs e) { if (FileUpload1.PostedFile!= "") // LOSES VALUE AFTER POSTBACK { // Here is my basic class that gets the user input from UI _Gallery galleryItem = new _Gallery() { ID = 0, Title = txtTitle.Text, Description = txtDescription.Text, Image = FileUpload1.PostedFile, // LOSES VALUE AFTER POSTBACK SortOrder = 0 }; // The I create a new instance and plugging in some // default value and some from my class Ektron.Cms.LibraryData libData = new LibraryData() { FileName = "/uploadedImages/Images/" + galleryItem.Image, LanguageId = 1033, Title = galleryItem.Title, ParentId = 104, // ID of the image folder FolderName = "Images", ContentType = 7, // 7 is for library item Type = "images" }; // Save using ektron API and return ID Ektron.Cms.API.Library libAPI = new Library(); long libID = libAPI.AddLibraryItem(ref libData); } // End If() } // end btnUpload_Click()

    Any suggestions?

    Friday, September 2, 2011 2:37 PM

Answers

  • User705563967 posted

    your are using fileupload control inside the update panel try to remove update panel then check its working fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 2, 2011 6:04 PM
  • User-578657687 posted

    Hi,

    This article elaborates FileUpload control and provide several sample code about how to  save files to a specified path:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload(v=VS.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 6, 2011 4:51 AM
  • User1737692904 posted

    Thanks for the suggestion. After removing the EktronCMS IWidget namespace, I was able to use my FileUpload control. I think Ektron is using some sort of UpdatePanel that is causing my controls to lose their values after postback.

    public partial class widgets_Gallery : System.Web.UI.UserControl//, IWidget

    {

    // IWidgetHost _host; 

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 6, 2011 2:09 PM

All replies

  • User705563967 posted

    your are using fileupload control inside the update panel try to remove update panel then check its working fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 2, 2011 6:04 PM
  • User-578657687 posted

    Hi,

    This article elaborates FileUpload control and provide several sample code about how to  save files to a specified path:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload(v=VS.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 6, 2011 4:51 AM
  • User1737692904 posted

    Thanks for the suggestion. After removing the EktronCMS IWidget namespace, I was able to use my FileUpload control. I think Ektron is using some sort of UpdatePanel that is causing my controls to lose their values after postback.

    public partial class widgets_Gallery : System.Web.UI.UserControl//, IWidget

    {

    // IWidgetHost _host; 

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 6, 2011 2:09 PM