Solved

Dynamically Loading UserControl Containing DataBound Controls

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now