Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling Javascript from an asp button

Status
Not open for further replies.

LouisC4

Programmer
Jul 26, 2003
54
US
Hello, I am trying to call a javascript from a asp button but I am not getting any results, here is my code:

Code:
<head runat="server">
    <title>Untitled Page</title>

    <script language="javascript">
        var msec=0
        var sec=0
        var min=0
        var hrr=0
        
        function start()
        {
            document.forms[0].display.value=hrr+":"+min+":"+sec
            go=setTimeout("start()",1)
            msec++
            
            if(msec==100)
                {
                    msec=0
                    sec++
                }
            if(sec==60)
                {
                    sec=0
                    min++
                }
            if(min==60)
                {
                    min=0
                    hrr++
                }
        }
    </script>  
</head>
<body>
<form id="form1" runat="server">
     <asp:Button ID="btnOK" runat="server" Text="OK" Width="50px"  />
</form>
</body>

On Page_Load I have:

Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        btnOK.Attributes.Add("onClick", "return start();");
    }

What is wrong?

Thanks,

Louis
 
My first guess is that you are posting back the page and loosing state.

Never argue with an idiot. He'll just drag you to his level and beat you with experience!
 
How can you keep it from posting back?
 
Try adding the call to the function to the OnClientClick event of the Button.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I've tried onClientClick and it still doesn't fire my javascript.

It is strange.
 
It works perfectly for me in a simple test:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function test() {
            alert('hello');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="test();" />
    </div>
    </form>
</body>
</html>


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top