Monday, March 26, 2012

Why post back to the same page?

Ok, I've learning ASP.NET and I've only ever really used PHP.

I've bought some books and I'm working through them, but what I'm finding is that every example seems to have you posting the form back to itself, saving the database and clearing the form.

Lets use news articles as an example, in the past I've created a page which lists news articles from a database, with options next to each item to delete or edit, when you select edit you're taken to a new page which has a form on it, when you submit the form the new item is saved and you are returned back to the list. With all the learning materials I've seen, it's like they all expect you to make a page with the items listed on it, then when you select to edit an item the edit form appears on the page with the list of items, you make the changes, they're saved to a database and the list is shown again.

Obviously there'll be ways to send form data to another page which will either save the data or tell you theres been a problem, but why does all learning material seem to want everything on one page? new item form, the list of items, and the edit form.

The default behaviour of an ASP.NET web form is to post back to itself, which is why the tutorials and learning material typically show this behaviour.

To my mind, it's tidier and easier to maintain, and makes sense from the point of view of keeping functionality for the actions associated within a page restricted to that page's code. Even with classic ASP and PHP, my pages always posted to themselves, so it was no big problem for me to get to grips with it. If you think about it, what's the point in creating a separate page to process the results of posting a form?


Thats a fair question really, and it's one that had occured to me when I moved from classic ASP, to ASP.NET.

I don't know if there's a straight forward answer though, other than as it relates to the scope of variables and objects, etc. Certainly, when you introduce AJAX into the page for your edits / inserts, it all starts to make much more sense (partial page updates, etc), but I believe part of the power of ASP.NET is that you don't need to go creating separate pages for edits, inserts, database updates, etc - although you can if you want too.

As my own understanding of ASP.NET grows as it does day by day, I do find myself becoming more and more comfortable with the idea of a page being a little application in it's own right, and extremely powerful. I guess as you become more familiar with the concepts of OO design and programming, it'll become second nature to think of a page for what it is. A class, with methods, events, and handlers, which you can extend.

When it comes to learning material, I believe it certainly helps to keep the number of pages to a minimum so as not to be confusing.

I'm not 100% sure what to tell you to be honest, and I'm probably not the right person to try to answer the question, but I wanted to mention that I thought it was an interesting question!


You can redirect to an Edit form just like in PHP or old ASP instead of posting back to the same form. Postback is a very important and useful feature in ASP.NET where we dont need another form to get the values of the current form, which saves time as well as effort. See this article for details:

http://www.xefteri.com/articles/show.cfm?id=18

Hope this helps,

Vivek


I don't argue that theres benefits of posting back to the same page. In PHP your left having to tell PHP to assign the value of a form field to a variable. i.e. ($name = $_POST['Name'];), now if it's a big form, theres a fair amount of work involved, and usually I never get it right first time and make spelling mistakes somewhere.

Sometimes I put specific stuff into it's own DIV, for example, the list of items in one DIV, the edit form in another. So that when the edit form appears I can use javascript to hide the list DIV. Can the same thing be done in ASP.NET without using javascript directly? maybe similar to how you'd make a textbox not visible i.e. (nameTextBox.Visible = false), can you set a div like formDiv.Visible = false;

It's a bit strange learning something else, I've done PHP for the last 4 year, everything I've done is in PHP. Now not only am I learning a different language, it's a fairly different way of doing things as well. The learning curve is failry steap, but having knowledge of PHP, IF statements, SQL etc all help make using ASP.NET eaiser to learn.

0 comments:

Post a Comment