Postback checkbox Asp.net with Switchery plugin

Asked

Viewed 301 times

1

I’m putting together a list of records in a table with Asp.net. I create a checkbox for each line to perform a postback action every time the checkbox is checked or unchecked. I use the Switchery plugin to style this checkbox, but when I click on the switch (action equal to checking/unchecking the checkbox), it does not perform postback. If I comment the line with the default class used by the plugin or change the class name (thus leaving the "raw" checkboxes in the table) the postback works correctly.

JS Switch Creation Code

 var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
            elems.forEach(function (html) { 
                var switchery = new Switchery(html); 
            });

Code C#

CheckBox lkBtnExcluir = new CheckBox();
                        lkBtnExcluir.ID = dr["empr_codigo"].ToString() + "_id"; 
                        lkBtnExcluir.AutoPostBack = true;
                        lkBtnExcluir.Attributes.Add("USUARIO", dr["COD_USUARIO"].ToString());
                        lkBtnExcluir.InputAttributes.Add("class", "js-switch");
                        //lkBtnExcluir.CssClass = "js-switch";

                        if (dr["usa_atend"].ToString() == "S")
                        {
                            lkBtnExcluir.Checked = true;
                            lkBtnExcluir.Attributes.Add("STATUS", "N"); 
                        }
                        else
                        {
                            lkBtnExcluir.Attributes.Add("STATUS", "S"); 
                        }

                        lkBtnExcluir.CheckedChanged += new EventHandler(mudarStatus_click);
                        tdAcao_.Controls.Add(lkBtnExcluir);

1 answer

0

I was able to fix it. In case someone has the same problem I had, follow an event on Javascript to force the Postback:

  <script>
  $(".js-switch").change(function () {
            __doPostBack($(this).attr("id"), '');
        });
  </script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.