WebControls' text properties are all over the place

T

TH

Could anyone please explain the bizarre and utterly infuriating effect
I am observing while adding controls programmatically? I've reduced the
problem to the following code:

<form id=Form1 method=post runat="server">
<asp:label id=Label1 runat="server"/><BR>
<asp:Label id=Label2 runat="server"/><BR>
<asp:Button id=Button1 runat="server" Text="Button"/><BR>
<asp:Button id=Button2 runat="server" Text="Button"/><BR>
<asp:Button id=Button3 runat="server" Text="Button"/><BR>
<asp:Button id=Button4 runat="server" Text="Button"/><BR>
<asp:Button id=Button5 runat="server" Text="Button"/><BR>
</form>

private ArrayList MyLabels = new ArrayList();

private void Page_Load(object sender, System.EventArgs e)
{
WalkControls(this);
foreach(Label label in MyLabels)
{
label.Text = label.ID + " " + DateTime.Now.ToString();
label.Parent.Controls.AddAt(label.Parent.Controls.IndexOf(label),
new TextBox());
}
}

private void WalkControls(Control control)
{
if (control is Label)
{
Label label = control as Label;
MyLabels.Add(label);
}
foreach(Control c in control.Controls)
{
WalkControls(c);
}
}

I.e. I'm recursively looking for Labels and adding them to an ArrayList
where they are later given a sibling TextBox. I'm also setting the
label text here to illustrate the problem. The buttons shouldn't be
doing anything.

On each postback, the first button's text property somehow acquires the
text from the second label's previous text value, and each subsequent
button picks up the previous text value of the button above.

Can anyone please help? It's driving me mad.
Thanks, Tony.
 
T

TH

TH said:
Could anyone please explain the bizarre and utterly infuriating effect
I am observing while adding controls programmatically?

I'll take that as a no then.

Does anyone actualy use usenet any more?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top