Need help finding a control on a page using FindControl

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!
dyaroshAsked:
Who is Participating?
 
DhaestCommented:
Where is this code ? On your asp.net aspx-page or in a class ?
Do you pass the control to a class ?
0
 
Alan WarrenApplications DeveloperCommented:
Is the FileAttachementControl inside a formview or some such control?
If so you would use:
  Control faControl1 = myFormView.FindControl(FileAttachementControl);


In VB we wrap it in a Ctype too, but not sure how to do that using C
           
objTextBox = CType(Me.myFormView.FindControl("TheTextBox"), TextBox)
            If objTextBox IsNot Nothing Then
                With objTextBox
                    .Text = .Text.Replace(strToFind, vbLf)
                End With
                objTextBox.Dispose()
            End If

Open in new window

Alan
0
 
Gerry BartleySoftware EngineerCommented:
If passing the control to a class you also need to pass the page


// Check if there is a File Attachment
        if (FileAttachmentControl != null)
        {
            // Find control on page.
           FileUpload faControl1 = (FileUpload )page.FindControl("FileAttachementControl");
            if (faControl1 != null)
            {
                // Get Attachment
                msg.Attachments.Add(new Attachment(faConrol1.PostedFile.InputStream, faControl1.FileName)); 
            }
        }

Open in new window

0
 
dyaroshAuthor Commented:
Is there a way to pass the FileUpload control from the page where it resides to the Class so I can reference it in the Class?  If yes, what would be the proper syntax?

Here is what is on the aspx page:

<asp:FileUpload ID="FileUpload1" runat="server" />

In the aspx.c I would make the following call:
msg.SendEmailMessage(email, SubjectTB.Text, MessageTextArea.Value, FileUpload1);

In the SendEmail Class, I defined the SendEmailMessage as follows:
 public string SendEmailMessage(string senderEmail, string subject, string msgBody, Control FileAttachmentControl)

How do I cast FileAttachmentControl as a FileUpload?
0
 
dyaroshAuthor Commented:
Thanks for the suggestion.  I passed the control to the class and used the following syntax to access the properties of the control.

((System.Web.UI.WebControls.FileUpload)(FileAttachmentControl)).HasFile
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.