Solved

Dynamically Loading UserControl Containing DataBound Controls

Posted on 2011-09-08
2
215 Views
Last Modified: 2012-06-27
Hi All,

I have created a user control which consists of a Page_Load event which binds data from SQL Server into several Gridview controls.

If I add this control into an aspx page at design time then this runs fine and returns the gridview correctly.

I am then trying to use the content of this UserControl as an email template (attached is the code) but what I am finding is the markup of the control is showing correctly within the delivered email but any databound data controls arent showing so its as though the page_load even isn't being fired.

Can anyone suggest how to fix this?

Thanks,

Rit
StringBuilder sb = new StringBuilder();
UserControl ctrltmp = new UserControl();
Control ctrl = ctrltmp.LoadControl("~/controls/mail/mail_news_summary.ascx");

StringWriter sw = new StringWriter(sb);
Html32TextWriter htw = new Html32TextWriter(sw);
ctrl.RenderControl(htw);

string strBody = sb.ToString();


MailAddress from = new MailAddress("enquiries@mydomain.com");
MailAddress to = new MailAddress("me@mydomain.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "Test Email Send";
message.Body = strBody;
message.BodyEncoding = Encoding.ASCII;
message.IsBodyHtml = true;

SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.UseDefaultCredentials = true;

try
{
    mySmtpClient.Send(message);
}
catch (Exception ex)
{
    MessageBox.Show(string.Format("Exception caught: {0}", ex.ToString()));
}

Open in new window

0
Comment
Question by:rito1
2 Comments
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
ID: 36502070
Page.Controls.Add(ctrl) is missing.
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 36527702
Appologies for the delay.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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