views:

263

answers:

3

Hi,

I work on asp.net applications 'in-house' and would love to use asp.net mvc, but am not able to as our applications are deployed to web servers that are not in our control and currently only have .net framework v2 and IIS6. As well as there being no possibility of having time/money spent on converting current WebForms applications...

So any information to help me create better WebForms asp.net applications would be greatly appreciated...I've read the "what sucks" list about WebForms from the MVC people for which surely there are solutions/work-arounds to make WebForms "less sucky"...

stuff that I sort of understand that could be improved (but need help on):

  • remove viewstate for pages that do not postback (remove the runat="server" tag?)
  • minimise usage of viewstate - using <span><asp:literal /></span> instead of an asp:label (we never use the style properties of server side controls but use the CssClass attribute mainly to keep CSS in the CSS files)
  • use ashx handlers for ajax requests (can this be furtherd?? e.g. a postback to a handler?)
  • can viewstate be removed completely and still postback? (is it reading the form variables instead of reading control values?)
  • can you turn off the viewstate programmatically if you know the user has javascript? e.g. then an ajax request can submit the form but will work if javascript disabled?
  • we already keep all our business logic in a BR/BL layer and we have a similar DAL layer for database access...so aspx code behind is generally fairly light...(validation is also in the BR/BL so that responding to an ajax request or a postback for the same task requires little (or no) duplication of code)
  • use of less components...(less server controls...to some degree this also means using controls that won't have the crap IDs)

i don't really care for the neatly formatted URLs as its intranet based (and so pretty much irrelevant) and SEO is of no concern...even though the MVC urls look brilliant...

Again, any help, resources, code samples or whatever would be awesome! thanks heaps.

A: 

For the ViewState questions, make sure you understand the difference between ViewState and ControlState. These articles helped me with that. I turn ViewState off in the web.config for my existing Webforms apps and I don't have any issues. I also avoid any of the *View controls and stick mostly to PlaceHolder, Literal, Repeater, Button, TextBox, DropDownList (can act quirky without ViewState, but nothing showstopping) and other basic controls. You can postback with ViewState off.

ASHX works great for Ajax requests. I wouldn't take it any further than that though.

I think you're on the right track. You're already separating your layers and putting stuff where it should be and you seem to know where things should go, and knowing is half the battle.

John Sheehan
read that article and feel dodgy for all the viewstate pollution i've no doubt caused...EXCELLENT link! Also that it clarified the viewstate is not necessary to get form values during postback (what i now know is a myth)
davidsleeps
+2  A: 

Can add to this

use ashx handlers for ajax requests

Use Jayrock to handle Ajax requests. This is very much like MVC with only JsonResult.

can you turn off the viewstate programmatically if you know the user has javascript

You can detect if JS is enabled using this approach. Thus fairly easy to disable ViewState. Can be easily handled in base page if you have such. So the amount of repeatative code -> 0.

Additinaly you could find WebFormsMVP helpful.

Dmytrii Nagirniak
Yep, was going to add MVP. Not really as nice as MVC, but for WebForms, it is about all you got.
Martin
MVP looks useful, but: Requires ASP.NET 3.5 SP1
davidsleeps
A: 

Thought i'd add to this post (for when someone comes across it) the following link which is quite useful when using jQuery (or javascript for that matter):

from Rich Strahl's blog: jQuery Form Serialization without ASP.NET ViewState:
http://www.west-wind.com/weblog/posts/472329.aspx

davidsleeps