Gridview ImageButton在鼠标悬停和鼠标移动时更改图像

时间:2016-05-15 12:17:29

标签: javascript c# jquery asp.net gridview

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

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

如果为false则显示" images / completeiconfixed_transparant.png"并将该属性附加到onmouseout" this.src =' images / completeiconfixed.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';");
        }
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

在这两种情况下,您可以通过设置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';");
        }
    }
}