locked
onserver_change event not firing RRS feed

  • Question

  • User-153404742 posted

    I have the following checkbox and related Javascript....but the onserverchange event only fires the first time page is loaded... <input id="btnYesNo" runat="server" class="test_check" type="checkbox" onserverchange="btnYesNo_OnServerChange" /> Javascript is as follows:

    $(document).ready(function(){
      // triggered each time a field is checked
      $('.test_check').lc_switch();
     
      $('body').delegate('.test_check', 'lcs-on', function () {
          console.log('field is checked');
          __doPostBack('btnYesNo', '');
      });
     
      // triggered each time a is unchecked
      $('body').delegate('.test_check', 'lcs-off', function () {
        console.log('field is unchecked');
      });
    });
    debuggin shows the console log that field is updated to checked or unchecked....but the code behind  protected void btnYesNo_OnServerChange(object sender, EventArgs e) only gets fired the very first time page is loaded.....any idea on how to fire this event each time the yesno button is changed?
    Wednesday, February 27, 2019 7:44 PM

All replies

  • User475983607 posted

    I cannot reproduce this behavior.  The onserverchange fire when the checkbox changes.

        <div>
            <input id="btnYesNo" runat="server" class="test_check" type="checkbox" onserverchange="btnYesNo_ServerChange" value="test" />
        </div>
        <div>
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </div>
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </div>
        public partial class _Default : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Label1.Text = string.Empty;
            }
    
            protected void btnYesNo_ServerChange(object sender, EventArgs e)
            {
                Label1.Text = "btnYesNo_ServerChang " + btnYesNo.Checked;
            }
        }

    Wednesday, February 27, 2019 8:59 PM
  • User-474980206 posted

    $.delegate hasn't been supported for years, you must be using really old jQuery or its throwing an error. check the console for errors. also onserverchange calls __dopostback, so why are you calling it?

    Thursday, February 28, 2019 5:12 PM
  • User-153404742 posted

    not sure if the javascript that displays on/off button instead of the checkbox affects this....the "test_check" class ?

    Friday, March 1, 2019 7:04 PM
  • User839733648 posted

    Hi inkaln,

    According to your description and code, I'm sorry that I could not reproduce your issue.

    Nothing has happened running your code, and there is an information in the console tab that could not identify .lc_switch().

    And I'm confused that how do you use delegate.

    I suggest that you could use onserver_change event like the following,

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <script language="C#" runat="server">
    
            void Button1_Click(object sender, EventArgs e)
            {
    
                if (Prev_Check_State.Value == Check1.Checked.ToString())
                    Span2.InnerHtml = "CheckBox1 did not change state between clicks.";
    
                if (Check1.Checked == true)
                {
                    Span1.InnerHtml = "CheckBox1 is selected!";
                    Prev_Check_State.Value = "True";
                }
                else
                {
                    Span1.InnerHtml = "CheckBox1 is not selected!";
                    Prev_Check_State.Value = "False";
                }
    
            }
    
            void Server_Changed(object sender, EventArgs e)
            {
                Span2.InnerHtml = "CheckBox1 changed state between clicks.";
            }
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <input id="Check1" type="checkbox" onserverchange="Server_Changed" runat="server" />
            CheckBox1 &nbsp;&nbsp;
          <span id="Span1" style="color: red" runat="server" />
            <br />
            <input type="button" id="Button1" value="Enter" onserverclick="Button1_Click" runat="server" />
            <br />
            <br />
            <span id="Span2" runat="server" />
            <input type="hidden" id="Prev_Check_State" visible="false" runat="server" />
        </form>
    </body>
    </html>
    

    Reference: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.htmlcontrols.htmlinputcheckbox.onserverchange?view=netframework-4.7.2

    Best Regards,

    Jenifer

    Tuesday, March 5, 2019 9:24 AM