Using the same code behind file for multiple .aspx pages - c#

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.

Related

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

Handling Master Page Panels with Base Control Class

I have admin module. This module have one master page which has the no of panels at left side. I have to make this panels visible and invisible with base control class which is constructed in VB language. I'm working with asp.net 2.0 with c# web site.
Please give me idea how do i construct this base control class and manage panels with Master Page on link button click event.
We'll there is a plenty of opportunities to make it.
I'd prefer using of facade pattern.
Construct a class which would use VB class inside:
public newManageClass()
{
private oldManagedVBClass _old;
//.ctor
public newManageClass()
{
_old = new oldManagedVBClass();
}
public void makePanelsVisible()
{
_old.MakePanelsVisible();
}
}
And then you can use this class inside your master page as helper class.

Re-using another page section

I have created a web page that I use as a small dashboard to hold issue or no issue. It works great. The page uses an .aspx and .aspx.cs. I would like to be able to reuse the information on this page on other pages. My site already uses master pages and I have not been able to find an easy way to include this information.
How can I use an include from a page that has coding in the code behind easily?
Typically you use Web User Controls for this.
Web User Controls allow you to package up other controls into one that you can drop onto multiple pages. They are great for common UI items such as address entries, dashboards, etc. Basically anything that needs to be the same across multiple pages.
At the risk of seeming very obvious - do you mean usercontrols. These will allow you to reuse chunks of functionality across your site.
I guess this question falls into two categories: User Controls, and Code Reuse. Not sure which one you are after.
User Controls
If you are talking about the controls on your page you will want to create a common user control.
Code Reuse
You need to create a common class (whether it is static or not depends on how you intend to use it) and define functions within that class.
For instance, lets say you have a page that you want to print "Hello World!" on any aspx/.cs page.
You could do this
public static class MyClass
{
public string PrintHelloWorld()
{
return "Hello World!";
}
}
Then you call it from any of your pages like so:
MyClass.PrintHelloWorld();
Right click on the project > Add New Item...
Select User Control (.ascx)
Put your markup & code behind there.
Then you add that control in any other page (includding other controls [although I wouldn't recommend that])
It sounds like you may want to create an ascx User Control.
http://msdn.microsoft.com/en-us/library/ie/2x6sx01c.aspx

How To retrieve a control in a Master Page from Content Page

When I asked recently a question about how To retrieve a control in a Master Page from Content Page.
Many peoples suggest me to use this code from my Content Page:
Label lbl = this.Master.Page.FindControl("uxLabel") as Label;
//Note any server controls defined in the master page could be not be accessible even after a cast is performed, because they could be marked as protected
This approach certainly works, I also realize that is available a strongly-typed solution that doesn't involve casting the Master property.
In the Master Page place:
public Label HeaderLabel
{
get { return uxLabel; }
}
Using a MasterType in the Content Page:
<%# MasterType VirtualPath="~/Templates/WebsiteMasterPage.master" %>
Now it is pretty easy find the control from the Content Page:
protected void Page_Load(object sender, EventArgs e)
{
this.Master.HeaderLabel.Text = "Any Text here!";
}
I would like to know:
what do you think about this
approach?
any other solution?
Thanks for your time
My answer is "why not?".
Both are for me good approaches but first needs less coding in order to get started with it, since you don't need to initialize any class field and design properties. But control must be found during run-time.
Second approach, call it "typed approach", is just cast to specific master page class and you get access to any class-specific member.
What would be the main problem of "typed approach"? You need a reference to the class library (assembly) in order to access to such master page's members, which wouldn't be desirable in some scenarios. For example, you've a control library and you want to access a mandatory master page's control which provides some behaviors needed to work with some library's control. You would need to reference your web client assembly, but you can't do that because you reference your control library in the web client itself, and this is a circular reference.
Tools, approaches are there for use in specific scenarios. Why not? answer can be expanded to why not to use "typed approach" if it's needed and your scenario is compatible with that concept?.

ASP.NET MVC ViewPage rendered in a ViewPage

I'm wondering if it is possible to render a ViewPage inside of a ViewPage.
Normally, you'd have this:
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<my_object>"%>
and then in the page you can render a user control:
<%Html.RenderPartial("~/Views/Shared/mycontrol.ascx", this.ViewData);%>
However I'd like to render a ViewPage in a ViewPage. The reason? I'm working on a very large project with 10+ developers and a lot of code already written. A large piece is already written which dynamically renders the UI however there are 15+ methods which pass around a ViewPage.
In other words, I'm calling this in the ViewPage:
<%this.RenderUI();
And the method stub is this:
public static void RenderUI(this ViewPage viewPage)
So in order to reuse the most code, I'd like to pull the call to this.RenderUI() into it's own ViewPage but then include that on some ViewPages. It's a hairy situation and it sounds unnecessary, but there's a lot of code rework that would need to be done otherwise. Does anyone know if this can be achieved?
Render your subview in the main view using RenderAction() instead of RenderPartial(). This allows you to keep your controller and view for each subview, and "inject" the output of each controller/subview combination into any main view, in a location of your choosing within the main view.
This arrangement should ease some of the complexity of managing each of the views and subviews. It is an approach reminiscent of the "web parts" in traditional ASP.NET or SharePoint. It is a more modular approach that provides for better reusability.

Categories