Gridview ImageButton更改mouseover和mouseout上的图像

Gridview ImageButton change image on mouseover and mouseout

本文关键字:图像 mouseout ImageButton 更改 mouseover Gridview      更新时间:2023-09-26

我有一个Gridview,其中有一个Imagebutton。它显示基于hfComplete(一个隐藏字段)值的图像。

如果值为true,则显示"images/completeiconfixed.png",并将属性附加到onmouseover"this.src='images/completieconfixed_transparant.png';"

如果为false,则显示"images/completeiconfixed_transparant.png",并将属性附加到onmouseout"this.src='images/completieconfixed.png';"

到目前为止,它第一次运行良好。它加载图像很好,当我第一次鼠标悬停时,它会更改图像,但第二次不会。

知道如何让它在每只老鼠身上工作吗。我的密码如下。

<asp:TemplateField HeaderText="C">
    <ItemTemplate>
        <asp:ImageButton ID="imgComplete" runat="server" CommandName="completeRecord" 
            CommandArgument='<%# Eval("TaskID") + "," + Eval("Completed")%>' 
            Height="16px" Width="16px"/>
    </ItemTemplate>
    <ItemStyle CssClass="mycol-md-3px mycol-xs-3px"></ItemStyle>
</asp:TemplateField>

protected void grdNetwork_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ImageButton imgComplete = (ImageButton)e.Row.FindControl("imgComplete");
        if (Convert.ToBoolean(hfCompleted.Value) == true)
        {
            imgComplete.ImageUrl = "images/completeiconfixed.png";
            imgComplete.Attributes.Add("onmouseover", "this.src='images/completeiconfixed_transparant.png';");
        }
        else
        {
            imgComplete.ImageUrl = "images/completeiconfixed_transparant.png";
            imgComplete.Attributes.Add("onmouseout", "this.src='images/completeiconfixed.png';");
        }
    }
}

提前谢谢。

您可以通过在以下两种情况下设置onmouseoveronmouseout来获得您想要的行为:

protected void grdNetwork_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ImageButton imgComplete = (ImageButton)e.Row.FindControl("imgComplete");
        if (Convert.ToBoolean(hfCompleted.Value))
        {
            imgComplete.ImageUrl = "images/completeiconfixed.png";
            imgComplete.Attributes.Add("onmouseover", "this.src='images/completeiconfixed_transparant.png';");
            imgComplete.Attributes.Add("onmouseout", "this.src='images/completeiconfixed.png';");
        }
        else
        {
            imgComplete.ImageUrl = "images/completeiconfixed_transparant.png";
            imgComplete.Attributes.Add("onmouseover", "this.src='images/completeiconfixed.png';");
            imgComplete.Attributes.Add("onmouseout", "this.src='images/completeiconfixed_transparant.png';");
        }
    }
}