0

I want to generate a column full of input text boxes inside a table.

At present, i generate my table dynamically from C# through StringBuilder.

sb.Append("<tr><td>" + item[0].ToString() + "</td><td>" + item[1].ToString() + 
    "</td><td>" + item[2].ToString() + "</td><td>" + item.GetString(3) + "</td><td>" + 
    item.GetDecimal(4) + "</td><td>"+ "<input type=\"text\" class=\"input-mini\" 
    columns=\"1\" name=\"ids\" ID= 'textbox" + i + "' /></td></tr>");

I understand that i can get the input values via jquery and pass it to C# method, but is there a more simpler way to achieve the same?

EDIT: To show Form Handling

for (int element = 0; element <= totalelements; j++)
{
    toList.Add(Request.Form["'textbox" + element + "'"]);
}

.aspx page (This button's visibility is toggled by a div)

<asp:Button runat="server" CssClass="btn" Height="27px" Width="190px" OnClick="doSomething" ID="do" Text="Send" />
3
  • ajax using Jquery is the simplest way... Commented Sep 20, 2013 at 10:05
  • @musefan I mean, posting the values back and retrieving at the server. Sorry for not being clear. Commented Sep 20, 2013 at 10:05
  • @Naraen: How are you posting? Form post, or AJAX post? You should include either you server side POST handling code, or your javascript AJAX code Commented Sep 20, 2013 at 10:07

1 Answer 1

2

If you want to handle dynamic textboxes on the server side after a form post then firstly you need to change your HTML to use unique name values, for example:

name='textbox0'

ID attributes will not be used when a form is submitted.


Then, server side you can access the elements using Request.Form["elementName"]. If you know the number of dynamic elements you have, then do it like this:

for(int i = 0; i < numberOfElements; i++)
{
    string textValue = Request.Form["textbox" + i].ToString();
    //do something with text value
}

If you don't know the number of elements, you can do it like this:

int i = 0;
while(true)
{
    var textBox = Request.Form["textbox" + i];
    if(textBox == null)//ran out of textbox elements to process
        break;

    string textValue = textBox.ToString();
    //do something with text value

    i++;
}
Sign up to request clarification or add additional context in comments.

Comments

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.