By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 1,922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

Page.LoadControl from within a class?

P: n/a
I am trying to load a UserControl which inherits from one of our customer
classes which in turn inherits from UserControl. We use Page.LoadControl()
from within our ASP.NET pages to load these controls but would like to also
execute them form within a class not necessarily with a Page reference. Is
there a way with Reflection or any other method of executing a UserControl
without a specific page reference? Is it possible to create a new page
reference by any chance? Thanks so much I appreciate it!

Christopher
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Think through your strategy.

One of the main things we try to do is seperate our business login from our
user interface. Having a Customer Class, which is a business class, I'm
assuming, serive from a UI Element is going to cause some major
restrictions, which you just found out.

Try deriving your Customer Class from System.Object. When you need to
integrate that with a UI Element (e.g. CustomerDisplayControl) then have the
control expose a CurrentCustomer Property whose type if a Customer Class.


"Christopher" <Ch*********@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
I am trying to load a UserControl which inherits from one of our customer
classes which in turn inherits from UserControl. We use Page.LoadControl() from within our ASP.NET pages to load these controls but would like to also execute them form within a class not necessarily with a Page reference. Is there a way with Reflection or any other method of executing a UserControl
without a specific page reference? Is it possible to create a new page
reference by any chance? Thanks so much I appreciate it!

Christopher

Nov 19 '05 #2

P: n/a
Yes, that does make sense though separating business logic from UI isn't
exactly why we were needing this function. Our main goal is to be able to
tell a developer "write some c# code here in this text file and our XYZ
product will simply run it at some point in time". For example, say our
product is a scheduler program that just sits open all day and maybe once a
day executes a UserControl that someone wrote. We actually don't need any UI
at all, we're just looking for a way to have people write code in script and
our product pick up that code, compile it at runtime, and fire methods on it.
We considered UserControls for now mainly because you can load them, cast
them to a generic class and fire predetermined methods on them is all. Sorry
about that confusion, but you're absolutely correct on the other point on
business logic! Thanks again though!

Christopher

"David Jessee" wrote:
Think through your strategy.

One of the main things we try to do is seperate our business login from our
user interface. Having a Customer Class, which is a business class, I'm
assuming, serive from a UI Element is going to cause some major
restrictions, which you just found out.

Try deriving your Customer Class from System.Object. When you need to
integrate that with a UI Element (e.g. CustomerDisplayControl) then have the
control expose a CurrentCustomer Property whose type if a Customer Class.


"Christopher" <Ch*********@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
I am trying to load a UserControl which inherits from one of our customer
classes which in turn inherits from UserControl. We use

Page.LoadControl()
from within our ASP.NET pages to load these controls but would like to

also
execute them form within a class not necessarily with a Page reference.

Is
there a way with Reflection or any other method of executing a UserControl
without a specific page reference? Is it possible to create a new page
reference by any chance? Thanks so much I appreciate it!

Christopher


Nov 19 '05 #3

P: n/a
Have you thought about defining an interface for the functionality, then?
That way you could compile the class at runtime, load that assembly/type and
then interact with it through the interface that you defined. That way you
can define specific units of functionality for the interface. Heck, you
could even create a libraby of interfaces for different types of interaction
instead of always assuming that the single Load method is the only thing
that you can interact with.
"Christopher" <Ch*********@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
Yes, that does make sense though separating business logic from UI isn't
exactly why we were needing this function. Our main goal is to be able to
tell a developer "write some c# code here in this text file and our XYZ
product will simply run it at some point in time". For example, say our
product is a scheduler program that just sits open all day and maybe once a day executes a UserControl that someone wrote. We actually don't need any UI at all, we're just looking for a way to have people write code in script and our product pick up that code, compile it at runtime, and fire methods on it. We considered UserControls for now mainly because you can load them, cast
them to a generic class and fire predetermined methods on them is all. Sorry about that confusion, but you're absolutely correct on the other point on
business logic! Thanks again though!

Christopher

"David Jessee" wrote:
Think through your strategy.

One of the main things we try to do is seperate our business login from our user interface. Having a Customer Class, which is a business class, I'm
assuming, serive from a UI Element is going to cause some major
restrictions, which you just found out.

Try deriving your Customer Class from System.Object. When you need to
integrate that with a UI Element (e.g. CustomerDisplayControl) then have the control expose a CurrentCustomer Property whose type if a Customer Class.

"Christopher" <Ch*********@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
I am trying to load a UserControl which inherits from one of our customer classes which in turn inherits from UserControl. We use

Page.LoadControl()
from within our ASP.NET pages to load these controls but would like to

also
execute them form within a class not necessarily with a Page reference.
Is
there a way with Reflection or any other method of executing a

UserControl without a specific page reference? Is it possible to create a new page reference by any chance? Thanks so much I appreciate it!

Christopher


Nov 19 '05 #4

P: n/a
We've considered an interface but aren't sure yet about it, but it does sound
very promising. If we had defined say a IPlugIn interface with a Run()
method say for simplicity sake, a UserControl would inherit from this
Interface correct? How would a Windows Service or other running app outside
the scope of a web request run that UserControl though? Right now we use the
LoadControl method but that's only good if you have a page request it looks
like, meaning if we fire say 100 Run() methods within one request, the
request may time out. Below is what we are hoping to accomplish. Once again
thanks for all your help this has been biting our team for a while now!

1. User clicks a button in the web app
2. In the button OnClick event, the code tells the web app to raise a
particular event named say "EmployeeAdded"
3. The web app then starts to execute the event handlers for each
UserControl which is subscribed to that event. The trick here is that the
UserControls may take a very long while to run so they need to run almost run
in the background while the initial button press click event completes,
whether that's a windows service or another process started etc.
4. User continues on without actually knowing that other event handlers are
running on the server-side. No notification is necessary to the end user in
our case.
When we say "event handler" etc we don't necessary mean the csharp event
mechanism as may need to somehow devise our own less elaborate mechanism
since it's so asynchronous etc. but again we aren't sure, the question for us
has been how to parse and run a UserControl from outside the scope of one
HttpRequest.

Thank you once again!

Christopher


"David Jessee" wrote:
Have you thought about defining an interface for the functionality, then?
That way you could compile the class at runtime, load that assembly/type and
then interact with it through the interface that you defined. That way you
can define specific units of functionality for the interface. Heck, you
could even create a libraby of interfaces for different types of interaction
instead of always assuming that the single Load method is the only thing
that you can interact with.
"Christopher" <Ch*********@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
Yes, that does make sense though separating business logic from UI isn't
exactly why we were needing this function. Our main goal is to be able to
tell a developer "write some c# code here in this text file and our XYZ
product will simply run it at some point in time". For example, say our
product is a scheduler program that just sits open all day and maybe once

a
day executes a UserControl that someone wrote. We actually don't need any

UI
at all, we're just looking for a way to have people write code in script

and
our product pick up that code, compile it at runtime, and fire methods on

it.
We considered UserControls for now mainly because you can load them, cast
them to a generic class and fire predetermined methods on them is all.

Sorry
about that confusion, but you're absolutely correct on the other point on
business logic! Thanks again though!

Christopher

"David Jessee" wrote:
Think through your strategy.

One of the main things we try to do is seperate our business login from our user interface. Having a Customer Class, which is a business class, I'm
assuming, serive from a UI Element is going to cause some major
restrictions, which you just found out.

Try deriving your Customer Class from System.Object. When you need to
integrate that with a UI Element (e.g. CustomerDisplayControl) then have the control expose a CurrentCustomer Property whose type if a Customer Class.

"Christopher" <Ch*********@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
> I am trying to load a UserControl which inherits from one of our customer > classes which in turn inherits from UserControl. We use
Page.LoadControl()
> from within our ASP.NET pages to load these controls but would like to
also
> execute them form within a class not necessarily with a Page reference. Is
> there a way with Reflection or any other method of executing a UserControl > without a specific page reference? Is it possible to create a new page > reference by any chance? Thanks so much I appreciate it!
>
> Christopher


Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.