I am dynamicaly generating a HTML form that is being submitted to a .aspx webpage. How do I determine in the resulting page what variable names were submitted and what the value
Each of Request.Cookies
, Request.Form
, Request.QueryString
and Request.ServerVariables
is a NameValueCollection
or similar. You can ask each of those for its keys, and proceed from there.
foreach (string key in Request.QueryString.Keys)
{
string value = Request.QueryString[key];
// etc
}
Quite which collections you need will depend on the purpose of this - if it's just for diagnostics, I'd be tempted to dump them all out.
In the declaration of the .ASPX add Trace='True'
<%@ Page Language="VB" Trace="True" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
You should use Request.Form as a collection. Note that multi-valued parameters (a multi-select) are turned into a collection themselves. There's an example on this page.
Have you tried something like this:
For post request:
foreach(string key in Request.Form.Keys )
{
Response.Write ( Request.Form[key] );
}
For a get request:
foreach(string key in Request.QueryString.Keys )
{
Response.Write ( Request.QueryString[key] );
}
JohnFx's method works well. You can also include query string, server variable, and cookie data into the mix by using the Request.Params collection, as in:
foreach(string key in Request.Params.Keys)
{
Response.Write(String.Format("{0}: {1}<br />", key, Request.Params[key]));
}
You need to be careful when using the Request.Params
collection. If a QUERYSTRING variable and FORM variable both share key "Name" and have values "Val1" and "Val2", then the value of Request.Params["Name"]
will be "Val1, Val2."
In the declaration of the .ASPX add Trace='True'
<%@ Page Language="CS" Trace="True" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>
You will be able to see the Forms data, Session, Application data etc.