Skip to content Skip to sidebar Skip to footer

C# - How To List Out Variable Names And Values Posted To Aspx Page

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 values w

Solution 1:

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] );
}

Solution 2:

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)
{
    stringvalue = 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.

Solution 3:

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."

Solution 4:

Don't read from the Request collection, that is a composite collection that contains form and querystring values, but also cookies and server variables. Read from the specific collections where you want the data from, i.e. the Request.Form (for POST) and Request.QueryString (for GET).

This is how you can get all the keys and values into a string:

StringBuilder builder = new StringBuilder();
builder.AppendLine("Form values:");
foreach (string key in Request.Form.Keys) {
   builder.Append("  ").Append(key).Append(" = ").AppendLine(Request.Form[key]);
}
builder.AppendLine("QueryString values:");
foreach (string keu in Request.QueryString.Keys) {
   builder.Append("  ").Append(key).Append(" = ").AppendLine(Request.QueryString[key]);
}
string values = builder.ToString();

Solution 5:

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.

Post a Comment for "C# - How To List Out Variable Names And Values Posted To Aspx Page"