【问题标题】:ASP.NET - Accessing All ImageButtons on a Page and Putting Two Images on an ImageButtonASP.NET - 访问页面上的所有 ImageButtons 并将两个图像放在一个 ImageButton 上
【发布时间】: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


    【解决方案1】:

    对于该问题的第 (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 期间)。太晚添加控件会导致一些视图状态问题。

    【讨论】:

    • 对于第 (1) 部分,我想在选定的 ImageButton 上显示一个“复选标记”,以表明这是选定的,直到用户“提交”他们的选择。现在我正在考虑它,我可能只使用 Image 控件而不是 ImageButton 控件。对于第 (2) 部分,我尝试使用上面的代码,但是,allOtherImageButtons 每次都像往常一样返回一个空列表。所有的 ImageButtons 都具有相同的父容器,并且它确实正确地从“发件人”中抓取了点击的那个。任何想法为什么会这样?
    • 我只是想 - 为什么我在访问其他 ImageButton 对象时遇到这么多麻烦的原因是因为它们是从绑定的 List 对象动态创建的?我的意思是,我将 ListView 绑定到代码隐藏中的 List,然后使用 为绑定 List 中的每个项目添加 ImageButton ....可能这就是您在上面建议的查询似乎不存在的原因上班吗?
    • 嗯,这似乎是一个嵌套控制问题。如果图像按钮位于 ListView 中,我的第一个想法是您将不得不使用 MyListView.ItemTemplate.Controls 之类的东西来代替上面代码示例中的 parentCotrol.Controls 位。
    • 我通过调整您的代码(见上文)解决了这个问题!如果没有你的帮助,我怀疑我能做到这一点,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-06-27
    • 2012-07-19
    • 2011-02-19
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    相关资源
    最近更新 更多