Accessing dynamically added htmlinputfiles

xxsashixx

New Member
I'm fairly new to asp.net so bear with me. I've been struggling on how<BR>to access dynamically added htmlinputfile controls. I basically have a<BR>form with a table control. I have two check boxes, one to add another<BR>input file, and another one to delete one. Both are autopostback<BR>enabled. The problem is I can access the input file's properties (e.g.<BR>value, ID), but I can't perform the PostedFile.SaveAs function. Here is<BR>my code:<BR> <BR>Imports System.Data.OleDb<BR>Imports System.Configuration<BR>Imports System.Data<BR>Imports System.Web.UI.HtmlControls<BR>Imports System.Web.UI<BR>Public Class Add_FD_Journal<BR> Inherits System.Web.UI.Page<BR> Protected WithEvents Entry As System.Web.UI.WebControls.TextBox<BR> Protected WithEvents Submit As System.Web.UI.WebControls.Button<BR> Protected WithEvents Pic_Count As<BR>System.Web.UI.HtmlControls.HtmlInputHidden<BR> Protected WithEvents Table1 As System.Web.UI.HtmlControls.HtmlTable<BR> Protected WithEvents More As System.Web.UI.WebControls.CheckBox<BR> Protected WithEvents Less As System.Web.UI.WebControls.CheckBox<BR> Protected WithEvents Table2 As System.Web.UI.HtmlControls.HtmlTable<BR> Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel<BR> Protected WithEvents Label1 As System.Web.UI.WebControls.Label<BR> Protected WithEvents RequiredFieldValidator1 As<BR>System.Web.UI.WebControls.RequiredFieldValidator<BR> <BR> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As<BR>System.EventArgs) Handles MyBase.Load<BR> If More.Checked Then<BR> Pic_Count.Value = http://aspmessageboard.com/archive/index.php/Pic_Count.Value + 1<BR> More.Checked = False<BR> ElseIf Less.Checked Then<BR> Pic_Count.Value = Pic_Count.Value - 1<BR> Less.Checked = False<BR> End If<BR> Dim i As Integer<BR> For i = 1 To Pic_Count.Value<BR> Dim Row As New HtmlTableRow()<BR> Dim Cell1 As New HtmlTableCell()<BR> Dim Cell2 As New HtmlTableCell()<BR> Cell1.Width = "125px"<BR> Cell1.InnerText = "Picture " & i<BR> Row.Cells.Add(Cell1)<BR> Dim ch As New System.Web.UI.HtmlControls.HtmlInputFile()<BR> ch.ID = "picture_" & i<BR> ch.Style.Add("WIDTH", "200px")<BR> Cell2.Controls.Add(ch)<BR> 'Panel1.Controls.Add(ch)<BR> Row.Cells.Add(Cell2)<BR> Table2.Rows.Add(Row)<BR> Next<BR> End Sub<BR> <BR> Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As<BR>System.EventArgs) Handles Submit.Click<BR> Dim d As DateTime = DateTime.Now<BR> Dim Conn As New<BR>OleDbConnection(ConfigurationSettings.AppSettings("Connection_String"))<BR> Dim Comm As New OleDbCommand("INSERT INTO FD_Journal<BR>(Entry_Date, Entry) VALUES (#" & d & "#,'" & Request.Form("Entry") &<BR>"')", Conn)<BR> Try<BR> Conn.Open()<BR> Comm.ExecuteNonQuery()<BR> Catch MyException As Exception<BR> Response.Write("An error has occurred : " &<BR>MyException.ToString())<BR> Finally<BR> If Not Conn Is Nothing AndAlso ((Conn.State And<BR>ConnectionState.Open) = ConnectionState.Open) Then<BR> Conn.Close()<BR> End If<BR> End Try<BR> <BR> Dim upload As New Control()<BR> upload = Table2.FindControl("picture_1")<BR> CType(upload, HtmlInputFile).PostedFile.SaveAs("test.jpg") <BR> End Sub<BR> <BR> Protected Overloads Sub LoadViewState(ByVal sender As System.Object)<BR> 'MyBase.LoadViewState(sender)<BR> <BR> 'Dim Obj As Object<BR> End Sub<BR>End Class<BR> <BR>My form fails at the CType(upload,<BR>HtmlInputFile).PostedFile.SaveAs("test.jpg") line. I get the following<BR>error:<BR> <BR>System.NullReferenceException: Object reference not set to an instance<BR>of an object.<BR> <BR>I have verified that I can access the input file's value property (i.e.<BR>file path) by outputting it to a label on the form. I also have another<BR>question about the way I implemented the adding of the input files.<BR>Currently I have a hidden field that holds the current number of input<BR>files to generate on each post back. Is there a way to add the input<BR>file permanently instead of having to generate all of them on each and<BR>every postback? You can see how I did it in my Page_Load subroutine.<BR> <BR>TIA,<BR>LDawg<BR>Is your form's enctype property set to "multipart/form-data" ? Please verify. If this is not set, then, your file control's PostedFile returns null instead of a reference to the file to be uploaded.Thank you so much. It's always those small details that manages to stump you. It works now. Thanks again.<BR><BR>-LDawg
 
Back
Top