Hi all,

I think I've got myself into an unresolvable situation.

I have a form (it is used so that non-techies can create their own questionnaires) that asks for a user to enter a number that reflects the number of options they want that question to have. A button is then pressed which calls a JavaScript function that unhides a <div> and inserts HTML that creates the required amount of textboxes.

JS code:

function displayOptionTextBoxes() {
            var numOptions = document.getElementById('OptionsBox').value;
            var code = "";
            for (i = 0; i < numOptions; i++) {
                code += "Option " + (i+1) + ": ";
                code += "<input type=\"text\" name=\"optionBox" + i + "\" id=\"optionBox" + i + "\"/> <br/>"
            }
            document.getElementById('textBoxDiv').innerHTML = code;
        }

The next step was when the user clicks the 'Next Question' button to enter a new question, the information entered is put into a class which is then entered into the database, and the resultant questionnaire_id is received back to allow for the insertion of more questions relating to that questionnaire. I wanted to add each of the values of the textboxes to an ArrayList using code similar to this (the OptionsBox textbox holds the number of options):

int numOptions = Convert.ToInt32(this.OptionsBox.Text);
            for (int i = 0; i < numOptions; i++)
            {
                String optionBoxName = "optionBox" + i;
                String anOption = this.optionBoxName.Text;
            }

Probably quite obviously, this errors, saying that there isn't a field called optionBoxName.

Anyone have any ideas on an alternative approach, to either generating and displaying the options textboxes, or getting the data from them?

Many thanks in advance.

For anyone that finds this question through Google and wants to know the answer I found, simply have the JavaScript apply 'name' attributes to the fields created, not IDs, and then in your C# code use the Request.Form[fieldName] method, as per code below:

int numOptions = Convert.ToInt32(this.OptionsBox.Text);
                ArrayList theOptions = new ArrayList();
                for (int i = 0; i < numOptions; i++)
                {
                    String textBoxName = "optionBox" + i;
                    String optionText = Request.Form[textBoxName];
                    theOptions.Add(optionText);
                }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.