Processing a Web Form
4 Feb 2008 @ 02:08PM

Updated: 22 Jan 2010 @ 02:11PM
Now that we have a web form submitting data, let's go ahead and get that data to do something with it. I'll begin with the basic code we're going to use to retrieve the submitted information.


Get the POSTed Information


So basically I've added 2 new things (once for each form field). The first is along the lines of string name = Request.Form.Get("name");. The string name part we've covered before. We're creating a new variable called name and assigning it to equal what's on the right side of the equal sign. That is Request.Form.Get("name");. This is a method that's built into C#. Just like we're editing the Page_Load method for the default page, other methods are automatically made available to us via C#. We're taking a Request of the type of Form (since we submitted an HTML form) and we're getting a value. In this case we're getting the value that goes by the name of 'name'. Similarly we're getting the values for birthdate and occupation.

When you type Request. into the Visual Web Developer IDE, you'll notice it automatically comes up with a drop down of available commands. This is called IntelliSense and is one of the coolest things about Visual Studio. By looking through all the options and reading the description you can learn alot about the capabilities of C# and what you can do with it. Feel free to play around with it. In the meantime, I'll get back to the tutorial.

The other change I made was to alter our contentString string. It's no longer a solid string. On line 15, for instance, I close the double quotes, add a plus sign, put in name, then add another plus sign and reopen the string with @". Name is a variable we defined earlier. It's set to equal the value that was submitted with our form. I've altered the html code of the form to take this value and autopopulate the input blanks with it. If there is no value returned then the value is just empty. Go ahead and try it. Though this isn't all that impressive on its own, we now have the form contents in variables. Once we have that, we can do something more interesting.
Comments (0)