File Upload from within datalist

  • Thread starter Thread starter David Kenneally
  • Start date Start date
D

David Kenneally

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
 
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
 
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
 
Back
Top