Iterate Through The Form Collection In ASP.NET 2.0

Yesterday I had to use an iterator to loop through all the data values in a form request. This is well documented for classic ASP and can even be found in the old Active Server Pages SDK help file but it was difficult to find the same method to use for ASP.NET 2.0.

First I should explain why I needed to do that. I was working on another "value added service" for YouTube users which will allow them to export their video favorites to Excel. YouTube will frequently suspend or delete user accounts without warning or explanation so it is a good idea to back up your favorites or you'll be unable to find them again. Any way I had a form for entering the account username and used a PostBackUrl to transfer it to another page with its ContentType set to return an Excel file. As far as I know, you cannot change the ContentType on a postback. I was also using a master page so my form elements were getting lengthy names. I was finding it difficult to determine how to reference a form value via its name so I wanted to use the classic ASP method of looping through the form collection.

Here is some sample code on how to do this. It generates an Excel spreadsheet with the form collection key names and values:

   1: Imports System.Text
   2:  
   3: Partial Class CreateExcel
   4: Inherits System.Web.UI.Page
   5:  
   6:     Dim sbHTML As New System.Text.StringBuilder("")
   7:  
   8:     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   9:         Response.Clear()
  10:         Response.Buffer = True
  11:         Response.ContentType = "application/vnd.ms-excel"
  12:         Response.Charset = ""
  13:         Response.AddHeader("Content-Disposition", "attachment; filename=Form Collection.xls")
  14:         Page.EnableViewState = False
  15:  
  16:         sbHTML.Append("<table border=""0"" cellpadding=""3"" style='"border-collapse: collapse"" width=""100%"" bordercolor=""#000000"" id=""table1"">" & Environment.NewLine)
  17:         sbHTML.Append("<tr>" & Environment.NewLine)
  18:         sbHTML.Append("<td bgcolor=""black"" colspan=""2""><b><font face=""Arial"" color=""white"" size=""4"">Form Collection</font></b></td>" & Environment.NewLine)
  19:         sbHTML.Append("</tr>" & Environment.NewLine)
  20:         sbHTML.Append("<tr>" & Environment.NewLine)
  21:         sbHTML.Append("<th>Name</th>" & Environment.NewLine)
  22:         sbHTML.Append("<th>Value</th>" & Environment.NewLine)
  23:         sbHTML.Append("</tr>" & Environment.NewLine)
  24:  
  25:         Dim i As Integer
  26:         For i = 0 To Request.Form.Count - 1
  27:             sbHTML.Append("<tr>" & Environment.NewLine)
  28:             sbHTML.Append("<td height=19>" & Request.Form.AllKeys(i) & "</td>" & Environment.NewLine)
  29:             sbHTML.Append("<td height=19>" & Request.Form(i) & "</td>" & Environment.NewLine)
  30:             sbHTML.Append("</tr>" & Environment.NewLine)
  31:         Next
  32:  
  33:         sbHTML.Append("</table>" & Environment.NewLine)
  34:         'Debug.WriteLine(sbHTML.ToString())
  35:         Response.Write(sbHTML.ToString())
  36:         Response.End()
  37:     End Sub
  38: End Class

5 Comments

  • First off all.. in csharp the following is the easiest way to iterate:
    .
    . foreach (string key in Request.Form)
    . {
    . Console.Write(Request.Form[key]);
    . }

    Second, you are using a stringbuilder but you are still concatenating the strings first before passing them to the stringbuilder. Do every append with the stringbuilder.

    Third, you are using Environment.NewLine in a webpage. These are fixed newline escapes and does not variate on each platform thus you can use static new line escapes here like \n.

  • Thanks for the suggestions Ramon. I have made those improvements but the static new line escape wound up in the Excel spreadsheet so I kept the Environment.NewLine but without concatenating it to the strings.

  • I'm trying in vain to parse the Request's form to find a checkbox control that was generated programmatically on the page with JavaScript. I can find the Key, but I can't get at the Checked property of the Control. How does one do this?

  • Ah, I see. It's been so long since ASP 3.0, I'd forgotten that if the checkbox ain't checked, it ain't gonna be in the request.form object, and if it is, it is. Doh!

  • Iterate through the form collection in asp net 2 0.. Not so bad :)

Comments have been disabled for this content.