0

I am trying to get the value that is entered into a text box (string of characters) and display them. The text fields have a the same name which is "important". When I run this I get something like "testhtestetestltestltesto" which is suppose to be "test hello". This is just the first step into what I am trying to do. If anyone would like to give me a suggestion for the bigger issue see the paragraph after the code.

int i;
var items = Request["important"];
for (i=0; i < items.Lenght; i++){
    Response.Write("test" + items[i]);
}

I have a list box that contains a number of "courses" which is populated by a sql statement. When the use selects an item in the list box it triggers a statement which writes input boxes for the corresponding course for users to enter in data about the course. I want the user to be able to select multiple courses and input information for these multiple course which will then be submitted and entered into a sql statement hopefully using a foreach statement. The problem is that I don't know how to grab each of the values from the boxes with the same name.

2 Answers2

0

If you want "test hello," do this:

int i;
var items = Request["important"];
string myString = "test ";
for (i=0; i < items.Length; i++)
{
    myString += items[i];

}
Response.Write(myString);
user1646737
  • 169
  • 7
  • Request[] that is Request.Item[] returns only 1 item – LINQ2Vodka Nov 09 '13 at 23:21
  • Likely, you are right, but not nece-celery. An item could be a list, too. I doubt his is, so thanks for down-voting. – user1646737 Nov 09 '13 at 23:22
  • Really, he probably has a collage of issues. He needs to be more specific as to the nature of the array he has created and what its elements contain. – user1646737 Nov 09 '13 at 23:24
  • That does work. When I tried it with "hello" in the input field(important) and "test" as my string I got "test hello" although when I have two different input boxes with the same name and different values I get "test hello, hi" as my response write. – user2974999 Nov 09 '13 at 23:26
  • Do jim a favor and mark this as "Answer." ;=) – user1646737 Nov 09 '13 at 23:27
0

I recommend that you change your approach. When form fields with the same name are submitted, the result is a comma delimited list of all the values. Since you are using text boxes, the user could put commas into those values which will really mess up your code.

My approach would be to append a record identifier to the name of the text box. Then when you are processing the form submission, you can use the name of the form field to identify the record associated with the value that was submitted.

I know how to do this in ColdFusion so I assume that it could also be done in .net.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43