【问题标题】:Mouse over highlighting of dates in ASP.NET calender将鼠标悬停在 ASP.NET 日历中的日期突出显示上
【发布时间】:2012-09-04 16:18:06
【问题描述】:

我正在尝试将鼠标悬停在颜色更改功能上添加到 ASP.NET 中的默认日历。到目前为止,我已经尝试实现以下代码:

    Color col = new Color();
    col = Calendar1.DayStyle.BackColor;
    if (col != Color.Empty)
    {
        e.Cell.Attributes["onmouseover"] = "this.style.backgroundColor='pink';";
        e.Cell.Attributes["onmouseout"] = "this.style.backgroundColor='" + col + "';";
    }
    else
    {
        e.Cell.Attributes["onmouseover"] = "this.style.backgroundColor='pink';";
        e.Cell.Attributes["onmouseout"] = "this.style.backgroundColor='';";
    }

如果我不点击日期,一切似乎都正常。但是,当我单击日期并且日期背景变为灰色时,背景颜色变为粉红色,然后再次变为白色。这似乎是因为不知何故

    col = Calendar1.DayStyle.BackColor;

没有选择正确的背景颜色?

这里有什么我错过的吗?

【问题讨论】:

  • 不,这是因为onmouserover和onmouseout事件之后触发,它变成粉红色而不是白色。

标签: c# asp.net calendar mouseover


【解决方案1】:

为什么不以编程方式进行:

Color col = new Color();
col = (e.IsSelected) ? Calendar1.SelectedDayStyle.BackColor.ToString() : Calendar1.DayStyle.BackColor.ToString();

e.Cell.Attributes["onmouseover"] = "this.style.backgroundColor='pink';";
e.Cell.Attributes["onmouseout"] = "this.style.backgroundColor='" + col + "';";

您只需要确定当前日期是否被渲染;我不确定是否有 IsSelected 属性;如果没有,请将 e.IsSelected 替换为执行此操作的内容。

【讨论】:

  • 我意识到为什么我的代码不起作用:这是因为 Calendar1.DayStyle.BackColor 是未选择日期的固定背景颜色。不幸的是,这意味着新的问题是我无法找到一种方法来识别鼠标悬停的单元格。这意味着无法区分您希望 onmouseout 显示一种颜色的单元格和您希望它显示另一种颜色的另一个单元格。
  • 为什么不呢?您只想为未选定的单元格使用一种颜色,而为选定的单元格设置一种颜色?我不明白为什么上面的解决方案可以解决这个问题......
  • 我想要的是当您将鼠标悬停在日期上时背景颜色从白色变为粉红色,当鼠标移出日期时从粉红色变为白色。对于选定的日期也是如此,但它会(例如)从灰色到粉红色再到灰色。但是,我找不到仅通过将鼠标悬停在单元格本身上来区分选定单元格和未选定单元格的方法。
  • 对,但是您将自定义 javascript 附加到每个单元格;因此,从服务器中,您可以使用该选定属性确定选择了哪个单元格,以及未选择哪个单元格。每次选择一个新单元格时,都会导致回发,该回发重新呈现控件,触发 DayRender 事件,并适当地更新 javascript。使用我上面的解决方案就可以了。
  • 好的,如果我改变 col = (e.IsSelected) 就可以了? Calendar1.SelectedDayStyle.BackColor.ToString() : Calendar1.DayStyle.BackColor.ToString();感谢 col = e.Cell.BackColor
猜你喜欢
  • 2020-06-16
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多