Click to See Complete Forum and Search --> : Access aspx controls from classes
Dreamon
January 2nd, 2003, 01:26 PM
If I have a asp.net page with some Controls on it, and I want to build a set of classes (not the code-behind aspx.cs file), how can I access the Controls in the asp.net file from those classes?
Is it enough just to inherit the System.Web.UI.Page class in my base-class?
Anyone?
Thanks.
pareshgh
January 2nd, 2003, 05:20 PM
you could use asp: tags which are provided in aspx page . like for example
<asp:Label id="lblLink" runat="server" width="150" Height="25" Text="sample" BackColor="beige"/>
this is label with text sample.
you could use javascrip/vbscript along with it to access it and manipulate it . there are serverside controls and client side controls as well.
by same way you could use other controls which don't call back to server and runs itself in client memory and client side.
thanks
Paresh Gheewala
Dreamon
January 2nd, 2003, 07:02 PM
I think you misunderstood me. I know how to use the Controls and how they work with the code-behind class (Somepage.aspx.cs). But, if I then create a brand new class (f.ex. newClass.cs), this class won't have any connection to the aspx page by default (right?). So, my question is; what do I have to do in that class to access the Controls in the aspx page?
Hope I was a bit clearer this time.
Thanks,
TheCPUWizard
January 2nd, 2003, 07:05 PM
If you add a public method to the code-behind class for the aspx file that exposes a control, you can simply register the object with you other class. There is really no difference in this from the way you establish communication between any two object instances.
If you could provide a clearer example, maybe we can help some more.
Dreamon
January 2nd, 2003, 07:09 PM
Hey, that kind of helps me out. I mean, it would suck bigtime (and it would be VERY dirty) if I had to create a method JUST to expose a control to get access to it in a stand-alone class.
Would it be considered completely retarded and dirty to let my stand-alone base class inherit the somename.aspx.cs class? I mean, then I would get access for sure.
Thanks,
TheCPUWizard
January 2nd, 2003, 07:58 PM
That would usually be a bad idea. Inheritance is for specific purposes. This is not one of them.
Consider that if you did follow this path, and wanted to access the controls of a specific (already created) instance of the aspx class. The issues are enormous.
I am also curious as to why you want to access the controls.. A mor typical architecture is to create a class which contains state and calculation information, the form would have a member variable of this "data" class. Other classes could also access the same instance of the data.
This is much more flexable and enhances encapsulation. Consider what the impact is if you currently have a checkbox on the form and later want to change it to a dual radio button. Methods that exposed the control itself would all break. Designs that had proper encapsulation would work without change (a big part of the goal of OOP).
If you could provide more exact specific information on WHAT you are tring to achieve, we could give you more targeted feedback on HOW to achieve it.
Dreamon
January 3rd, 2003, 01:40 AM
Well, the application I'm developing is a Event Calendar application. Select a date (or daterange) and then show matching Events for that date (range).
What I was aiming at with the class question, was to create a set of classes (with a typical base class) that takes care of displaying the different Events. These Events will be presented in different ways (because of polling from different tables in the database). So, what I wanted to do was to create a set of classes that took care of the database handling and displayed the different Events as I would like them displayed.
Hope I made myself clear. (It's late).
Thanks,
TheCPUWizard
January 3rd, 2003, 06:59 AM
Create a set of classes (with a common "GenericEvent" base class). Put all of the common data in the base class. Do NOT have any code about physical representation in theses classes.
Create a set of Web Controls all derived off one common base. Let each of these Web Controls take the appropriate EventType object in its constructor.
Now you can create a sigle page that ties it all together. Put one instance of each WebControl on the page. Using the objecttype you want to display, make the one WebControl that applies visible.
The above leaves out some of the details realting to the DBMS and such, but shoudl give a good idea of the type of architecture we would use in developing this solution.
Dreamon
January 6th, 2003, 12:38 PM
[Create a set of Web Controls all derived off one common base. Let each of these Web Controls take the appropriate EventType object in its constructor.]
I don't know how to do this, but not to worry. I'll look into it.
Thank you so much for your help. I have had problems with thinking in a OO way after converting from ASP to .NET. You got me on track (I hope).
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.