C# ASP.NET-访问页面上的所有ImageButton,并在ImageButton上放置两个图像

C# ASP.NET-访问页面上的所有ImageButton,并在ImageButton上放置两个图像,c#,asp.net,css,imagebutton,C#,Asp.net,Css,Imagebutton,我实际上有两个问题: (1) 是否可以在已经设置了ImageUrl的ImageButton的顶部放置另一个图像(不更改ImageUrl-字面上只需添加“顶部”的第二个图像)?甚至使用CSS (2) 我在ListView中动态设置了许多ImageButtons。当用户单击ImageButton时,我会更改所单击按钮的.CssClass属性,以便“突出显示”它。我的问题是:每当单击ImageButton时,我不仅需要高亮显示它,还需要确保取消高亮显示所有其他按钮。然而,我很难找到其他人。我使用 ((

我实际上有两个问题:

(1) 是否可以在已经设置了ImageUrl的ImageButton的顶部放置另一个图像(不更改ImageUrl-字面上只需添加“顶部”的第二个图像)?甚至使用CSS

(2) 我在ListView中动态设置了许多ImageButtons。当用户单击ImageButton时,我会更改所单击按钮的
.CssClass
属性,以便“突出显示”它。我的问题是:每当单击ImageButton时,我不仅需要高亮显示它,还需要确保取消高亮显示所有其他按钮。然而,我很难找到其他人。我使用

((ImageButton)sender).CssClass = "SelectedImageButton";
在事件处理程序中。但是,我如何让所有其他人都能将他们的风格“恢复”为非高亮风格

提前感谢您的帮助

更新:已回答 我已经使用以下算法解决了(2)中提到的问题。注意,我在下面将@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";
}
//将发送者强制转换为ImageButton,以便单击ImageButton
ImageButton点击ImageButton=发送者为ImageButton;
//ListView包含ListViewDataItems,ImageButtons位于
//因此,这些子控件与ImageButtons的父控件ID匹配
Control parentControl=单击图像按钮。父项;
列出allOtherImageButtons=MyListView.Controls.OfType i.ID!=单击edimagebutton.Parent.ID).ToList();
//突出显示
单击edimagebutton.CssClass=“HighlightedStyle”;
//不高
foreach(allOtherImageButtons中的ListViewDataItem按钮)
{
//ImageButton始终是ListViewDataItem的第二个子控件
ImageButton childImageButton=(ImageButton)按钮。控件[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";
    }
}
protectedvoid ImageButton5\u单击(对象发送方,ImageClickEventArgs e)
{
ImageButton clickImageButton=发送者作为ImageButton;
//本例假设所有图像按钮都具有相同的父按钮。
//根据需要调整页面布局
Control parentControl=单击图像按钮。父项;
列出allOtherImageButtons=parentControl.Controls.OfType().AsQueryable()。其中(i=>i.ID!=clickImageButton.ID).ToList();
//突出显示
单击ImageButton.CssClass=“WhateverHighlights”;
//不高
foreach(allOtherImageButtons中的ImageButton按钮)
{
button.CssClass=“WhateverClears”;
}
}

编辑:还有一件事。确保在加载页面之前(即在初始化期间)添加动态添加的任何控件。添加控件太晚时会出现一些viewstate问题。

对于第(1)部分,我想在所选图像按钮上显示一个“复选标记”,以表明在用户“提交”其选择之前,该按钮是所选的。现在我在想,我可能只需要使用一个图像控件而不是ImageButton控件。对于第(2)部分,我尝试使用上述代码,但是,AllocherImageButtons每次都返回一个空列表。所有ImageButtons都有相同的父容器,并且它正确地从“sender”抓取了单击的容器。你知道为什么吗?我只是想-为什么我在访问其他ImageButton对象时遇到这么多麻烦,原因是因为它们是从绑定列表对象动态创建的吗?我的意思是,我将ListView绑定到代码隐藏中的一个列表,然后使用为绑定列表中的每个项目添加一个ImageButton…可能这就是您上面建议的查询似乎不起作用的原因?嗯,这似乎是一个嵌套控件问题。如果图像按钮位于ListView中,我的第一个想法是您必须使用类似MyListView.ItemTemplate.Controls的内容来代替上面代码示例中的parentCotrol.Controls位。我通过调整代码(见上文)解决了这个问题!我怀疑没有你的帮助我会走那么远,所以非常感谢!