【发布时间】:2012-01-28 09:25:15
【问题描述】:
其实我有两个问题:
(1) 是否可以将 another 图像放在已经设置了 ImageUrl 的 ImageButton 之上(不更改 ImageUrl - 实际上只是将第二个图像添加到“顶部”)?即使使用 CSS?
(2) 我在 ListView 中包含动态设置数量的 ImageButton。当用户单击 ImageButton 时,我会更改单击对象的 .CssClass 属性以“突出显示”它。我的问题是:每当单击 ImageButton 时,我不仅需要突出显示它,还要确保 unhighlight 所有其他按钮。但是,我很难得到其他人。我得到点击的 ImageButton 使用
((ImageButton)sender).CssClass = "SelectedImageButton";
在事件处理程序中。但是,我如何获得所有其他人以便我可以将他们的样式“恢复”为未突出显示的样式?
提前感谢您的帮助!
更新:已答复! 我已经使用以下算法解决了(2)中提到的问题。请注意,我在下面将@OFConsulting 的答案标记为正确答案,因为如果没有他的算法,我永远不会得到以下算法(这来自于稍微调整他的算法)。谢谢@OFConsulting!
// Cast the sender to an ImageButton to have the clicked ImageButton
ImageButton clickedImageButton = sender as ImageButton;
// The ListView has ListViewDataItems and the ImageButtons are in
// THOSE children controls, thus match on the ImageButtons' Parents' IDs
Control parentControl = clickedImageButton.Parent;
List<ListViewDataItem> allOtherImageButtons = MyListView.Controls.OfType<ListViewDataItem().AsQueryable().Where(i => i.ID != clickedImageButton.Parent.ID).ToList();
// Highlight
clickedImageButton.CssClass = "HighlightedStyle";
// Unhighlight
foreach (ListViewDataItem button in allOtherImageButtons)
{
// The ImageButton is always the 2nd child control of the ListViewDataItem
ImageButton childImageButton = (ImageButton)button.Controls[1];
childImageButton.CssClass = "NoHighlightedStyle";
}
【问题讨论】:
标签: c# asp.net css imagebutton