mikeb <ma************ @nospam.mailnul l.com> wrote in message news:<ea******* ******@TK2MSFTN GP10.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.NullRefe renceException: Object reference not set to an instance
of an object. at System.Web.UI.U serControlParse r.GetCompiledUs erControlType(S tring
virtualPath, String inputFile, HttpContext context) at
System.Web.UI.T emplateControl. LoadControl(Str ing virtualPath) at
tt360.Cms.Adm.G lobal.Timer_Ela psed(Object sender, ElapsedEventArg s e)
in c:\vss\tt3\cms\ 004\web\tt360.c ms.adm\global.a sax.cs: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.Fro m = "te**@test.com" ;
mailMessage.To = "te**@test.com" ;
mailMessage.Bod yFormat = System.Web.Mail .MailFormat.Htm l;
string html = "html";
string subject = "subject";
try
{
System.Web.UI.P age controlLoader = new System.Web.UI.P age();
System.Web.UI.C ontrol control =
controlLoader.L oadControl("~/Emails/test.ascx");
StringWriter writer = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter( writer);
control.RenderC ontrol(htmlWrit er);
html = htmlWriter.Inne rWriter.ToStrin g();
subject = "subject";
}
catch (Exception ex)
{
html = ex.ToString();
}
mailMessage.Sub ject = subject;
mailMessage.Bod y = html;
System.Web.Mail .SmtpMail.SmtpS erver = "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.