1

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?

Hikari
  • 3,797
  • 12
  • 47
  • 77
  • Have you seen this: http://stackoverflow.com/questions/16071925/asp-net-equivalent-of-jsp-include – David Tansey Jun 23 '15 at 17:45
  • 1
    you can use user controls – Alexan Jun 23 '15 at 17:47
  • There's nothing wrong with MasterPages, or nested masterpages for that matter. What you want is resusable components, which would be a UserControl, that are included in the aspx (webform) files where needed. You can even nest UserControls within each other if you want that. **For what it's worth, if starting a new project, go for MVC. You will thank yourself later.** – scheien Jun 23 '15 at 17:58
  • Thanks I'm gonna read the links and about User Controls. I'd like to not use a framework, I've never needed frameworks like Zend, CakePHP, Struts, etc. This is a language/platform issue and not a lack of framework. – Hikari Jun 23 '15 at 18:24
  • You *are* using a framework. You're using Web Forms. You could use MVC as scheien described, and would likely result in a much cleaner implementation. Even if you choose not to use it, you should learn what it is as it's going to be the way forward with ASP.NET. – mason Jun 23 '15 at 18:36
  • lol I'm not, I hate these nipty-nipty asp trash! As I also don't like JSF. When I was forced to, I took 8 times more time to use gridview+linq than I'd take if I'd just use DTO+DAO+simple loop. And the very well paid M$ consultant was totally lost not knowing how to do it properly. – Hikari Jun 24 '15 at 14:24

1 Answers1

1

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.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Interesting, thanks a lot! I'm gonna read this walkthru. Maybe later I try to take some time to look at MVC, but it seems odd to need a framework to do simple stuff that the rest of the world do easily with basic language. – Hikari Jun 24 '15 at 14:26
  • Web forms and MVC are both "frameworks." WIth web forms, you drag and drop controls onto a designer, and the controls emit HTML at run time. With MVC, you actually write the HTML yourself and insert data from the back end as you go. It's much less intrusive to the process and, IMO, much more like PHP. – John Wu Jun 24 '15 at 18:28
  • hmm interesting, I'm gonna look at MVC, I just need time to do it. If MVC provides more freedom to use HTML it may be better. But why not just `<% %>` and echo DTO properties inside it? – Hikari Jun 25 '15 at 12:31
  • You can certain use <% DTO stuff %> on your aspx pages-- in between controls. It's a lot harder to get stuff rendered within a control, which is usually where you're going to want it. You're limited to the extension points provided by the control, e.g. you can use DataSource and DataBind. If you need deeper customization you have to override OnPreRender which can be a real pain in the neck. MVC makes it much easier. – John Wu Jun 25 '15 at 19:00
  • I don't use controls like listview, label, etc. I hate having asp creating dumb invalid html (including tables for layout!!!!), and hate even more when it creates broken JavaScript and floods web server with useless requests (and VisualStudio LOVES to report errors on jquery file!!!!!). It's just total trash, I'm not a dumb noob, I can easily handle HTML, JavaScrit and HTTP requests, and it's MUCH more productive than these lazy proprietary components. What I'm gonna do is create custom user controls, all created with elegant clean HTML. – Hikari Jun 26 '15 at 16:21
  • Good news. I was able to create an example user control and it works fine, and I was able to share it from the libraries project to host projects. I'll explain how I did it in http://stackoverflow.com/questions/31009615/best-way-to-use-a-master-page-and-its-assets-from-a-project-into-another-project. Now I must find out how the host project can extend the library class and make the master page use its extended class instead of the library one... this will be tough! – Hikari Jun 26 '15 at 16:24