loading ascx from windows desktop application?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

I am trying to write a #C windows desktop application that loads an ascx file contents (user control) from file, initiates it and then registers to database everything it finds. I tried that way:

1 Page page=new Page();
2 Control control=page.LoadControl(virtualPath);
3 string debug = control.ID;

but I got NullReferenceException on the 2nd line.

From earlier post http://www.dotnet247.com/247reference/msgs/29/148858.aspx
I found out that to create a control dynamically, one first needs to include className into the ascx and to register the control in page. My brand new dynamically created page object of course lacks that registration and I guess that's why the LoadControl method does not work.

Is anybody able to explain, how to properly look up the contents of an ascx file?

I would appreciate if you sent all answers directly to (e-mail address removed) as well.

Thanks in advance,

Kristjan
 
Hi Kristjan,

Did you already ask this question in the newsgroup
microsoft.public.dotnet.framework.aspnet

That is a very active and quick answering newsgroup.

I think that with your question you have the best change there.

Cor
 
I don't think the registration is your problem. LoadControl is used when
there are no registrations. When user controls are explicitly registered,
they is usually an instance of them on the page as well, as opposed to
dynamically loading them via LoadControl.

I think your problem lies in the fact that you are creating an instance of
Page, and not ASP.NET. Your instance of page has no Request, Reponse, etc -
how could it? It was not created from a web request, so it cannot have a web
context. And I believe that is where your problem lies.

I would imagine simulating everything that ASP.NET does and create a fake
http context would be extermely difficult.

Kristjan Sander said:
Hello

I am trying to write a #C windows desktop application that loads an ascx
file contents (user control) from file, initiates it and then registers to
database everything it finds. I tried that way:
1 Page page=new Page();
2 Control control=page.LoadControl(virtualPath);
3 string debug = control.ID;

but I got NullReferenceException on the 2nd line.

From earlier post http://www.dotnet247.com/247reference/msgs/29/148858.aspx
I found out that to create a control dynamically, one first needs to
include className into the ascx and to register the control in page. My
brand new dynamically created page object of course lacks that registration
and I guess that's why the LoadControl method does not work.
Is anybody able to explain, how to properly look up the contents of an ascx file?

I would appreciate if you sent all answers directly to
(e-mail address removed) as well.
 
Back
Top