我正在编写一个显示一些事件的日历。每天都有一个用于上午、下午和晚上事件的按钮,当有事件显示该按钮已启用并更改其颜色时。我在 html 表格中显示这些按钮,当有人更改显示的月份时,程序必须通过禁用所有按钮并将其颜色再次设置为白色来“清理”按钮。事情是我能够通过使用包含按钮的表格上的 FindControl 方法来启用它们:
string butControl = /* id of the button */
Button block = mainTable.FindControl(butControl) as Button;
block.BackColor = Color.Gray;
block.Enabled = true;
它工作正常。在我的清理方法中,我不想调用所有按钮的名称,因为有 105 个,而是我使用了这种方法:
private void CleanUp()
{
foreach (Control c in mainTable.Controls)
{
Button bot = c as Button;
if (bot != null)
{
bot.BackColor = Color.White;
bot.Enabled = false;
}
}
}
但这不会改变任何按钮的颜色或启用属性。我的问题是:表格的 Controls 属性中的控件是否与可以通过 FindControl 方法找到的控件相同?或者我在检索控件时做错了什么?