?
Solved

Need help finding a control on a page using FindControl

Posted on 2012-08-21
5
Medium Priority
?
970 Views
Last Modified: 2012-08-22
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!
0
Comment
Question by:dyarosh
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 38319250
Where is this code ? On your asp.net aspx-page or in a class ?
Do you pass the control to a class ?
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38319440
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
 
LVL 5

Expert Comment

by:Gerry Bartley
ID: 38319441
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
 

Author Comment

by:dyarosh
ID: 38320833
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
 

Author Closing Comment

by:dyarosh
ID: 38321005
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

569 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