Entries in ASP.NET (2)

Saturday
Mar012014

Partial Pages in ASP

I wrote some code. Imagine that! If someone were familiar with this backup tracker project, but not with me or my family, it would appear that I started testing out partial pages, then fled in terror for 3 months. Not true! While the specific implementation of partial pages in ASP.NET is new to me, I understand the benefits.

The basic idea is to write a piece of a page that you can use multiple times in other pages. In this particular case, that's a form. Whether you want to add a backup/harddrive, modify an existing entry, or delete one, you need some kind of interface for entering that data. If you want to add a new entry, the form is blank. If you want to modify an entry, you pre-populate the form fields with the data about the harddrive or backup and allow the user to edit some or all the fields. If you want to delete, you pre-populate the form, restrict the ability to edit, and make it super-duper clear that you're deleting something. Partial pages lets you write the big chunk of that page -the form- once and use it in different contexts by inserting it in other pages.

This was a new way of thinking about this problem for me. I'm mostly self-taught, and I started with PHP. When I needed to do add/edit/delete forms for a project in PHP, it took a while to figure out how to do it. My first thought was to just write 3 pages, but I rejected that idea almost immediately. Copying code like that is a bad idea. What if the form has to change? What if something with the database changes? What if someone else has to change something, and doesn't know to change the other two?

So the next thing I thought of was to do all of them as a single page. This seems like a good idea until you try to do it. The hard part isn't really writing the code, it's trying to understand it later. Now I'll admit that I probably didn't hit upon the best way to do this particular idea, so maybe there are ways to do it that are easier to manage or make more sense. My pages were a mess. The problem is you need to account for 3 different complex tasks, all while trying to use the least possible amount of code. You want the add/edit/delete options to use the same form or you run into the problem of having 3 completely separate pages. The solution is to use variables in the form, and account for them in the code blocks for the 3 specific options.

So what I did was split the page into 3 parts. The first part was Setup, where there was some code to figure out what you want to do, and prepare the correct data for that option. If you wanted to add an entry, it would use blank variables so the form would be empty. If you wanted to edit it, then it would get the info from the database and put it in the variables to pre-populate the forms. The delete would do the same as modify, but also add a little piece to the form that made the fields uneditable. This section would also define the instructional text above the form, change the title of the page, etc.

The second part was the actual page. This was mostly variables, since the title should be different for each, and instructions. This is where the actual form code was written, but it was almost unreadable because of all the jumping in and out of PHP code, or using echo or HEREDOC statements to build the forms.

The third part was the Results section. This is where it would attempt to validate the data, and perform the final database option of adding, editing, or deleting, and display the results. Sometimes this section was bigger than the other two combined if a lot of work had to be done on the data before interacting with the database.

To make things more complicated, I usually did forms on a single page and split them up by how they interacted with the server. This is get and post. When you first request a page, you use GET to retrieve it from the server. When you're submitting data, you're using POST. So if the server got a GET request for the page, it would display the form. If it was POST, it would execute the form processing steps and then add it to the database. This meant my 3 part page was further split into 2 sections, with the Setup and Form parts in the GET section, and the Results part in the POST section.

The end result was a huge, complicated, almost unreadable page that worked, but god forbid I ever need to modify anything in it. It's ridiculous, I would be afraid to change anything because it was so hard to see what the results of that change would be. Would I break another part? How would I ever find that part again if it did break? What a mess!

Now using partial pages doesn't completely negate all that, but it does make it easier to manage. That changes the actual form from taking up easily 40+ lines for a simple form to a single line of code to render that as a partial page. This makes using separate pages for each action far more attractive. You still run into the problem of adjusting for a database change, but changing the form is simple. It's also easier to keep track of what you're doing when you don't have that big chunk of form code sitting in the middle of your work. It makes all the code (except the one line) for those pages only about the task of that page. It was also pointed out to me today that there are other options for doing the delete that don't require the form, so that makes things even easier.

Now the only trick is actually writing it. I'll report back on that later.

Tuesday
Dec172013

C#, ASP.NET, and Other Stuff. Mostly Other Stuff

I haven't written anything in a while.  Not intentionally, I just find that this blog is the first thing to go when I get busy with other things.  I also need to address something that I feel is weird. It involves the nature of this blog. I don't know if it's generational, or a personality thing, but I just have trouble with social networks.  Anytime I think about starting to use one, I can't help but ask "Who cares?"  To which I never have a satisfactory answer.  I don't own a business or have some cause that I'm promoting, and while I happen to think that I'm endlessly amusing, I'm also self-aware enough to know that very few (if any) share that opinion.  I have other ways to keep in touch with people I care about, so I can never muster a good reason to get involved with any of the networks beyond lurking.  But I know that anyone in a technology field looks like a weirdo without some kind of online presence, so I got this website.  I don't know why, but writing on here feels different.  I think it's because people have to seek it out.  Moving on, the weird thing that I referenced earlier is that anyone who is likely to read this is already very familiar with what's going on.  I guess I should approach this without an audience in mind, but I don't quite know how to do that.
 
So here's what's been going on during the writing break.  I have decided to become serious about programming, and have started down a path that will hopefully lead to a new career.  I've been flirting with this idea for years.  I got a Java programming book when I was in college, but never really had time for it.  Then I got into PHP after college, and started working on web apps that I would eventually deploy at work.  I can't remember how much of this I've talked about before, but I do IT support in Seattle, and there are many day-to-day tasks that I do manually that could be transitioned to a database with a web front-end.  So that was where I started.  I did all the work at home, and I thought I would write about my progress.  Now maybe this is just me being shy, or part of that "who cares" reaction I have to social networking, but I didn't really tell anyone about this.  My wife didn't even know I had started writing on here again.  After months and months of inactivity, I can't blame her for not checking.  Somewhere during this time, though, my sister found the blog and was probably horrified that her sweet, darling little brother had turned into a degenerate who rants about video games, scotch, and amateur web development.
 
Somewhere in the process, my sister tells me that her husband, Adam, would be willing to show me the ropes.  This is where talking about this stuff gets weird.  I knew Adam was a software developer from way back when we were all still living in NY and I was a smartass teenager needling both of them over any little thing, but it had never occurred to me to reach out about this.  I think part of it was because I wasn't 100% sure at that point if I really wanted to take programming past the hobby level.  The offer to teach me really made me start thinking more seriously about what I wanted to do.  I didn't want to waste Adam's time by jumping in and then deciding 3 months later that I didn't actually want to do this. So I decided that if I could make something reasonably complex (for my skill level at the time) and actually finish it, then I would take him up on it.  I know myself, and I know that I'm real good at planning and starting, but I tend to lose steam near the end.
 
The surgery tracker that I've written about in the past was borne from that test.  It's kind of crazy to look back at that, even as recently as I made it, and see how much I've changed.  There is some rough, ugly code in there.  That was written with PHP and first MySQL, then adapted for MS SQL Server.  One of the first things Adam did was introduce me to C#, and shortly after that ASP.NET.  My familiarity with object oriented languages was largely theoretical at that point, so it's been slow going, but I'm making progress.  Some of the other major changes that have come from this was Adam dragging me into modern day web programming practices.  Stuff like Github and Bootstrap, and using an actual IDE to write code in (I still love you Notepad++).  I learned HTML and CSS during the time (or from a book written during the time) somewhere between the utter chaos of early HTML, where tables and iframes reined supreme, but before the div tag became the standard structural element of all web pages.  I remember the book stressed the importance of separating the structure of the page from the appearance of the page, but it didn't really offer any suggestions on good ways to do that.  Just being exposed to some of these things has helped me tremendously.
 
Currently, I'm working on a tracker for hard drive based backups that I'll use at some point at work.  I'll write more about that at a later date.  I'm still getting used to ASP.NET, but I'm making progress.  Well, I haven't made much progress lately.  At work they decided to renovate the biggest clinic over the course of 2 months, and I'm playing Christmas elf at home.  We're having my wife's brothers up for Christmas, and it's the first time at least 2, if not all 3, have seen the house, so we want to make it special.  I'll take some pics and put them up here somewhere when it's done.  We keep adding or changing things, so it's a work in progress.  Isn't everything?