Request.Form - get values for fields having the same name

Problem
You know it’s possible to have controls with the same name on one page. In this case Request.Form[”txtSomeText”] returns the values form all 5 textboxes comma separted. (For example: text1,text2, text3.text4, text5).

Example:

.....

<input type="text" name="txtSomeText" />
<input type="text" name="txtSomeText" />
<input type="text" name="txtSomeText" />
<input type="text" name="txtSomeText" />
<input type="text" name="txtSomeText" />

.......

The question iin this case is “How to get the values for each textbox?”.
You can say “I’ll split the values…” but it’s not a good solution. What if one of the values is “text,text,text”?

I spent a lot of time splitting strings and searching for workaround of “comma values” (a,b,c) until I found the solution. Here it is..

Solution:
It’s simple: Request.Form returns NameValueCollection. GetValues is a NameValueCollection class method which gives us the requested result.

Request.Form.GetValues(”txtSomeText”) returns a string array with all the values.

, , , ,
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Technorati
  • digg
  • Reddit
  • SphereIt
  • DotNetKicks
  • YahooMyWeb

2 Responses to “Request.Form - get values for fields having the same name”


  1. 1 Kunal Sidhpura Jan 19th, 2009 at 8:58 pm

    Hi Vesy,
    I am a newbie to .NET was looking for something like this. Your blog is great also got to learn some new things. May be you could help me more with all the tips and tricks. If you could give me your email id it would be great would like to learn new things in .NET, my Email Id: kunu_sid@yahoo.com.

    Thanks and Regards
    Kunal Sidhpura

  2. 2 Kalyan Jun 18th, 2009 at 8:16 am

    hi
    clear and concise solution.
    Thanks for posting. really helpful!

    cheers,
    kalyan

Leave a Reply




Google

Blogroll


Blogroll Me!

Enter your email address:

Delivered by FeedBurner







Academics Blogs - Blog Top Sites