Make the Form Dance
9 May 2008 @ 08:47AM

Updated: 25 Jan 2010 @ 08:47AM
Ok, we're not really going to make it dance. However, we'll go ahead and code it to do something useful. Go ahead and hit the little minus (-) next to the header, footer, and Page_Load methods. This will collapse them, cleaning up the display some. When you're dealing with hundreds (or thousands) of lines of code, collapsing the view can be VERY helpful. A quick way to do it is to hold down control, then hit m, then o. Then you can let go of control. This will collapse all your methods. Then just click the + on the form method to open it back up.


Making the Form Dance

I haven't done anything completely new here. I declare my string variables at the top, including the content variable we already had plus a name and comment variable. I then use the code described in a previous tutorial to check for passed form variables (the try-catch on lines 40-55). On line 57 I check to make sure that both name and comment are not null. If they aren't, I execute the first block where I thank the person for submitting their comment. If it is blank, I execute the else block, outputting the form.

You may notice how I'm setting the value of the input tags in the form. Why? Well, what happens if someone fills out their name but doesn't fill out a comment? With the value filled out like it is, their name is automatically repopulated into their form so they don't have to type it out again. Go ahead and try it... fill out only one input and hit submit. You'll notice the form doesn't go all blank on you. This is user friendly. We can take error handling a step further as well.
Comments (0)

Some More Error Handling

Above is a screenshot of the form part of the code. After outputting the form, I have some ternary operations. The way I have it configured, if both name and comment are blank, nothing gets written out. However, if the name is not null, the comment must be so I put out a request to fill out the comment. Conversely, if the comment isn't blank, then the name must be. If neither were null, we would've executed the if part of this if/else statement, so if one variable is not null, we know the other must be null. This isn't anything earth shattering, but it's very helpful to your end user who may be confused why his form didn't submit properly.
Comments (0)
A Method for Getting Form VariablesCalling the Method

So let's step through this. On line 37 I'm creating my string variable name and setting it equal to the result of getVariable("name"). The getVariable() part should look familiar: I'm calling a method. The part that reads "name" passes "name" into the method.

On line 68 I've defined the method private string getVariable(string input). It's a private method, it returns a string, it's called getVariable, and it takes a string called input. Remember the part above that reads getVariable("name")? The "name" part is automatically assigned to the string input. This would be as if I'd written string input = "name"; within the method.

From there I create a string called variable. I then look for the form variable. However, rather than have Request.Form.Get("name") or Request.Form.Get("comment") I have Request.Form.Get(input). If you remember, we set input via our method call above (getVariable("name")) so it equals "name". Thus, it's the equivalent of writing Request.Form.Get("name"). We get the value of the form variable "name" and then return it.

On line 38 we do the same thing with comment. Since we passed "comment" into the method, we're now looking for a form variable like Request.Form.Get("comment"). What we've done is we've taken some repetitive code and pulled it out into its own method, making it easy to use and quick to write.

You may want to play with this on your own some before continuing. I realize this can be confusing at first: It took me a really long time to finally figure out objects after I'd first been introduced to them. However, after doing it a few times it should start to come naturally. Next, let's start on the database.
Comments (0)