javascript onchange not firing

Posted on 2011-10-18
Last Modified: 2012-05-12
I am trying to implement a way to keep track of the dirty status of controls within wizard steps.
I have put a hidden field in each step to track changes and want to set this field value to "true"/"false".  I have added an onchange event to each textbox and dropdownlist, but the event doesn't fire.
I even tried adding an attribute.add to the textbox, but I still don't get the alert.
I am also using masterpages.
How do I make this work?
Private Sub wzdContactInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles wzdContactInfo.Load
      If Not Page.IsPostBack Then
         Me.txtFirstName.Attributes.Add("onchange", "javascript:setdirty('HiConInfoDirty')")
      End If
   End Sub

<asp:TextBox ID="txtFirstName" runat="server" onchange="javascript:setdirty('HiConInfoDirty');" MaxLength="50" CssClass="backyellow" EnableViewState="true" AutoPostBack="true" ></asp:TextBox>

####Javascript function

function setdirty(hifield) {
    var hi = document.getElementById(hifield);
    hi.value = "true";
    var hipage = wzdConInfo.getElementById("HiPageDirty");
    hipage.value = "true";
    alert('Set Dirty');


Open in new window

Question by:Sheritlw
    LVL 81

    Expert Comment

    place your alert at the beggining (line 14) instead at the end
    LVL 13

    Expert Comment

    instead of onchange, try onKeyUp or onKeyDown events..
    LVL 28

    Expert Comment


    Me.txtFirstName.Attributes.Add("onchange", "alert('test')")

    Didn't work? moved this outside If Not Page.IsPostback...


    function setdirty(hifield) {

    Alert not what expected? Fix it. Alert show? setdirty() needs to be fixed. Alert doesn't show...

    Then Try:
    Me.txtFirstName.Attributes.Add("onchange", "setdirty('HiConInfoDirty')")

    Still nothing?

    Let me know.
    LVL 28

    Expert Comment

    "instead of onchange, try onKeyUp or onKeyDown events.."

    If using these, you will have to check for Tab, Shift, Control, etc and filter them out.

    Author Comment

    I set the alert at the beginning of the event and it did fire.
    The reason I had the alert in there was to make sure the values were being set to the hidden field.
    I am wondering if it didn't show the alert was because the value was not getting set.
    Should I be passing the rendered id of the hidden field, or just the name?
    How can I make sure this works?
    LVL 28

    Accepted Solution

    You need to pass the rendered ID of the control. The best way to do this is via the ClientID property.

    e.txtFirstName.Attributes.Add("onchange", "javascript:setdirty('" + HiConInfoDirty.ClientID + "')")

    Author Closing Comment

    Thank you

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now