Solved

Dynamically Loading UserControl Containing DataBound Controls

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Easy filter aspnet 2 35
looking for an ASP.net report designer 4 42
ASP.NET Calendar Control 5 28
c#  FTP ftpwebrequest URI invalid 6 13
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

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