Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am so tired and frustrated, i tried everything and it won't work, why is this code not working? when i press the button it should run the javascript function. asp.net with java script

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function LoadJScript() {


            var message = "Hello";

            alert(message);
        }
    </script>
</head>
<body>
    <form id="form1" method="post" runat="server">
        <div>

            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="LoadJScript()" />

        </div>
    </form>
</body>
</html>


What I have tried:

i tried to put the javascript code inside the body, i tried everything but it won't work
Posted
Updated 4-Feb-23 14:34pm
Comments
Member 15627495 4-Feb-23 6:07am    
js is sometimes so demanding... write 'onClick' , not OnClick

As I told you in your other question, it will not call the javascript function because you're using a server-side control, asp:Button.

Change the asp:Button to a normal HTML button element and you can easily call the javascript code.
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function LoadJScript() {


            var message = "Hello";

            alert(message);
        }
    </script>
</head>
<body>
    <form id="form1" method="post" runat="server">
        <div>
            <button type="button" id="Button1" onClick="LoadJScript()">text</button>
        </div>
    </form>
</body>
</html>
 
Share this answer
 
If you wanted to call a javascript function, you would have to wire up the click event yourself, when the page loads. Something like this:
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormsTest.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test</title>

    <script src="/Scripts/jquery-3.4.1.min.js"></script>

    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            $('#<%= okButton.ClientID %>').click(function (e) {
                LoadJScript();
            })
        });

        function LoadJScript() {
            var message = 'Hello';
            alert(message);
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <asp:Button id="okButton" text="OK" runat="server"/>
    </form>
</body>

</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900