WebControls' text properties are all over the place

Discussion in 'ASP .Net' started by TH, Jun 16, 2006.

  1. TH

    TH Guest

    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.
     
    TH, Jun 16, 2006
    #1
    1. Advertising

  2. TH

    TH Guest

    TH wrote:
    > 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?
     
    TH, Jun 19, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Wessle

    pointer to type all over the place

    Gary Wessle, Nov 30, 2006, in forum: C++
    Replies:
    1
    Views:
    282
    Noah Roberts
    Nov 30, 2006
  2. Sonal
    Replies:
    3
    Views:
    692
  3. majid

    Reading properties in custom webcontrols

    majid, Jul 4, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    107
    majid
    Jul 4, 2003
  4. Vili
    Replies:
    1
    Views:
    128
    Alessandro Zifiglio
    Jun 30, 2006
  5. Sam

    perl lib all over the place

    Sam, Sep 18, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    98
Loading...

Share This Page