+2  A: 

I typically write out the html in the code behind.

That part is a little odd, and not something I recommend for webforms. If you want to do that, consider an asp.net mvc project instead.

In webforms, you really want the meat of your html to live with the markup rather than the code. The two should remain separate. You also don't want a huge stringbuilder that encompasses your entire page. This will force you to keep the entire page in memory twice (once for the stringbuilder bytes and once for the built string at the end) rather than writing the page to the response stream as it's built. That means more memory per request, which can really kill scalability.

To those ends, I would abstract distinct portions of your stringbuilder code into custom/user controls that you can use in the aspx markup. These controls can use a stringbuilder to create their output. This means you only need to keep enough html markup in memory to render one control at a time. It also allows you to more easily re-use common markup across pages or even sites.

Joel Coehoorn
Ok. I wonder what the benefit is of say stuff like <List>, etc. in 3.0. I've been able to do pretty much whatever I needed with writing things out with the StringBuilder. I've not ran into many situations where I felt like I needed something that approach couldn't do.
d3020
I don't recommend a List in 3.0 either, for the same reason: it pulls everything into memory at once. Use the leaner IEnumerable instead, because you can use that to only keep one element in memory at a time.
Joel Coehoorn
A: 

A big problem you have with that it can get pretty messy... having to escape all the " or messing with carriage returns. Sure YOU can program around that, but what if you want to copy/paste code? sounds like a nightmare and WAY more work than it's worth.

Joel
Yea, there are times when the tick marks can get a little tricky but you get familiar after a while and it doesn't seem like that big of deal. I'm ok with doing this approach. I just found myself not using the .net controls and asking myself, is this right. Wasn't sure if I was heading down a wrong track, but like I said. It works. I read some of the 3.0 stuff and I'm not doing much of that like with <List>, etc. I've really just not had a need.
d3020
+1  A: 

There are times when you need to generate some HTML in your code behind, but in general, you want to leave the HTML where it belongs, and that's seperated from your code. The VS IDE is a pretty good HTML editor. Use it.

Anthony Pegram
A: 

It sounds like you should be writing a custom control and using HtmlTextWriter to write the markup.

Or perhaps more appropriate would be a user control, with markup in the aspx page and anything else in the code behind.

Matt Olenik
A: 

If you're using this approach, you should migrate your development efforts to ASP.Net MVC. Whereas ASP.Net actively tries to abstract the HTML, CSS, JavaScript, etc. away by using web controls, ASP.Net MVC is built around a paradigm of directly controlling the markup itself (though that may arguably be the least of the differences between the two - you should definitely read up on it to at least know the alternatives, even if you stick with ASP.Net in the long run).

Otherwise, what you're doing works if done properly (though you'll be fighting the framework the whole way), though I'd recommend using a StringWriter instead. It uses a StringBuilder internally so the performance characteristics are the same between the two, but the semantics are more consistent with the rest of the .Net framework (e.g., Write vs. Append).

Dathan
A: 

I think this approach kind of defeats the purpose of what webforms was trying to accomplish (separating markup and code).

Giovanni Galbo
I think you're right. And as odd as it might sound. This is the only way I really know how to do web form development. I know, strange, but it's how I learned it and just sort of stuck with it. Now I've got myself a big project filled with this approach, so...
d3020
@d3020 my one bit of advice to you then is don't caught in the "well I'll keep doing it this way to keep consistent with what's already there" trap. I've fallen in that trap many times. Always make improvements with new code and when you can update the old code, little by little.
Giovanni Galbo
I agree man - at least in my head I do. It's actually why I reached out and asked here. But like I said, I don't know at this point if MVC is actually the right choice. I'm pretty sure I need to at least start seperating things out a bit more into maybe classes - which I don't have right now. I find sometimes repeating code of say a certain function on different pages. I probably could benefit from having say a separation from my data access and StringBuilder stuff, whereas now I'll just call the EntitySpaces select and loop through all within one function in my code behind.
d3020
+1  A: 

I'm going to go out on a limb and guess you may have come from a "Classic" ASP (vbScript) or PHP background.

My back ground is "Classic ASP" and my first attempts at the Webforms Model were pretty much the same as yours, once I started usnig them and understanding them I've never looked back. There is a disctinct learning curve though in understanding how the page life cycle interacts with the various WebForm controls.

Look up the various threads on ASP.net WebForms vs MCV to see which suits your projects needs the best. MVC Isn't a magic cure-all but in many respects may be more familiar if you're from a "Classic ASP" or PHP backgound.

From a practical perspective, assuming you're sticking with WebForms, if there is the possibility of other developers becoming involved in the project you aim towards using more of the inbuilt controls where you can as that is more than likely what they will be familiar with. Stating the obvious, the more you use the controls the more you will become familiar with what they can and can't do and before to long you will find yourself writing your own controls to fill the gaps or finding existing 3rd party controls.

Jon P