DataList, Repeater,...?

  • Thread starter Thread starter =?ISO-8859-1?Q?Andr=E9_Nobre?=
  • Start date Start date
?

=?ISO-8859-1?Q?Andr=E9_Nobre?=

Hi all...

I need to put 20 textbox in an aspx, but I don't want to put it
manually. How can I code a Sub to do that? Something like a For. I tried
using a For and puting the asp:textbox code inside a asp:label. It
didn't worked. Can I use DataList ou Repeater with a fixed number os
repetitions, not based on DataSet or DataTable?

Thankz,
André
 
Hi all...

I need to put 20 textbox in an aspx, but I don't want to put it
manually. How can I code a Sub to do that? Something like a For. I tried
using a For and puting the asp:textbox code inside a asp:label. It
didn't worked. Can I use DataList ou Repeater with a fixed number os
repetitions, not based on DataSet or DataTable?

Hi Andre,

If you want to stick with your 'for loop' strategy, you could put a
<asp:PlaceHolder> control on your page and in your code-behind, add
each new TextBox to the Controls collection of the placeholder.

TextBox tb = null;
for (int i=0; i < 20; i++)
{
tb = new TextBox();
// customize each textbox as you see fit
PlaceHolder1.Controls.Add(tb);
}

Hope that gets you start.

Roger
 
Using placeholder is a solution. But generally not a wanted one. Because you
would like to display something nearby textboxes.

My advice u to use a repeater. By this way you can easily separate your
design and code.

You can use any collection to bind it. no need to use a datatable or a
dataset. For instance, just use an arraylist. And fill arraylist with
indexes.

ArrayList al = new ArrayList();
for(int i=0;i<20;i++) al.Add();
repeater1.DataSource=al;
repeater1.DataBind();
 
Placeholder is the best solution. Placement of the TextBox can be controlled
by using another container object (HtmlTable). Just add the textboxes in the
table and add the table to the placeholder (beauty of Controls.Add !!).
Going further- onClick event handler can be added to the TextBox control as
soon as they are created(with uniqueid).

Repeater approach may run into name conflict (ID=??). You can change the ID
attribute during the binding event. It can be done but it would be too
complex.

Prodip
www.aspnet4you.com

Read my recent article on ASP.NET-
Friday! Trace Asp.Net Application User Information
 
For now I used the ArrayList solution, it was quite fast for me
(learning). I going to study PlaceHolder to know if it was the best
choice for that case.

Thanks for the answers,
André
 
Back
Top