Solved

asp.net create HtmlInputFile controls dynamically

Posted on 2007-11-20
3
1,062 Views
Last Modified: 2013-12-17
I am trying to create HtmlInputFile controls on the fly.  

Basically, the user will click a button to upload a file, a postback will happen and an input form should appear.
I want to make it so that they can click this for as many attachments as they want to send (I'll put limits in as needed, but for the purposes of getting this initial step working...)
the first HtmlInputFile control adds fine, which brings me to problem one:  I attach a file and it won't post afterwards (I have the form configured to multipart).  
the second problem is that the control disappears on postback and I can't have 2 of them.

If this is not doable I'll accept a solution that will let me upload attachments one a time and then relate them to the form that I'm uploading them from.  (this is a form for 2 tables, an "attachment" table that stores the files which is related to another table that stores the "tickets".

thanks in advance!

Let me know if you need any more info.  I've attached the snippet of code that creates and adds the control to the place holder.
private void AddAttachment_Click(object sender, EventArgs e)
{
	HtmlInputFile HIF;
	
	int i=0;
	IEnumerator IE = ((PlaceHolder)AddTicket.FindControl("AttachmentControls")).Controls.GetEnumerator();
	
	while (IE.MoveNext())
		{
			HIF = (HtmlInputFile)IE.Current;
			
			((PlaceHolder)AddTicket.FindControl("AttachmentControls")).Controls.Add(HIF);
			
			i++;
		}
	
	HIF = new HtmlInputFile();
	
	((PlaceHolder)AddTicket.FindControl("AttachmentControls")).Controls.Add(HIF);
	
	HIF.ID="Attachment"+i.ToString();
	
}

Open in new window

0
Comment
Question by:snowburnd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 250 total points
ID: 20335618
If you add controls dynamically and want them to survive a postback, you have to recreate them every time the page is created.

If you select a file to be uploaded, it will be posted to the server, but only once. It will be uploaded on the next postback, and you ony have one chance to recieve it on the server. It's impossible to make the control keep the file selection through a postback so that it gets uploaded later.

So, if the user first clicks to show the upload field, selects a file in that field, then clicks the button to add another field, in the postback you have to:

1. Take care of the file that is sent to the server.
2. Recreate the first upload field (alternatively replace it with an indicator that a file has been uploaded).
3. Add the new upload field.

If you first recreate the previous upload controls, you can check for uploaded files using them. Otherwise you can get the uploaded files from the Request.Files collection.
0
 

Author Comment

by:snowburnd
ID: 20349534
Thanks for your response.

So I would have to create something like the OWA file upload or figure out a way to add them client side only without postback.  

This should give me enough to go on and stop me from beating my head on the wall any more.
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20350223
Yes, you can add the upload controls either on the server side or the client side.
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 96
Developing a front end to SPLUNK 1 104
SSIS GUID Variable 2 57
Concurrency Error Persistent using Data Grid View Update C# 2 28
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question