views:

660

answers:

6

I am sorry for possible misleading about the title, but I have no idea for a proper title.

Feel free to edit.

Anyway, I am using ASP.NET Web Forms, and maybe this isn't how web forms is intended to be used, but I like to construct and populate HTML elements manually. It gives me more control.

I don't use DataBinding and that kind of stuff. I use SqlConnection, SqlCommand and SqlDataReader, set SQL string etc. and read the data from the DataReader. Old school if you like. :)

I do create WebControls so that I don't have to copy-paste every time I need some control, but mostly, I need WebControls to render as HTML so I can append that HTML into some other function that renders the final output with the control inside.

I know I can render a control with control.RenderControl(writer), but this can only be done in (pre)Render or RenderContents overrides.

For example.

I have a dal.cs file where is stored all static functions and voids that communicate with the database. Functions mostly return string so that it can be appended into some other function to render the final result.

The reason I am doing like this is that I want to separate the coding from the HTML as much as I can so that I don't do <% while (dataReader.Read()) %> in HTML and display the data. I moved this into a CodeBehind. I also use this functions to render in the HttpHandler for AJAX response.

That works perfectly, but when I want to add a control (ASP.NET Server control (.cs extension, not .ascx)) I don't know how to do that, so I see my self writing the same control as function that returns string or another function inside that control that returns string and replaces a job that would RenderContents do, so that I can call that function when I need control to be appended into a another string. I know this may not be a very good practice.

As I see all the tutorials/videos about the ASP.NET MVC, I think it suite my needs as with the MVC you have to construct everything (or most of it) by your self, which I am already doing right now with web forms.

After this long intro, I want to ask how can I build my controls so I can use them as I mentioned (return string) or I have to forget about server controls and build the controls as functions and used them that way? Is that even possible with ASP.NET Server Controls (.cs extension) or am I right when I said that I am not using it right.

To be clear, I am talking about how to properly use a web forms, but to avoid data binders because I want to construct everything by my self (render HTML in Code Behind).
Someone might think that I am appending strings like "some " + "string", which I am not. I am using StringBuilder for that so there's no slowness.

Every opinion is welcome.

+5  A: 

You need to stop thinking in terms of "controls" and webforms. MVC is a completely different way of constructing applications.

I also hate the automatic renderers in WebForms, they produce horrible html that never makes any sense. However, you dont want to be writing your html in your codebehind and passing it around as strings, thats just nasty. Your presentation code is mixed in with your logic, AND youre writing HTML in c# strings!!!

So, MVC... Instead of "widgets" that do interacty things with codebehinds and postbacks, yours view ONLY display data and contain forms to allow you to post to the controllers.

Because of this, you can strongly type your views to a Type, and then access the data you pass to it from a controller via the Model property. The equivalent to UserControls are Partial Views (ViewUserControl) which can be used to modularise your rendering code for types. For example, you might make an Address partial to which you pass your Person's Address property every time you need it rendered. This way you aren't repeating html all over the place.

P.S. A single file for all your DAL?

Andrew Bullock
I was talking about web forms. I thought this was clear, but sorry for the confusion.Yes. DAL is in one file, but I will separate it in smaller parts as it becomes bigger and less maintainable.I am still learning about n-tier applications so I need some practice to get it right.Thanks for reply.
lopkiju
+6  A: 

I hope I never ever have to work on an app you wrote in this manner. If your application is string-heavy then something is wrong.

sliderhouserules
+1  A: 

Agree with @sliderhouserules, the way you are using MVC framework is awful. You must forgot all your "old school" techniques. You should never use SqlCommands, SqlReaders, etc. in the code of the pages. You should pass to the view only a model (e.g. View(bar)) and it will be better if you avoid usage of

ViewData["some magic string"] = bar

Every time when you will use "old school" technique 2 mans and 2 cats will be killed :).
Also it's better to use some ORM (Object-Relational Mapper) like Linq2sql, NHibernate, SubSonic, etc.
If you need in samples of good application design please look at SharpArchitecture. It has a very good architecture and implementation and may help a lot. It has one sample (with Northwind db) and one more sample will be added soon.
Also look at CodeCampServer. It has very good architecture too.
It's better to look at the code of these projects instead of looking videos because existing videos can't demonstrate good sample of architecture, just a simple usage of functionality.
About server controls, you may use them if they can be used without 'runat="server"', like PlaceHolder. And you may create them too, but you shouldn't load any data in them directly. If you don't want to copy-paste html you should review your code and you should refactor it. Every duplicated code should be moved to MasterPages of UserControls (ascx ones).
And once more, please spend some time to look at these samples. You'll save your nerves and time in the future when you'll need to update the app or fix something. At the first look they can be hard to understand but this is only at the first look. Hope this helps.

zihotki
I thought that I will make a confusion mentioning MVC.All I wrote was about web forms. I just mentioned MVC as a better tool for me then web forms since you have to construct your tables by your self.Sorry for the confusion and thanks for the reply.
lopkiju
No problem :)And mvc is not better tool, it's just another tool. It has a lot of advantages but has disadvantages too. It's better to use webforms when you need to create site fast and it will be data-driven (a lot of grids, etc). But I prefer MVC, it's very powerful and comfortable for me :)
zihotki
I like the idea of MVC. Anyway, the point of my long post was the proper use of controls in ASP.NET web forms. I don't know how to display a data when the func that communicate with the db is in separate file (without DataBinding) so what I did was string append and call the function where needed.
lopkiju
it is a better tool ;)
Andrew Bullock
I do think that MVC is better too, at least for me. Web forms work with automatic generation which I am not fond of, but I realize that automatic generation could save some time, that's why I think about making my own automatic table generator. :) I have a better control over it that way.
lopkiju
A: 

If you want the ASP.NET MVC path, you can set up controls as ASCX and they will simply be tags filled by the controller. It may not be old school enough for you, however.

You can create your full UI in code behind, if you so desire. This can include creating the HTML in routines.

But, I would recommend, if you are sticking with ASP.NET, reconsidering the bind model over the DataReader.Read() and output + loop. It is not only old style, it is highly inefficient and hard to maintain.

ASP.NET MVC does create a much looser ASPX, as it is just a view. And there is a much greater separation of code and UI. But it will not fit with the old school model either.

Gregory A Beamer
A: 

Have you considered using micro-templates? Dave Ward has a good example of of a client side data repeater that is uses micro-templates for layout after making a call to a page method. This sounds like this is more in the spirit of what you are trying to accomplish and can still be integrated nicely with WebForms.

The side effect will be that you will not rely on passing around HTML and can isolate your presentation from your logic.

David Robbins
A: 

@lopkiju, I think that the MVC pattern would serve you much better than your current WebForms solution if you want that much control over the output HTML.
You can use Web Forms this way as you already do, but it is not designed to be used this way, so it will be a pain.

More detail

In my opinion, read some articles about the Separation of Concerns (also known as SoC) principle. If you apply it correctly, it can save you many-many headaches when you'll debug your app, and also for those people who you work with (eg. those who may have to read or modify your source code).

My other tip for you is this:
You are right that you shouldn't do things like <% while (dataReader.Read()) %> in your View code. But perhaps there are better ways to make it more elegant than your current way.

I think you should consider using some sort of ORM for these purposes. (LINQ to SQL, or even NHibernate.) If you get it, it will be much simpler. So much that you may not want to use DataReaders directly again. :-)

What I recommend for you

Simply, read the NerdDinner tutorial, and build the samle app by yourself step-by-step.
After that, try to build a similar app that serves a different purpose by yourself while applying the same rules and design that you applied to the tutorial.

I'm pretty sure you have the expertise to do it, and after actually doing something with it, you can pretty much get the feel of it.

The tutorial also explains and includes the principles I mentioned above, which will be very much use to you.

Venemo