mikeb <ma************@nospam.mailnull.com> wrote in message news:<ea*************@TK2MSFTNGP10.phx.gbl>...
Robert Walter wrote: I want to user the Page method LoadControl inside the Global.asax.cs
but when I instantiate a page and try it I get the following error...
System.NullReferenceException: Object reference not set to an instance
of an object. at System.Web.UI.UserControlParser.GetCompiledUserCon trolType(String
virtualPath, String inputFile, HttpContext context) at
System.Web.UI.TemplateControl.LoadControl(String virtualPath) at
tt360.Cms.Adm.Global.Timer_Elapsed(Object sender, ElapsedEventArgs e)
in c:\vss\tt3\cms\004\web\tt360.cms.adm\global.asax.c s:line 95
I guess this is becuase the HttpContent is null. Now is there anyway I
can get round this?
Global.asax is for application level events - there's no page to load a
control into.
What overall result are you looking for?
Basically I'm using the Global.asax.cs to run timed application
events, sending out emails. The emails I want to send out are the
output from UserControls on the site.
I can get standard ASP.NET controls to generate fine but when I do a
LoadControl it gives the above error. Now I'm sure what I'm trying is
possible as Paul Wilsons Application Keep Alive Article
(
http://authors.aspalliance.com/paulw...rticles/?id=12) does a
LoadControl to compile user controls throughout a site.
The code I'm running is...
MailMessage mailMessage = new MailMessage();
mailMessage.From = "te**@test.com";
mailMessage.To = "te**@test.com";
mailMessage.BodyFormat = System.Web.Mail.MailFormat.Html;
string html = "html";
string subject = "subject";
try
{
System.Web.UI.Page controlLoader = new System.Web.UI.Page();
System.Web.UI.Control control =
controlLoader.LoadControl("~/Emails/test.ascx");
StringWriter writer = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
control.RenderControl(htmlWriter);
html = htmlWriter.InnerWriter.ToString();
subject = "subject";
}
catch (Exception ex)
{
html = ex.ToString();
}
mailMessage.Subject = subject;
mailMessage.Body = html;
System.Web.Mail.SmtpMail.SmtpServer = "mailserver";
System.Web.Mail.SmtpMail.Send(mailMessage);
And test.ascx is simply
<%@ Control Language="C#" %>
<asp:label runat="server" id="Test" Text="test"></asp:label>
Thanks in advance for your help,
Rob.