javascript/asp.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashima515
    New Member
    • Nov 2006
    • 40

    javascript/asp.net

    Hi...
    M a newbe to asp.net. I am trying to use a javascript function in my asp.net page. The function works fine but for the second time. I have made a function that displays an alert box. but wen i click the button for the first time nothing happens but on second click the alert box is displayed. y so?

    here's the code:

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

    Button1.Attribu tes.Add("onClic k", "return hello()")
    End Sub

    n her's the javascript function included in the head of the asp.net form

    <script language="javas cript">
    function hello()
    {
    var msg= "hello"
    alert(msg);
    }
    </script>
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    My suggestion is to use:
    Code:
    Button1.Attributes.Add("onClick", "javascript:alert('hello')")
    If that works then step your code forward from there.

    Hope that this helps.

    Comment

    • ashima515
      New Member
      • Nov 2006
      • 40

      #3
      Hi..
      The problem was that I added the attribute on the click event of the button, so when the button id clicked for the first time the function was added and for thr second time it fired. So the solution is to add botton.attribut e.add() in the load event




      Originally posted by kenobewan
      My suggestion is to use:
      Code:
      Button1.Attributes.Add("onClick", "javascript:alert('hello')")
      If that works then step your code forward from there.

      Hope that this helps.

      Comment

      • Rathorevivek
        New Member
        • Dec 2006
        • 17

        #4
        Hi Ashima,
        Instead of declaring the code in Button_Submit event,add the following code in Page_load event:
        btnSubmit.Attri butes.Add("onCl ick","return hello();"); // in c#

        I hope it will certainly work fine for you.

        Comment

        Working...