Tuesday, 24 April 2012

How to change the Image of a button or control on MouseOver as in Windows7


<script type="text/javascript">
        function OnMouseOver( ) {           
            document.getElementById("imgbtnSubmit").src = "btnActive.png";
        }
        function OnMouseOut( ) {
            document.getElementById("imgbtnSubmit").src = "btnInActive.png";
        }
    </script>

Method 1 :-
    Call the above JavaScript function on client MouseOver and MouseOut;

Method 2 :-
    Using styles in the CSS file,
 You can change the image on hover and on out of focus you can show the default Image.

for e.g. :
              <asp:ImageButton   ID="imgBtnSubmit"  ImageUrl="~/Image/btnInActive.png" CssClass="btnSubmit" runat="server"></asp:ImageButton>

In the CSS File:
      .btnSubmit:hover {backgroundImage:url("~/images/btnActive.png"); }

 Method 3 :-

In the page's Load event, call the Add method of the control's Attributes collection.

imgbtnSubmit.Attributes.Add("onmouseover","OnMouseOver( )");
imgbtnSubmit.Attributes.Add("onmouseout", "OnMouseOut( )");

Just enjoy..........that's all.

No comments:

Post a Comment