How to make WPF Pages have common interface - c#

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.

Related

Base class for ascx and aspx

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.

Make sure that the class defined in this code file matches the 'inherits' attribute

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.

Access Web Methods from multiple aspx pages

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

Using the same code behind file for multiple .aspx pages

We are developing a site inside a CMS that pushed files to our production server. If the .apsx pages created share the same code behind file, will this cause a problem?
Why don't you let both pages inherit from the same class?
public class MyPage : System.Web.UI.Page
{
// common logic that both pages should have
protected void Page_Load(object sender, EventArgs e)
{
}
}
public partial class PageA : MyPage
{
// specific logic for page A
}
public partial class PageB : MyPage
{
// specific logic for page B
}
Yes. It is technically possible, but it is not a supported way of using ASP.NET and there will likely be gnarly difficulties with it.
You should use User Controls or AppCode instead.
I would suggest to avoid such design, so each page should has own code behind file.
Also take a look at the following ASP.NET features whcih can simplify sharing of common layout and behaviour across the web pages:
Master pages (.master), basically multiple pages which has the same layout could use a shared master page which provides the common layout and supporting code behind logic. Moreover - You can switch master pages in runtime and master pages could be nested, so Master Page could be placed inside of an other Master Page, this gives you a much of freedom and flexibility to design your web application layout and separate responsibilities as well.
User Controls (.ascx), it worth to separate concerns and split page by a set of controls, for instance, it could be LoginControl.ascx,, AdControl.ascx, SearhcControl.ascx, so you keep a code of page clean and each control provides specific functionality and own layout.
Inheritance, so basically set of pages have the same base class which inherited from the Page class (I would not suggest use this approach massively, one base page it is enough)
You may use other common development techniques like dependency injection to share code across multiple pages, this depends on particular case and business goals which were considered under the hood.

Load template from user control

Im working with Sitefinity and I'm developing a Control Designer - however i dont think my question is specific to SiteFinity.
I have a class such as:
public class CaseStudyFeaturedItem : CaseStudySelectorControlDEsignerBase
The class it is inherriting from is itself inheriting from UserControl, like so:
public class CaseStudySelectorControlDesignerBase : System.Web.UI.UserControl {
Within CaseStudyFeaturedItem is it possible to load a template which is an embedded resource and then access the controls on that control?
So essentially, I have usercontrol.ascx which is an embedded resource so has a string like:
mynamespace.myclass.usercontrol.ascx;
And from within CaseStudyFeaturedItem I want to be able to load that usercontrol and then modify the controls (i.e. literals/labels) that are within it?
Is this possible?
Thanks
Al
We do this with every control in Sitefinity, but it would be a little complicated to do with your own custom controls (I assume you are using Sitefinity 3.7). The steps are the following:
- Implement a template container control, inheriting from GenericContainer:
protected class ItemListContainer : GenericContainer
{
public virtual Repeater RepeaterControl
{
get { return base.GetControl<Repeater>("repeater", true); }
}
}
- You need to get the template from the resource (use ControlUtils.GetTemplate method - Sitefinity does that for you):
public virtual ITemplate ItemListTemplate
{
get
{
if (itemListTemplate == null)
itemListTemplate = ControlUtils.GetTemplate(<virtual path to template>, <resource file name>,
<type to determine assembly for template>);
return itemListTemplate;
}
set
{
itemListTemplate = value;
}
}
- You need to call InstantiateIn method of the template, and pass it the container control
listContainer = new ItemListContainer();
ItemListTemplate.InstantiateIn(listContainer);
- Access all controls through the container
listContainer.RepeaterControl.DataBind();
In Sitefinity 4.0 we've included a base class for all controls, which will give you this functionality out of the box. In 3.7 though, you'll have to do all this by hand.
The ControlUtils class is in the Telerik.Framework.Web namespace. The code above is how this all is done in the ContentView control, you should probably make slight modifications depending on your case.
Cheers,
Slavo
The Sitefinity team # Telerik
Yes it is possible, but I'm not entirely sure what you're trying to accomplish based on your question. You can use LoadControl to dynamically load user controls. If you cast the result to the appropriate control type, you will then have access to all of its properties. From there, you can add it into whatever container you want to hold it. Is that the kind of thing you're trying to do?

Categories