HTML

Post With Parameters To An ASP.NET Page

August 6th, 2008  |  Published in ASP.NET, HTML

I keep seeing people hitting my blog while looking for search terms similar to “posting to .net” or “asp.net post with parameters”. I’m not sure exactly what is being searched for, but I have a hunch that it’s how to post a page to an ASP.NET page.

I did find this somewhat hard to understand in the beginning. I think that’s because I have been spoiled by what ASP.NET does for you under the covers.

Let’s start with a simple HTML page. It could really be a PHP or JSP or any other page that can do a simple “POST” with a form – which is pretty much every page you can view in a browser.

I am going to do this all in Visual Studio 2005. We will have 2 separate projects here. The first will be our ASP.NET page that we are posting to. The second will be a simple HTML page with a form. It doesn’t need to be in its own project, but I made one for it for ease of presentation.

Lets start with our simple input form on the HTML project which I have called “FormExample”. Here we have HTML for a simple form that wants to know your name and favorite color (its either blue or red because everyone knows those are the best colors in the world).

Our form is going to look like this…

It’s not pretty but you get the idea. Now lets take a look at the very simplistic code behind this basic HTML form…

We have declared a form object and put some very simple HTML controls inside. Notice that the “Method” on the form is set at “POST” and the action is the URL of the aspx page we are posting to. To post our form, we just add an “onclick” event to the button and set it to javascript MyForm.submit(). That URL is set to the URL of our ASPPostExample project. I have configured it to run on IIS. It runs in the ASP.NET Development Web Server by default, but it is easy to make it run on IIS…

Now we add some simple code to the Default.aspx page in our ASPPostExample project to read in the form that was posted. We do this by using the Request.Form command. We can either provide an index, or the name of the control we want the value of…

Now fire up your project with the HTML form a click the button…

I hope this has cleared up a bit how you can post data to an ASP.NET page. Hopefully that’s a similar answer to what people are looking for when they hit this blog on those search criteria. Let me know if this helps with a question, or if I completely missed the mark.