Saturday, December 17, 2011

How to add a client event handler to an ASP.NET server control declaratively

You can add client script to controls on an ASP.NET Web page declaratively, as you would to HTML elements. In the control's markup, add an attribute for the event, for example, onmouseover or onkeyup. For the attribute's value, add the client script that you want to execute. The following code example shows an ASP.NET Web page that includes client script that changes the button text color when the user passes the pointer over it.
Here is javascript.
 <script type="text/javascript">
        var previousColor;
        function MakeRed(e) {
            var targ;
            // e gives access to the event in all browsers
            if (!e) var e = window.event;
            if ( targ =;  //for IE
            else if (e.srcElement) targ = e.srcElement;  //for Firefox
            previousColor =;
   = "#FF0000";

        function RestoreColor(e) {
            var targ;
            if (!e) var e = window.event;
            if ( targ =;
            else if (e.srcElement) targ = e.srcElement;
   = previousColor;

 Here is form tag.
<form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button1" onmouseover="MakeRed(event);"
            onmouseout="RestoreColor(event);" />

See the other example :

No comments:

Post a Comment