Getting Posted Values Of a page on Page Load

While Googling around i found a way to get all the posted in a asp.net page on Page load event it’s
available on msdn here

What i did is to change that approach a little bit to suite my needs.!

void Page_Load(object sender, EventArgs e)
{
System.Text.StringBuilder displayValues =
new System.Text.StringBuilder();
System.Collections.Specialized.NameValueCollection
postedValues = Request.Form;
String nextKey;
for(int i = 0; i < postedValues.AllKeys.Length - 1; i++)
{
nextKey = postedValues.AllKeys[i];
if(nextKey.Substring(0, 2) != "__")
{
displayValues.Append("<br>");
displayValues.Append(nextKey);
displayValues.Append(" = ");
displayValues.Append(postedValues[i]);
}
}
Label1.Text = displayValues.ToString();
}

To

void Page_Load(object sender, EventArgs e)
{
List<DataTuples> Data = new List<DataTuples>();
System.Text.StringBuilder displayValues = new System.Text.StringBuilder();
System.Collections.Specialized.NameValueCollection postedValues = Request.Form;
String nextKey;
for (int i = 0; i < postedValues.AllKeys.Length - 1; i++)
{
nextKey = postedValues.AllKeys[i];
if (nextKey.Substring(0, 2) != "__")
{
DataTuples temp = new DataTuples();
temp.Id = nextKey;
temp.value = postedValues[i];
Data.Add(temp);
}
}
}

Where Data Tuples Class would look like

public class DataTuples

{
public string Id;

public string value;

}

Dont forget to add Collections and generic on the top!

using System.Collections;
using System.Collections.Generic;

Now on Page_Load You will get a populated List of id along with the corresponding Values and it would be Handy to use Data[i].Id isnt it ;)

Advertisements

~ by Azeem on April 28, 2008.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: