0

I am trying to create a poll system that will ask the users for the number of options they would like to add from a dropdownlist.

Then when the user choose a number i would like to add text-boxes for those numbers and finally use asp.net to iterate through the text-boxes and add their values in the database.

Example:

  1. User chooses to add 5 options.
  2. I use Jquery to to append 5 inputs to the form.
  3. User adds their values.
  4. I iterate through the text-boxes and execute a void based on these values.

i am able to do the first 3 steps but i am stuck on the 4th step. To solve it i tried to use a loop:

foreach (TextBox tb in form1.Controls) 
{
   Response.Write(tb.Text);
}

but that throws an error:

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

how can i iterate throw the text-boxes?

thanks

5
  • Umm... You wrote only 4 steps... Commented Jan 21, 2013 at 19:09
  • @Blachshma - My mistake, question updated. Commented Jan 21, 2013 at 19:11
  • Please add the code you have until now, and explain what exactly is your problem.. Commented Jan 21, 2013 at 19:12
  • You have a bit of a disconnect here. You imply that you want to create the controls client side with jQuery but want to iterate through them with asp.net which is a server side technology. Those two approaches won't play nicely with one another. Commented Jan 21, 2013 at 19:14
  • @andleer - this step is not essential, i can use asp.net instead. Commented Jan 21, 2013 at 19:18

2 Answers 2

2

Any static content is represented by a LiteralControl, which is why you experience that. A real easy way is to use LINQ:

var ctls = form1.Controls.OfType<TextBox>();
foreach (var ctl in ctls) { .. )

Or check the type as you loop through the controls to make sure it's a textbox first:

foreach (Control tb in form1.Controls) 
{
   if (tb is TextBox)
      Response.Write(((TextBox)tb).Text);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Brian, both solutions works fine, i was wondering though if one solution is better?
I like LINQ as it keeps it concise. The latter might be sliiiightly more efficient, as the first option loops through all controls, does the type check, returns a collection, and then you can foreach through it. The latter does all of that in one operation. But I don't really think the performance gain is that huge...
0

Brian's answer seems a solid one. Another option that comes to mind at first sight is:

  • Declare a function...
  • Declare a simple array or even a string in js
  • Iterate from client side your inputs
  • And for each iteration you should be saving the value in that array or concatenating the value.

The array then can be saved as a string in some asp hiddenfield in order to do your server-side stuff.

Best regards.

1 Comment

This would definitely work, but i am trying to combine the text-boxes with uploads, and i cannot retrieve the file path through JavaScript. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.