locked
onChange event's designated function is undefined? 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? 

    Thursday, August 2, 2018 12:57 AM

All replies

  • User409696431 posted

    The event you are looking for is called OnTextChanged not onChanged.  Correct that in your TextBox markup and see if that solves the problem.

    Thursday, August 2, 2018 3:54 AM
  • User-390139731 posted

    Changing OnTextChanged removed the unidentified error poping up. However, the txtChanged function is not activating the debug writeline. 

    Oddly enough, instead it looks like the Load_Page is being run again when the textbox is no longer in focus. That is enough for me to wiggle around for my project! 

    As for double posting, I thought a mod had removed my first thread since it wasn't approved and not showing up. Figured I might have been posting in the wrong form. 

    Thursday, August 2, 2018 4:06 AM
  • User409696431 posted

    OnTextChanged means it has to go back the server to run.  Naturally, the Page loads again.  It's not a local (javascript) event.

    As to Write.Debug not working, it may be working. Where do you expect to see the results?  You must be running in debug mode (VS in debug mode, webconfig compile element with debug="true" attribute), and it will show in the Output window when you are running.  (CRL-Alt-O if you don't see the output window.)  Also, be aware that OnTextChanged fires when you have changed the text, and also have clicked outside the TextBox or hit enter or tab - something to tell it that you have finished your entry.

    Thursday, August 2, 2018 5:02 AM
  • User-893317190 posted

    Hi  Quick_Question_,

     

    As KathyW has said , the Debug.WriteLine() only works when you are in debug mode, so you couldn’t see the output  the code when you are not in debug mode.

     

    As to the onChange event ,it only fires  at client side,so if you want it to work ,you should write js code at client side and it will not refresh the page.

     

    Below is my code. Please don’t add AutoPostBack="true".

    <asp:TextBox ID="TextBox1" runat="server" onChange="change()" ></asp:TextBox>
    
    <script>
            function change() {
                alert("item changed");
            }
    </script>
    

    If you want to trigger the event at server side  and don’t want the page to refresh. You could use an updatepanel.

     
    If you want to show message when you are not in debug mode , you could  register a  script , just write your own js code  as the third  parameter of ScriptManager.RegisterStartupScript method. Updatepanel needs a  scriptManager  in front of it  and you should add your textbox in updatePanel’s   contentTemplate.

    Below is my code.

      <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                          <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
                    </ContentTemplate>
    
                </asp:UpdatePanel>
    
    protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "myScript","alert('item changed')", true);
            }
    

    Best regards ,

    Ackerly Xu

    Friday, August 3, 2018 10:12 AM