【问题标题】:Gridview ImageButton change image on mouseover and mouseoutGridview ImageButton 在鼠标悬停和鼠标悬停时更改图像
【发布时间】:2016-05-15 12:17:29
【问题描述】:

我有 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';");
        }
    }
}

提前致谢。

【问题讨论】:

    标签: javascript c# jquery asp.net gridview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多