Need help finding a control on a page using FindControl
Posted on 2012-08-21
I am writing a SendEmail Class that has a method for sending emails. One of the parameters that is passed is the name of the FileUpload control on the page if a File Attachment is included in the email. If this parameter is null, then no file attachments are included in the email. If it is not null, it will contain the name of the control on the page that contains the FileUpload Control. The method then would use a FindControl to find the control on the page and attach the uploaded file to the email. My problem is I can't seem to get the syntax correct for using the FindControl and then for casting the control. Here is what I have so far: (FileAttachment is a string parameter passed to the method)
// Check if there is a File Attachment
if (FileAttachmentControl != null)
// Find control on page.
Control faControl1 = FindControl(FileAttachementControl); <-- Problem with this line here
if (faControl1 != null)
// Get Attachment
msg.Attachments.Add(new Attachment((FileUpload)faConrol1.PostedFile.InputStream, (FileUpload)faControl1.FileName)); <-- Problem casting here
1. What do I need to do to use FindControl?
2. How do I need to cast the control once I find it so I can access the PostedFile and FileName properties?
All help is greatly appreciated!