Solved

Dynamically Loading UserControl Containing DataBound Controls

Posted on 2011-09-08
2
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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