Solved

Dynamically Loading UserControl Containing DataBound Controls

Posted on 2011-09-08
2
216 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

832 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