Looping Through Submitted Variables
Looping through submitted form variables is useful when you are receiving the PostBack from an external page or when you are receiving the results from a dynamically created form. Let’s say you have a form with the following fields:
Name:
Age:
Location:
You can loop through variables submitted via the method=”POST” method with the following code:
foreach(string key in Request.Form) {
Response.Write(key + " = " + Request.Form[key] + "");
}
Similarly, you can loop through values submitted using the method=”GET” method by changing Request.Form to Request.QueryString:
foreach(string key in Request.QueryString) {
Response.Write(key + " = " + Request.QueryString[key] + "");
}
Looping Through Variables of the Same Name
Let’s say you have form variables of the same name you’d like to loop through. For example:
Checkbox 1
Checkbox 2
Checkbox 3
Checkbox 4
To loop through variables of the same name, you can use the following:
for(int i=0; i < Request.Form.GetValues("ckBox").Length; i++) {
Response.Write(Request.Form.GetValues("ckBox")[i] + "");
}
Looping Through Form Variables
Let’s say you have dynamically created fields and you’d like to loop through them on the form (click here to download):
<%@ Page Language="C#" %>
Once submitted, this code will display the form variables below the submit button (boxed in red):



