locked
onChange event errors out with undefined function RRS feed

  • Question

  • User-390139731 posted

    Hi all! 

    I'm just learning ASP.net with visual studio. I think this will be an obvious question but I can't see the forest from the trees yet.
    I have a test page called test.aspx - It contains this html:

    <asp:Textbox id="myTextbox" runat="server" onChange="txtChanged" AutoPostBack="true"/>

    on the test.aspx.cs script I have this snippet:

    namespace WebApp
    {
       public partial class test: System.Web.UI.Page
       {
        public void txtChanged (Object sender, EventArgs e){
            Debug.WriteLine("Item changed");
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }

    When running the site, there will be an error saying 'txtChanged' is undefined.  I think I'm adding my method either to the wrong file, placing it at the wrong level, or need to add another hook somewhere.  

    What am I doing wrong? 

    Wednesday, August 1, 2018 10:41 PM

All replies

  • User409696431 posted

    Please don't post the same question twice.  You (and others) will miss the answers.   I've answered you in your other post: https://forums.asp.net/t/2144837.aspx?onChange+event+s+designated+function+is+undefined+

    Thursday, August 2, 2018 4:03 AM
  • User283571144 posted

    Hi Quick_Question_:

    According to your codes, I found you use onChange attribute.

    But the asp textbox control doesn't contain the onChange attribute, it only contains the OnTextChanged event.

    I suggest you could use  OnTextChanged event instead of onchange.

    More details, you could refer to below test demo codes:

    Notice: This event will be fried when losing focus on the textbox.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextBoxChangedEventTest.aspx.cs" Inherits="AspNetNormalIssue.Webform.TextBoxChangedEventTest" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Textbox id="myTextbox" runat="server"   OnTextChanged="txtChanged"   AutoPostBack="true"/>
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

    using System;
    using System.Diagnostics;
    
    namespace AspNetNormalIssue.Webform
    {
        public partial class TextBoxChangedEventTest : System.Web.UI.Page
        {
            public void txtChanged(Object sender, EventArgs e)
            {
             
                Debug.WriteLine("Item changed");
                Response.Write("Item changed");
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
           
        }
    }

    Result:

    Best Regards,

    Brando

    Thursday, August 2, 2018 6:33 AM