2

Friends,

I Have a grid of 100 or more text boxes (HTML OR ASP.NET) each will be containing a text value of fixed length, need ALL of these passed back to the back end form for mass updating of the database..

I can do this by simple going through each of the controls .text property in code behind.

However that makes the code to big and ugly.

I was wondering if there is any why to go through each control using some controlled looping structure and retrieve data

i.e.

Private List<string> getdata()
{
  Private List<String> MyList = new List<string>();
    foreach (Textbox)control txbControl in ....// don't know what this will be
     {
       MyList.Add(txbControl.text);
     }
}

Please note that all of these textboxes have unique ID tag on the page i.e.

<tablr>
<tbody>
<tr>
<td>
<asp:TextBox ID="TxB_Customize1" runat="server"></asp:TextBox>
<td/>
<td>
<asp:TextBox ID="TxB_Customize2" runat="server"></asp:TextBox>
<td/>
<td>
<asp:TextBox ID="TxB_Customize3" runat="server"></asp:TextBox>

... ... ...

Sorry forgot to mention this, text boxes are grouped in columns and each textbox in a given column shares similar name i.e. "Txb_Customize" in the given instance.

So when retrieving the values I also need to know from where its coming from (may be textbox ID).

6 Answers 6

8

Look at the Control.Controls property.

You'd want something like:

foreach (Control control in Controls)
{
    TextBox textBox = control as TextBox;
    // Ignore non-textboxes
    if (textBox != null)
    {
        list.Add(textBox.Text);
    }
}

If you're using .NET 3.5 you could do this in a simpler way with LINQ:

return Controls.OfType<TextBox>()
               .Select(textBox => textBox.Text)
               .ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

or you can do something like:

int index = 1;
while ( ( TextBox tb
  = FindControl (
      string.Concat ( "TxB_Customize", index.ToString ( ) ) as TextBox != null )
{
  MyList.Add ( tb.Text );
  index++;
}

This could be good if you have actually some other textboxes as well, which is not part of this array of data.

Comments

1

You can do that in javascript - give them one class name and get them all into a collection and loop through the collection and then simply post the values using ajax. JQuery can be very handy here -

$(".yourclassname").each(function(index){ $(this).val() //this will be the value of your textbox })

you can coin one big string and parse on the server side or you can build a name value array and then use that instead

2 Comments

Hey Bharani i am not so femilier with JQuery however i like it because i any way have huge amount of java script on the page. can you please explain the "(index)" part in your answer. i.e. how do i get it. or its just a place holder. i have figured out rest. Thanks
each function is like a for loop and the index of the collection being iterated is passed as a parameter to the callback function. If inside the callback you need the actual DOM element you can use this or you can use $(this) if you need the jquery object.
0

You could iterate through the container's controls (with MyContainer.Controls), if all you have in there is text boxes it will be easy, otherwise you might need to do:

foreach (Control myBox in MyContainer.Controls)
{
    TextBox myBox = Control As TextBox
    if (myBox != null)
        // Do Stuff
}

Comments

0

When the logic of the code is related to textboxes'name, the only solution I see is to rewrite this code from scratch, then buy some good design books to the programmer who has written this crap.

Comments

0

If you don't want to depend on server controls you can use the Request.Form collection.

However you will need to use the control ids to tell whether it is a text box, using the question's ids:

Request.Form.AllKeys.Where(n => n.StartsWith("TxB_Customize"))
                    .Select(n => new[] { n, Request.Form[n] });

Which returns a collection of {Id,Value} pairs.

2 Comments

Wouldn't n be the UniqueID? In that case n.StartsWith wouldn't work.
UniqueID: of course. Would need something a little more sophisticated to match the keys.

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.