In jsp and php it's very common to have files with pieces of HTML/Java/PHP code and have other jsp/php files consume them. That's fundamental for reusability.
In asp, M$ created another approach, master pages. But I'm having trouble to get used to it, to be forced to have ALL reusable code in a unique file. In example, some apps with many pages may have a breadcrumb area, while simpler apps won't need it.
Yes I can put those blocks inside a ContentPlaceHolder and just add them empty to asp pages they aren't needed. But I'd rather have them on their own files and include them when needed, instead of "removing" them when not needed.
Another example is about menus. Each app has its own menu. In jsp and PHP I can have a local file with the HTML menu and add it. In asp, the best I could imagine is have the "master master page" with an example menu code, then have a local "nested master page" only with local app's menu, and have asp files consume that "nested master page".
Is it the only way to do these stuff? Or is there a better way for doing it?
You have many choices with ASP.NET web forms.
If you just need to share server code, such as utility functions, you can add additional classes to your web project and instantiate/call them just like any other code. Classes added in this manner are accessible to all of your pages.
If you want to share server side code across multiple projects, you can create a new project of type "Class Library," compile it, and set a reference to the resulting DLL from all of your web projects.
If you have a page snippet or section (e.g. a menu system) that is common across pages, you have two choices: custom controls or user controls.
Custom Controls: these are controls written from scratch exclusively using code. Essentially, you write a class which extends Control (directly or indirectly), and you take care of everything. You need to write logic to create child controls that you want to use, and you override the Render method to perform rendering. Typically, you build this control into a redistributable assembly (i.e. a DLL), which can then be used by any ASP.NET applications, simply by placing the assembly in the 'bin' directory of the app (or in the Global Assembly Cache).
User Controls: these control are written using an ascx file, which looks much like an aspx page. i.e. you can simply drag and drop the UI elements that you want to use into the design surface. You don't need to worry about control creation, nor about overriding the Render method.
Sounds to me like you want a user control in this scenario. Here is a helpful walkthrough to get you started.
I will echo scheien's sentiment and tell you that you might want to look carefully at MVC, which is totally different but might fit better with your PHP mindset.
Related
I've started a WPF (web-based) application and it has multiple pages. I want to implement the concept of a Master page in WPF, so that when I make changes in the design/layout of one page then all the others pages will follow suit without all the copy and paste.
I don't think the concept of Master Page is valid in WPF basically because there are no pages.
In WPF exists styles, templates and DataTemplates. Not only that, using the DockPanel and other containers you can achieve most of the functionality provided by ASP.NET Master Pages.
In any case, if you still the need of the master pages, I suggest check this article:
http://www.codeproject.com/KB/WPF/WPFMasterPageControl.aspx
HTH
The WPF Magellan Framework (open source) have Master pages (and other several interesting concepts)
use a master view and put a ContentControl with the master view, then bind the Content property to a property on your data context and set the property to an instance of a UserControl which will then populate the content section.
I will provide some more detail later
You can use global styles to apply a consistent look and feel across your app. As for functionality of a master page. Look at the MVVM pattern. Microsoft has an implementation but there are others.
It let's you define a shell in xaml and specify regions that act sort of like content placeholder's in ASP.NET. Then you write user controls that get loaded into those regions in the shell. It's a lot more involved than using a master page with ASP.NET though. But conceptually similar as far as screen layout goes.
As always, not a bad place to start getting an overview
http://en.wikipedia.org/wiki/MVVM
Tons more stuff on MSDN and so on.
You can use Prism open source framework (downloadable from: patterns & practices: Prism) and using that you can create a Shell Window, which can be used as the MainWindow for your WPF project. Then, inside your Shell window, you can create Regions and inject your views into those regions.
Below is what MSDN has to say about Shell (See: Composing the User Interface using Prism):
The shell is the application root object that contains the primary UI content. In a Windows Presentation Foundation (WPF) application, the shell is the Window object. In a Silverlight application, the shell is the RootVisualUserControl.
The shell plays the role of a master page providing the layout structure for the application. The shell contains one or more named regions where modules can specify the views that will appear. It can also define certain top-level UI elements, such as the background, main menu, and toolbar.
The shell defines the overall appearance of the application. It might define styles and borders that are present and visible in the shell layout itself, and it might also define styles, templates, and themes that will be applied to the views that are plugged into the shell.
Typically, the shell is a part of the WPF application project or primary Silverlight project. The assembly that contains the shell might or might not reference the assemblies that contain the views to be loaded in the shell's regions.
Basic Prism Shell structure with WPF:
I'm an expert web developer knowing all famous web frameworks.
I want to build a collection of Server Controls like ComponentArt components that you can use in your projects.
In ComponentArt: You can easily register the namespace of componentart in your project and bind data to those controls without any additional code or effort (like copying any additional files to our sample projects) and ofcourse they support ajax.
To accomplish this I came to realize that I have to develop some advanced UserControls that support callbacks for handling ajax and there are not much of a information about sharing UserControls amongst different projects. Actually here is the only useful link: http://www.codeproject.com/KB/user-controls/EmbeddedUserControl.aspx
On the other hand Server Controls don't have ascx template interfaces that I can comfortably design my control layouts.
I need a way to develop some web components that you can:
Share amongst other projects easily (Just add dll to project and start
using it)
Support CallBack For ajax(for example ICallbackEventHandler)
Can have a Template for HTML Design (like UserControl that have ascx template
file) so you don't have to hardcode HTML in your code like:
output.Write("<table>content</table>");
Custom Server Controls have the first two, but they don't have ascx files so you have to write all of your html code inside your cs file
Please advise...
Instead of hard coding your HTML you could have your Server Controls read it from a file, whose name and path you determine based on a configuration setting or the directory path. That would allow your Server Controls to be more dynamic and not have a need for recompiling upon changing the HTML. That would help fulfill condition #3 at a slight cost to #1 (since your Server Control now relies on an outside resource it's not as easily distributable). You could always hard code the default markup and fall back to it if the file is not found as an extra precaution against exceptions.
Requirements 1 and 3 are mutually exclusive. The markup has to come from somewhere--either a compiled resource or an external file. Your external file could be optional, but then you have to have default markup specified in the compiled resource. The compiled resource doesn't necessarily have to be your .cs file. You could put it in a resource file, but I'm not sure that's going to be any easier to work with. You could also build your control as a composite of existing server controls, but that's going to make your templating much more difficult.
You need to start educating yourself with books like:
Using this book, and a lot of time, I created a multi column TreeView. Can be seen HERE.
Is it possible to inherit a sharepoint master page(as a parent) in another application's master page which will be treated as child (i.e. after integrating that standalone application with the sharepoint application)? I am totally new to Sharepoint...Any links or article source related to this will be highly appreciated.
Regards,
DC
I would suggest use nested master page
Depends
SharePoint master pages are just other .Net masterpages. However if you're planning to use the same custom.master file, you will most probably receive errors because your stand alone application won't be able to find the SharePoint controls referenced on your .master file.
If you're talking about just a code behind, unless your the parent master class does SharePoint specific tasks, I don't see why you shouldn't be able to inherit from it in your standalone application.
If the reason you want to create the separate application is because you don't think its possible to put in standalone pages into SharePoint.. I've written a blog post to do this here: http://blog.zebsadiq.com/post/How-to-add-a-standalone-aspx-pages-to-SharePoint-navigation.aspx
[Edit]
Nested masterpage theory:
I've never tried this but you could:
Refactor your SharePoint masterpage so that it uses a nested structure
Stick parent.master in the /layouts/MyMasterpage/ Folder, The parent.master file by default will contain all SharePoint components. Wherever you define SharePoint controls, you put a custom placeholder tag around it.
Then create two children master pages, child1.master could then inherit from /layouts/MyMasterpage/parent.master. Additionally it could be installed AS the masterpage of your SharePoint site (via a feature). In theory, your Sharepoint site should work fine because parent.master has everything SharePoint needs, defined on it.
Child2.master could be your inherit from parent.master. It can override all placeholders that contain SharePoint specific controls. While obviously accommodating your forums application.
You may not be able to place a copy of parent.master in a place where both applications can see it but you might be able to duplicate the parent.master and its relating css files for both SharePoint and Forums. (Then you'll have to manage both).
This may or may not work, give it a go.
This may be a broad question because part of the problem is that I actually don’t know what the question is. What I would like to know is how you commonly organise ASP.NET applications in terms of placement of pages (aspx), user controls (ascx), server controls and other support classes and utility functions etc. First, let’s assume that there is already some data layer somewhere (perhaps in a different project). This is the not issue.
The issue I frequently face is that create several pages and realize that they need to share some common rendering logic or some utility function, class etc. Another typical case is that some pages become too large so that it seems handy to split them (say into some user controls). What is the best place to put these utility classes, share classes, user controls, server control etc.? Here are several possibilities.
Don’t really care about any organisation and place all types of files next to each other. So in one directory, you may have an aspx files, some cs files etc. This is not really an option probably.
Organize files by types. Let’s say you create a directory for user controls and put all user controls there. OK, but what about server controls and other regular classes? Should they be in special directories as well? It does not sound right. What I dislike most on this is that when you work on a feature (logically related piece of code), you must hunt it all over the place. I think that features and logical sections of your applications should be also grouped on the file system level in some way.
What I would like to have is to have the pages (aspx), user controls (ascx) and handlers (ashx) basically as dummy placeholders sitting in the directory structure organized from the logically according to the point of view of the outside visitor while the actual code (page, user controls implementations, serve control and utility classes) should be placed in s different folder structured into logical namespaces (organized by the modules or features of the application). It seems to me that the only way to achieve this is to manipulate the <%# Page ... %> directive manually.
Does it sound crazy? Am I asking too much? Is there a better way? What are your best practices? Do you know some good examples?
Edit: Another idea. This does not mess up with the generated aspx, aspx.cs and aspx.designer.cs files. One on my original requirements was that I wanted to place the code driving aspx pages to my own location and put it to a custom namespace hierarchy. So what if I simply subclass the aspx classes generated by VS? Let’s say I have a project called MyApp and MyPage.aspx page in it. VS then creates MyApp.MyPage inherited from System.Web.UI.Page. I leave this class be (no code will go there), but create a subclass, say in MyApp.SomeNamespace.SomeSubNamespace.MyPage, inherited from MyApp.MyPage. This way MyApp.SomeNamespace.SomeSubNamespace.MyPage will get access to the autogenerated protected fields corresponding to the server controls of MyApp.MyPage and I’ll get an entire "private" namespace for all the support classes which are related to this page. Any major disadvantages? Another related problem which bothers me is where should this new cs file be physically placed? In web projects, there is a standard folder for it called App_Code, but I’m interested in web applications. Creating a directory in the root of the application (such as Code) does not sound right.
Remember that you can create page classes that don't actually correspond to any markup. We often create base pages that our actual UI pages inherit from. This is a simple way of organizing "base" page functionality. Then when you create your .aspx pages, make them inherit from the base page class, rather than System.Web.UI.Page.
We usually place our base page .cs files into the top level directory if it's a small project, or for slightly larger projects we'll create a "Shared" or similar directory where they live.
However, we also have a huge enterprise web project, and we simply build our webcontrols and base pages into a class library called CompanyName.Web.UI, with a couple sub-namespaces to that. All our actual web site projects import that assembly and all the code for the controls, etc. is elsewhere. This sounds like it might be a good option for you.
If you remember that your .aspx codebehinds can inherit from any class file, it should make it easier for you to organize.
I have written a data browsing library that is being used in several projects. The library or component includes some classes as well as some ASPX pages.
Until now, we copy all the files into each project to reuse it, with the obvious drawbacks and updating nightmares. I want to encapsulate all the library into a component that the projects can use.
What's the best way to do it? I know I can create a DLL, but I think it's only for the classes, don't know how to go with the ASPXs pages.
We are using Visual Studio 2008 and Framework 2.0
Thanks in advance
This is a perfect case for custom controls.
Change the pages into user controls, and embed the new user controls back into pages that have nothing in them but the reference to the user control.
Once that works, you can change the user controls into server controls, little by little. They will be composite controls, as the user control already is.
There's a trick you can use at this stage: I'm not recalling the details this late at night, but someone else will fill those in: when ASP.NET compiles a page, it first "compiles" the markup into source code, then compiles the source code. You can get ASP.NET to not delete the temporary source files. That allows you to copy into your project code that creates precisely the same HTML as the user control did.
The difference will be - it's now your code and you can refactor it as much as you like, or even include it in a custom control library.