Form Action Page

| | |

This page will walk you through how to build one action page to handle various forms.

WORK IN PROCESS!

Form HTML fields should start with “form_” for this to work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections.Specialized;
protected void Submit_Click(object sender, EventArgs e)
	{
		if (Page.IsPostBack)
		{
			form.Visible = false;
			NameValueCollection submittedValuesCollection = Request.Form;

			Response.Write("<b>Submitted Values:<br /></b>");

			Response.Write(Request.Form["start"] + "<br/>");
			Response.Write(Request.Form["end"] + "<br/>");

			string sInputControl = "";
			string sInputValue = "";
			string[] sDataElement;
			string sSQL = "";

			foreach (string key in submittedValuesCollection.AllKeys)
			{
				sInputControl = string.Format("{0}", key, submittedValuesCollection[key]);
				sInputValue = string.Format("{1}", key, submittedValuesCollection[key]);

				try
				{
					if (sInputValue != "")
					{

						if (sInputControl.Contains("form_"))
						{
							sDataElement = sInputControl.Split('_');

							Response.Write("<p>Field: " + sDataElement[1]);
							Response.Write("<br>Value: " + sInputValue);


							/*	sSQL = "INSERT INTO table (category, measure, submitted, start, [end], [count], submittedby) Values ('"
								+ sDataElement[1]
								+ "','"
								+ sDataElement[2]
								+ "','"
								+ DateTime.Now.ToString()
								+ "','"
								+ Request.Form["start"]
								+ "','"
								+ Request.Form["end"]
								+ "','"
								+ sInputValue
								+ "','"
								+ sEmployee
								+ "');";
							 */

							//Response.Write("<br/>SQL: " + sSQL);
							//	sRunSQLStmnt(sSQL);

							Response.Write("</p>");
						}

					}
				}
				catch (Exception ex)
				{
					Response.Write("<p>" + ex.ToString() + "</p>");
				}


			}
			Response.Write("<br /><br />");
		}
	}
Originally Posted on December 17, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.