tags:

views:

720

answers:

7

friens,

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 controled looping structure and retrive 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 this 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 retriving the values i also need to know from where its comming from (may be textbox ID)

A: 

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
}
Steven Robbins
A: 

gotcha ... if you are using plain html (no asp.net webpage), you won't be able to get these inputs with the asp.net engine..

for a typical asp.net webpage, this should work:

foreach (var control in this.Controls)
{
    var textbox = control as System.Web.UI.WebControls.TextBox;
    if (textbox == null)
    {
     continue;
    }
    // work with textbox
}

edit: yes, i forgot the AS instead of a cast :) it's friday...

Andreas Niedermair
That code is very likely to throw exceptions... specifically of the InvalidCast kind.
Cerebrus
Use the "as" keyword to make it foolproof. ;-)
Cerebrus
aaaah.. i forgot this ... thank you
Andreas Niedermair
+8  A: 

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();
Jon Skeet
damn ... the linq-solution came up to my mind, after i've posted...
Andreas Niedermair
This LINQ solution... wow! :)
Pawel Krakowiak
+1  A: 

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

Bharani
Hey Bharanii 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
h_power11
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.
Bharani
A: 

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.

Nicolas Dorier
+1  A: 

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.

baretta
A: 

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.

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