views:

396

answers:

3
+2  Q: 

asp:textbox array

Hey! how does one do something like this with asp.net <asp:textbox> and c #? (Thus creating an array of text boxes)

HTML:

<input name="Search[]" value="google" />
<input name="Search[]" value="yahoo" />
<input name="Search[]" value="alltheweb" />

PHP:

$array = $_GET['Search']; // or $_POST['Search'];

/*
$array is now an array like:
array(
0 => "google",
1 => "yahoo".
2 => "alltheweb"
)
/*
+3  A: 

Since the controls will all be strongly named it would be easy to just grab them each by name, but if you need them in an array you could wrap the controls in a panel..

You could do something similar to this:

<div runat="server" id="myPanel">
    <asp:TextBox runat="server" id="search1" />
    <asp:TextBox runat="server" id="search2" />
    <asp:TextBox runat="server" id="search3" />
</div>

Linq:

IEnumerable<string> values = myPanel.Controls.OfType<TextBox>()
                                             .Select(textBox => textBox.Text);

Non Linq:

string[] values = new string[myPanel.Controls.Count];
for(int i = 0; i < myPanel.Controls.Count; ++i)
{
    values[i] = (myPanel.Controls[i] as TextBox).Text;
}

Edited:

If you are going to dynamically add (or just have non-asp) inputs then it can actually be significantly easier to turn the inputs into an array server side.

For instance if you wish to have a bunch of <input type='text' name='search[]' /> on the page, on the server you can do the following to turn the items into a string array:

string[] inputValues = Request.Form["search[]"].Split(',');

So for the original example provided the equivalent would be:

HTML:

<input name="Search[]" value="google" />
<input name="Search[]" value="yahoo" />
<input name="Search[]" value="alltheweb" />

C#:

string[] myArray = Request.Form["search[]"].Split(',');

/*
myArray  is now an array like:
0: "google"
1: "yahoo"
2: "alltheweb"
/*
Quintin Robinson
thank you! Can you explain briefly what is Linq?what if you want to add more covered boxes with javascript after, how it is done. Viewed I place more boxes inside "myPanel" side, will then be added to the array? or do I need to update it with ajax?
I will amend my answer to show how you could loop over client-side added inputs with the request object. Also LINQ stands for Language Integrated Query and I don't have room to explain it here, but you can get an overview at: http://msdn.microsoft.com/en-us/library/bb308959.aspx
Quintin Robinson
A: 

You could also create the array like this:

TextBox[] textBoxes = new[] { search1, search2, search3 };

<div runat="server" id="myPanel">
    <asp:TextBox runat="server" id="search1" />
    <asp:TextBox runat="server" id="search2" />
    <asp:TextBox runat="server" id="search3" />
</div>

It gives the same end result as Quintin's answer, but I find this easier to read.

jrummell
A: 

What if the textbox value contain ',' character ? In that case, you can not use .split(',') to get the values ! Is thers any other solution ?

Doan Ngoc Ha