Reputation: 353
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).
Upvotes: 2
Views: 1931
Reputation: 109140
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.
Upvotes: 0
Reputation: 7595
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.
Upvotes: 1
Reputation: 7475
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.
Upvotes: 0
Reputation: 781
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
Upvotes: 1
Reputation: 1503200
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();
Upvotes: 8
Reputation: 26599
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
}
Upvotes: 0