How to add a Javascript event in codebehind

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobertTheProgrammer
    New Member
    • Aug 2007
    • 58

    How to add a Javascript event in codebehind

    Hi all,

    I know this is a trivial question, but I can't seem to figure it out. Pardon my inarticulation in asking the question...

    I'm using ASP.NET such that in the OnLoad event of the codebehind, some javascript is dynamically assigned to a button's "onclick" event. Works as expected if the user clicks on the button. However, I also want this javascript to fire when other events occur on the page (i.e. certain values are entered into select textboxes). How, in the codebehind, can I simulate the user clicking this button so that the onclick javascript fires?

    Thanks.

    Robert
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi Robert,

    You can assign javascript to other controls in the same way as you did with your button. Check out this for a list of the possible events that you can use.

    If you want to use the same javascript code for different events then you should store it as a function. A Javascript function defined between the <head /> tags can be called from any part of your page. Simply define the function and call it like so:

    On your .aspx page:
    Code:
    <head> 
    <script type="text/javascript">
    function TestFunction()
    {
    alert('Hello World!!')
    }
    </script>
    </head>
    On your code behind page:
    Code:
     
    textbox1.attributes.add("onchange", "TestFunction();")
    Does this help?

    Dr B

    PS Robert, you've posted your question in the ASP Forum which is for Classic ASP only - I've moved it for you but please post all your future ASP.NET questions in the .NET forum. Thanks.

    Comment

    • deric
      New Member
      • Dec 2007
      • 92

      #3
      Could the following help?

      In your code behind:
      Code:
      Public myEvent As String
      myEvent = ""
      ...
      myEvent = "myFunction();"
      In your .aspx page:
      Code:
      <html>
      <script language="javascript">
      function myFunction(){
      ...
      }
      </script>
      ...
      <input type="button" name="myButton" onclick="<% =myEvent %>" />
      <input type="input" name="myText" onchange="<% =myEvent %>" />
      ...
      </html>

      Comment

      • kunal pawar
        Contributor
        • Oct 2007
        • 297

        #4
        DrBunchman is gives right code and solution hope this will helps you

        Comment

        • RobertTheProgrammer
          New Member
          • Aug 2007
          • 58

          #5
          First of all, thanks for moving the thread to the .NET forum instead. I'm still learning...

          Second, thanks for the response. I kind of figured that calling the javascript was the way to do it, but I just couldn't get my head wrapped around it.

          Thanks again.

          Robert

          Comment

          Working...