I have an entity Profile that I need to access from all aspx and ascx pages in my website. I have created a class "Context" which has the profile property and inherits from System.Web.UI.Page. This worked very well for aspx pages.
I need to apply the same parent to the user controls in my application, my only choice now is to create another Context class "UserControlContext" that inherits from System.Web.UI.UserControls and let the user controls inherit from it.
My problem is that this way i'm duplicating the code for both context pages, how can I use one Context class to let both ascx and aspx inherit from ? What type will that Context class be ?
No, you can't. Your control is either a Page or a Control (or UserControl). They can't share the same base class.
I suggest to move the duplicate code into another class that is called by both Page and Control. You can use the base class Control if you want to in that class, so the code is as generic as possible.
You can inherit only from one class in C# so you can't put the functionality into a common base class for both (as they already inherit from System.Web.UI.Page and System.Web.UI.UserControl). What you can do is to move the common functionality out to some other class and then use that class inside your Context and UserControlContext classes. You would not derive directly from the new class, but you could encapsulate it inside both Context and UserControlContext classes and call its methods from those classes.
When it comes to the question to which extent you can move the code out to the other class, that depends on your code.
Related
I have a page in which page class is inherited with BasePage class like this
public partial class Content_Document_DocumentGuideline : BasePage
so I am able to access BasePage class information like below
int accessPermission = this.AccessPermission;
Now I have a usercontrol in this page.
I wanted to access this base page information in that user control also.
So if I try to inherit this BasePage class in my usercontrol like this
public partial class Content_Document_GuideLinesList : BasePage
I am getting following error:
Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
So if I want to user BasePage class information from usercontrol, what would be the approach?
You should be able to access the members of BasePage from within the user control so long as you only ever use it on pages that inherit from BasePage. You may want to add a property to your user control for this.
public BasePage BasePage
{
get
{
return Page as BasePage;
}
}
Be aware this property will return null if you use the control on a page that does not inherit from BasePage. You may need to make some members of BasePage public if they are not already.
Inheritance is an link between a class that is another class. In your example, A Control is not a BasePage. It is contained in a Page. Think of a Page as the main outside Control container. This page could be a good start to learn. It deals with the difference between inheritance an composition.
About your specific question, there is, in Control class, a property that lets you access the Page that is currently containing it: look at the msdn documentation.
I have a user control which is used in multiple(4 to be exact) aspx pages. This usercontrol have a couple of Get/Save webmethods. Right now, I placed all the webmethods in one aspx page and kept calling the same ones from my javascript. I would like to place them in a central location that all the aspx pages can see, but not sure how/where. Any suggestions please?
Edit:
I know the WebMethods should be a part of a class inherited from 'System.Web.UI.Page'. Is there a better place that I can move these methods to, where js can call from.
try to creating Generic Handler (.ahx) and put all your code there.
or try to creating base page, where the base page inherited with the all the aspx pages
in your aspx page :
public partial class RekapDocumentView : based.PageBase
{
}
in your new class :
public class PageBase : System.Web.UI.Page
{
//your webmethods
}
perhaps this can help
If you try to create a class, VS will ask you if you wont to create a folder for it. All common classes should go in App_code folder. Then you can move your method in that class and reference them from the pages.
How about creating a web service which implements web methods that your user control needs to work properly.
Here is a MSDN article on this topic: http://msdn.microsoft.com/en-us/library/bb515101(v=vs.90).aspx
Hope this helps!
Regards,
Uros
How can I use parent(web page) property in web user control?
One more question:- How can I access the shared class(i.e. class in App_Code folder) property in web user control.
Thanks
You can use this.Page in asp.net user control to refer to page and it will always give you page in which that control is added.
You can access any public class declared in App_code folder directly in any user control without problem. Be careful of namespace and make sure to compile your project if you are having issues to access the class.
You could but I am not sure it would be a clean / full OOP approach, how about setting a public property or calling a public method of your user control from the page passing to it the value you need to use in the control?
this because the page hosting the control should be generic and is the page which contains the control not the other way round.
If this does not fit you, then you can take the control's Page property and cast it to the class of your page then you will be able to access its property but this will make your control specific instead of generic and it will only work when the control is hosted in pages of that exact type/class.
You have to mark the property as Public.
var myVar = ((ParentPageClass)this.Page).YourProperty;
To access the shared class you have to specify the namespace of that class:
YourProject.SomeNamespace.YourClass
or to include the namespace in your .ascx.cs file
using YourProject.SomeNamespace;
It's a cleaner aproach to pass the parameter to the user control from the parent page.
I'm building form validation controls for our C# ASP application. The bulk of the work is handled by a BaseValidator control (subclassing System.Web.UI.UserControl), which also has the markup for the validation output. This is then extended by subcontrols like PasswordValidator, that provides the Validate method and any extra fields needed by that validator control.
(The end goal is to have controls like <uc1:PasswordValidator ControlId="txtPassword" /> which we can plop into any form with minimum duplication.)
However, PasswordValidator.ascx.cs cannot access the form elements defined in BaseValidator.ascx; the only way I've found to do so is to duplicate the markup in each subcontrol's *.ascx file. How can I extend BaseValidator.ascx.cs and access BaseValidator.ascx's markup in the subclass?
I'm pretty sure you'll have to create Server Controls to accomplish this. Meaning, you'll need to generate the outputted Markup from code in the control rather than in the .ascx file.
If you have a true baseclass for your BaseValidator control which your PasswordValidator extends, then any markup/controls in the baseclass should be available through protected properties. I would even go so far as to argue that a true base should not have an ascx portion. The base class should provide methods and properties that expose controls to built on the fly (probably during Page_Init to maintain viewstate).
If you have 2 separate controls on the same page, your parent page can be modified to provide brokerage methods to allow such communication.
If you have the PasswordValidator and you just need the controls/markup, you can use the LoadControl method to create an instance of the BaseControl in memory, access its controls/markup programmatically and either add it or destroy it depending on what you want to do with it.
Barring any of that, it would be just as #Shawn said. Server controls.
Thanks for the suggestions.
I solved this using a service and the strategy pattern. There is a central FieldValidator class which provides validation for any user interface. For the web interface, these methods are exposed through a WebService and (through the WebService) a UserControl. There are no issues with accessing the page elements, because there is now only one UserControl class that stands between the form and the WebService.
For example, <uc1:FieldValidator ControlType="Password" ControlToValidate="txtPassword" runat="server" > will plop down fully-functional clientside and serverside validation, with no need for code behind. Great! :)
(I mentioned resolution a while back in a reply to my question, but I can't mark that as answered.)
We develop a wizard-like WPF application that has a Frame that navigates to various Pages. I define each Page in a separate xaml and xaml.cs file, and then have the application make the frame navigate between the page.
All of my pages currently inherit from Page. I would like to have a common interface to all my pages, so I can access them polymorphicly. However, changing the base class of the Page causes compilation to fail, as the files automatically generated by Visual Studio from the xaml file set the base class to Page, and I get an error that Partial declarations of must not specify different base classes.
One option I have is adding another interface (e.g. WizardPage) and make all the classes implement that interface, but that meas that each page need to implement all the intefaces functions, and this is inconvenient as I want most of the functions, for most of the pages, to have a default empty implementations.
Can you suggest other options I can use to address this?
Thasnkssplintor
You can make the root tag of your xaml to a class defined by you also.
Here you have to derive your base class from Page and then you can derive your page classes from your base class.
public class MyBaseClass : Page
{
.....
}
you can derive your page classes from a class like this:
public partial class MyDeriveClass : MyBaseClass
{
.............
}
and In your xaml write
<y:MyBaseClass y="add your Namesapace here" and add other attribute of Page also like default namespace and xmlns:x>
...............
</y:MyBaseClass>
Make a sort of adapter page that extends Page and implements WizardPage that provides empty implementations of the WizardPage Methods. Then make your other pages extend WizardPage.