简体   繁体   中英

Foreach on controls not working

I have this code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        foreach (Control c in this.Controls)
        {
            if (c is TextBox && c != null)
                ((TextBox)c).AutoPostBack = true;
        }
}

And alot of:

 protected void TextBox1_TextChanged(object sender, EventArgs e)

which dont trigger, I think that the problem is that the foreach doesnt set the autopostbacks of the textboxes to true, but I dont know why!

As i remember you can not modify objects into foreach loop, try with for

    for (int x = 0; x < this.Controls.GetLength(); x++)
    {
        if (this.Controls[x] is TextBox && this.Controls[x] != null)
            ((TextBox)this.Controls[x]).AutoPostBack = true;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM