我<TABLE>
在 ASP.net 中动态生成一个。根据某些事件,我需要突出显示(通过背景颜色)有<TD>
问题的特定单元格并取消突出显示任何先前选择的单元格。
可悲的是,在每个事件之后,之前选择的单元格仍然突出显示。我编写了一小段代码,用 2 个模拟两个事件实例的按钮来复制我的问题。
创建表...
protected void Page_Init(object sender, EventArgs e)
{
Table tbl = new Table();
this.Controls.Add(tbl);
TableRow row = new TableRow();
tbl.Controls.Add(row);
TableCell cell1 = new TableCell();
cell1.Style.Add("background-color", "green");
cell1.ID = "cell1";
cell1.Text = "CELL 1";
TableCell cell2 = new TableCell();
cell2.Style.Add("background-color", "green");
cell2.ID = "cell2";
cell2.Text = "CELL 2";
row.Controls.Add(cell1);
row.Controls.Add(cell2);
}
我在此事件中突出显示第一个单元格
protected void Button1_Click(object sender, EventArgs e)
{
TableCell ctr1 = (TableCell)this.FindControl("cell1");
ctr1.Style.Add("background-color", "yellow");
}
我在此事件中突出显示第二个单元格。第一个单元格不应再突出显示,因为我刚刚在回发时重新创建了此表!
protected void Button2_Click(object sender, EventArgs e)
{
TableCell ctr2 = (TableCell)this.FindControl("cell2");
ctr2.Style.Add("background-color", "yellow");
}
任何帮助或指示将不胜感激!实现预期效果的替代方法也将受到欢迎!