Click here to Skip to main content
15,881,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone solve this? it keeps telling me there is an error in line 32 ( the asp:Button Line), "CS1061: 'webform1_aspx' does not contain a definition for 'submitButtonClick' and no accessible extension method 'submitButtonClick' accepting a first argument of type 'webform1_aspx' could be found (are you missing a using directive or an assembly reference?"

Here is the Code:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test</title>
    <script type="text/javascript" language="JScript">
        function submitButtonClick(sender: Object, events: EventArgs): void {
            if (IsPostBack) {
                if (iceCream.SelectedItem == "Yes") {
                    message.Text = name.Text + " Likes ice cream!";
                }
                else {
                    message.Text = name.Text + " Does not like ice cream!";
                }
            }
        }

    </script>
</head>
<body>
    <form id="form1" method="post" runat="server">
        name: <asp:TextBox ID="name" runat="server" />
        <br />
        Do you like icecream?
        <asp:RadioButtonList ID="iceCream" runat="server" >
        <asp:ListItem>Yes</asp:ListItem>
        <asp:ListItem>No</asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <asp:Button text="submit" runat="server" onClick="submitButtonClick" />
        <br />
        <center>
           <h1> <asp:Label ID="message" runat="server"></asp:Label></h1>
        </center>
      
    </form>
</body>
</html>


What I have tried:

I tried everything< i've been trying for hours now with no luck, anyone knows the answer??
Posted
Updated 4-Feb-23 6:38am

1 solution

The onClick attribute in the asp:Button element is telling the control what method in your backing C# code to call, NOT YOUR JAVASCRIPT!

The asp: elements are executed on the server to generate HTML elements. The postback will be executed in the C# code, NOT YOUR JAVASCRIPT!

You have to change the code to work as C#. Also, the IceCream.SelectedItem cannot be compared to a string. You have to compare the SelectedItem.Text property to a string instead.
This updated code should work:
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 language="C#" runat="server">
        void submitButtonClick(Object sender, EventArgs e)
        {
            if (IsPostBack) {
                if (IceCream.SelectedItem.Text == "Yes")
                {
                    message.Text = name.Text + " Likes ice cream!";
                }
                else
                {
                    message.Text = name.Text + " Does not like ice cream!";
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        name: <asp:TextBox id="name" runat="server" />
        <br />
        Do you like icecream?
        <asp:RadioButtonList id="IceCream" runat="server" >
        <asp:ListItem>Yes</asp:ListItem>
        <asp:ListItem>No</asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <asp:Button text="submit" runat="server" onClick="submitButtonClick" />
        <br />
        <center>
           <h1><asp:Label id="message" runat="server"></asp:Label></h1>
        </center>    
    </form>
</body>
</html>

If you want to change this code to work with javascript instead, you're going to have to rework just about everything. Typically, you wouldn't use the server-side controls (everything that starts with asp:).
 
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