Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2013, 03:02 PM   PM User | #1
Philosophaie
New Coder

 
Join Date: Sep 2010
Posts: 46
Thanks: 1
Thanked 1 Time in 1 Post
Philosophaie is an unknown quantity at this point
Javascript in ASP.net

How do you run a Javascript program in ASP.net 4.0 controls.

The old way:
<input id="Button1" type="button" onclick="program();" />

or

<body onload="program();">

ASP.net controls do not accept onclick or onload.

How do you run a Javascript in ASP.net on any control?
Philosophaie is offline   Reply With Quote
Old 03-05-2013, 09:27 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, the best way to do it is the modern "unobtrusive JavaScript" scheme.

Code:
<form id="Form1" runat="server">
<asp:button id="Button1" Text="Something" AutoPostBack="False" />
<asp:button id="Button2" Text="Whatever" AutoPostBack="False" />
...
</form>

<script type="text/javascript">
(
  function( ) /* anonymous master function */
  { 
      // form not needed for this example, but this is what you could use:
      var form = document.getElementById("<%=Form1.clientID %>");

      // for the buttons:
      document.getElementById("<%=Button1.clientID %>").onclick = program;
      document.getElementById("<%=Button2.clientID %>").onclick = program;

      function program( ) 
      { 
          // inside this function, you can refer to the button via this.  Example:
          var buttonValue = this.value; // gets the ASP.Net TEXT property 
          ...
      }

  } // end of master anonymous function
)(); // self-invoke anonymous function
</script>
</body><!-- notice the placement of the script! just before the body's end -->
</html>
CAUTION: If the button is set to do an autopostback, then you can't just assign to the onclick property, as shown. Instead, you would need to use attachEvent (for older MSIE) and/or addEventListener (for newer MSIE and all other browsers). If you do just assign to onclick, as shown above, that would disconnect the autopostback and *ONLY* execute your client-side JS code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:32 PM.


Advertisement
Log in to turn off these ads.