- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
其实我有两个问题:
(1) 是否可以将另一个图像放在已经设置了 ImageUrl 的 ImageButton 之上(无需更改 ImageUrl - 字面上只是将第二个图像添加到“顶部”)?甚至通过使用 CSS?
(2) 我在 ListView 中包含动态设置数量的 ImageButton。当用户单击 ImageButton 时,我会更改所单击按钮的 .CssClass
属性以“突出显示”它。我的问题是:每当单击 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";
}
最佳答案
对于该问题的第 (1) 部分,在您的 css 类中设置背景图片可能会有所帮助,但您从未真正解释过为什么您不能更改 ImageUrl。如果您需要它是动态的,那么您可以随时将所有内容放在更新面板上,而无需编写一堆脚本。
第 (2) 部分看起来很简单。只需对页面中的相关控件集合使用一点 linq。
protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
{
ImageButton clickImageButton = sender as ImageButton;
// This example assumes all the image buttons have the same parent.
// Tweak as needed depending on the layout of your page
Control parentControl = clickImageButton.Parent;
List<ImageButton> allOtherImageButtons = parentControl.Controls.OfType<ImageButton>().AsQueryable().Where(i => i.ID != clickImageButton.ID).ToList();
// Highlight
clickImageButton.CssClass = "WhateverHighlights";
// Unhighlight
foreach (ImageButton button in allOtherImageButtons)
{
button.CssClass = "WhateverClears";
}
}
编辑:还有一件事。确保您要动态添加的任何控件在 Page_Load 之前添加(即 Init 期间)。太晚添加控件会导致一些 View 状态问题。
关于c# - ASP.NET - 访问页面上的所有 ImageButton 并将两个图像放在 ImageButton 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9028992/
我是一名优秀的程序员,十分优秀!