I want to have a list of 10 textboxes, let's say for first names, and then
10 for last names. The names will need to be created programmatically, like
first1, first2, first3, etc. How can I write a loop to create the textboxes
and then how can I programmatically refer to them in a loop after they have
been typed in to save the data? Thanks.lanem <lanem@.discussions.microsoft.com> typed:
> I want to have a list of 10 textboxes, let's say for first names, and
> then 10 for last names. The names will need to be created
> programmatically, like first1, first2, first3, etc. How can I write
> a loop to create the textboxes and then how can I programmatically
> refer to them in a loop after they have been typed in to save the
> data? Thanks.
Try this !
To create the textbox programmatically:
for(int i = 1; i < 11; i++)
{
TextBox tb = new TextBox();
tb.ID = "First" + i.ToString();
tb.Text = "First value for " + tb.ID;
this.plhTextBoxes.Controls.Add(tb);
Literal lit = new Literal();
lit.Text = "<br>";
this.plhTextBoxes.Controls.Add(lit);
}
where phlTextBoxes is a Placeholder WebControl.
To read the data:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach(Control ctr in this.plhTextBoxes.Controls)
{
if(ctr.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
sb.Append(((TextBox)ctr).Text);
sb.Append("<br>");
Response.Write(sb.ToString());
}
}
Davide Vernole
MVP ASP/ASP.NET
Microsoft Certified Solution Developer
Lanem,
You can do this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 10
Dim txt As New TextBox
Dim lbl As New Label
lbl.Text = "First Name: "
x.Controls.Add(lbl)
txt.ID = "firstname" & i.ToString()
x.Controls.Add(txt)
Dim lit As New Literal
lit.Text = "<br />"
plc.Controls.Add(lit)
Next
End Sub
This will dynamically load 10 textboxes with a unique id into a placeholder
called plc.
In your btn click event
you can read these values by doing:
for i as integer = 0 to 10
dim txt as TextBox = ctype(x.FindControl("firstname" + &.ToString()),
TextBox)
dim value as string = txt.Value
'do thing
next
Do consider if a repeater wouldn't do the job
Karl
"lanem" <lanem@.discussions.microsoft.com> wrote in message
news:90561BD4-6934-4799-9702-B61D4BD6FDB5@.microsoft.com...
> I want to have a list of 10 textboxes, let's say for first names, and then
> 10 for last names. The names will need to be created programmatically,
like
> first1, first2, first3, etc. How can I write a loop to create the
textboxes
> and then how can I programmatically refer to them in a loop after they
have
> been typed in to save the data? Thanks.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment