简体   繁体   中英

Why onClick function isn't recognized?

I'm trying to make a simple button onClick() function and I get this error:

Error 8 'ASP.default_aspx' does not contain a definition for 'onClick' and no extension method 'onClick' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)

Simply I just want to make that when I click the button, an alert will be popped up.

This is my code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"   Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">

    var mail=document.getElementById('Text1');
    var pass=document.getElementById('Password1');
    var btn = document.getElementById('Button1');
    function onClick(){
    alert("Got in");


        }
</script>

</head>
<body>
<form id="form1" runat="server" name="login">

<div align="center">
<br /><br /><br /><br /><br /><br /><br /><br />
    <asp:Label ID="Label1" runat="server" Text="Email:" align="center">     </asp:Label><input id="Text1"
         type="text" align="center" /><br /><br />
    <asp:Label ID="Label2" runat="server" Text="Password:" align="center">  </asp:Label><input id="Password1"
        type="password" align="center" /><br /><br />
    <asp:Button ID="Button1"  runat="server" Text="Login" align="center"  OnClick="onClick()"/><br /><br />

</div>

</form>

</body>
</html>

The OnClick handler is a server side handler. This isn't meant to be used to call into javascript but rather to post back to the server and then call the VB/C# function by that name.

Try using the OnClientClick property instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM