LoadControl in Global.asax.cs

R

Robert Walter

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.GetCompiledUserControlType(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.cs:line 95

I guess this is becuase the HttpContent is null. Now is there anyway I
can get round this?

Thanks,



Rob.
 
M

mikeb

Robert said:
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.GetCompiledUserControlType(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.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?
 
R

Robert Walter

mikeb said:
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/paulwilson/Articles/?id=12) does a
LoadControl to compile user controls throughout a site.

The code I'm running is...

MailMessage mailMessage = new MailMessage();

mailMessage.From = "(e-mail address removed)";
mailMessage.To = "(e-mail address removed)";
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.
 
M

mikeb

Robert said:
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/paulwilson/Articles/?id=12) does a
LoadControl to compile user controls throughout a site.

The code I'm running is...
.... snip ...

I see that you're creating your own page to load the control into.

However, when I cut-n-paste your code into a new project and run it, I
do not get an exception.

I'm using VS.NET 2003, in case that matters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top