File Upload from within datalist

Discussion in 'ASP .Net' started by David Kenneally, Jun 7, 2004.

  1. Hello-
    I'm having a problem doing a file upload from within the edit function
    of a datalist. I have a simple file upload that works on a standard
    ..net page:


    <td width="53"><input id="inpFileUp" type="file" runat="Server"
    />&nbsp;</td>
    <asp:Button id="Button1" onclick="Button_Click2" Runat="Server"
    Text="Upload File!"></asp:Button>

    And this is the function to upload the file:

    Sub Button_Click2( s As Object, e As EventArgs )
    Dim fileName As String
    Dim saveAs As String
    Dim resultsText as String
    fileName = inpFileUp.PostedFile.FileName
    fileName = Path.GetFileName(fileName)
    saveAs = "C:\inetpub\wwwroot\images\" & fileName
    inpFileUp.PostedFile.SaveAs( saveAs )
    resultsText = "Image file: "
    lblUploadResults.Text = resultsText
    lblUploadFile.Text = fileName
    inpFileUp.Visible=False
    Button1.Visible=False
    End Sub

    This all works fine. It doesn't, of course, work from within the edit
    item template of a datalist. I get this error:
    "Name 'inpFileUp' is not declared"
    I can't seem to use "find control" either, sense I'm calling it from a
    button click.

    If I try to skip the actual upload until I'm in the "update item"
    function, I get errors such as :
    System.Web.UI.Control cannot be converted to System.Web.HttpPostedFile

    So anyhow, the idea is that I have a datalist with a number of
    records, and I'd like to be able to upload a file from within the edit
    item template of one of these items.

    Any ideas?

    Thank you!

    David
    David Kenneally, Jun 7, 2004
    #1
    1. Advertising

  2. The easiest way to Debug these type of Problems is to Disable SmartNavigation and then to use the View Source function in your Browser. Then search for the code where the 'inpFileUp' is declared. If you see a Prefix then you will know that inside the ItemBound Event etc in a DataGrid you would have to use the FindControl Method and then pass the ClientID to the function. Let me know if you need a sample.

    Regards,

    Trevor Benedict R
    =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?=, Jun 7, 2004
    #2
    1. Advertising

  3. So I figured it out (with help from 4guysfromrolla.com), so hopefully
    this will help someone else:

    this is what I did:
    Dim saveAs As String
    Dim fileName As String
    Dim inpFileUp As New HtmlInputFile()
    inpFileUp = CType(e.Item.FindControl("inpFileUp"),
    HtmlInputFile)
    fileName = inpFileUp.PostedFile.FileName
    fileName = Path.GetFileName(fileName)
    saveAs = "C:\inetpub\wwwroot\images\" & fileName
    ' Response.Write(saveAs)
    inpFileUp.PostedFile.SaveAs( saveAs )

    And then I put this in the "Update Function" rather than keeping it in
    a separate button click.

    Works well now.

    David

    (David Kenneally) wrote in message news:<>...
    > Hello-
    > I'm having a problem doing a file upload from within the edit function
    > of a datalist. I have a simple file upload that works on a standard
    > .net page:
    >
    >
    > <td width="53"><input id="inpFileUp" type="file" runat="Server"
    > />&nbsp;</td>
    > <asp:Button id="Button1" onclick="Button_Click2" Runat="Server"
    > Text="Upload File!"></asp:Button>
    >
    > And this is the function to upload the file:
    >
    > Sub Button_Click2( s As Object, e As EventArgs )
    > Dim fileName As String
    > Dim saveAs As String
    > Dim resultsText as String
    > fileName = inpFileUp.PostedFile.FileName
    > fileName = Path.GetFileName(fileName)
    > saveAs = "C:\inetpub\wwwroot\images\" & fileName
    > inpFileUp.PostedFile.SaveAs( saveAs )
    > resultsText = "Image file: "
    > lblUploadResults.Text = resultsText
    > lblUploadFile.Text = fileName
    > inpFileUp.Visible=False
    > Button1.Visible=False
    > End Sub
    >
    > This all works fine. It doesn't, of course, work from within the edit
    > item template of a datalist. I get this error:
    > "Name 'inpFileUp' is not declared"
    > I can't seem to use "find control" either, sense I'm calling it from a
    > button click.
    >
    > If I try to skip the actual upload until I'm in the "update item"
    > function, I get errors such as :
    > System.Web.UI.Control cannot be converted to System.Web.HttpPostedFile
    >
    > So anyhow, the idea is that I have a datalist with a number of
    > records, and I'd like to be able to upload a file from within the edit
    > item template of one of these items.
    >
    > Any ideas?
    >
    > Thank you!
    >
    > David
    David Kenneally, Jun 8, 2004
    #3
  4. David Kenneally

    Al Guest

    Al, Nov 18, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nevyn Twyll
    Replies:
    8
    Views:
    1,867
  2. =?Utf-8?B?U2FyYXY=?=

    Upload a file without file Upload control - ASP.Net

    =?Utf-8?B?U2FyYXY=?=, Aug 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,432
    Bruce Barker
    Aug 3, 2005
  3. Heather Fraser
    Replies:
    0
    Views:
    621
    Heather Fraser
    Jul 5, 2004
  4. Replies:
    1
    Views:
    923
    Gabriel Genellina
    Nov 25, 2006
  5. Will

    Problem with DataList within DataList

    Will, Oct 10, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    188
    Walter Wang [MSFT]
    Oct 18, 2007
Loading...

Share This Page