The javascript function is called first and then the CheckChanged event of the dynamically generated checkbox

In my project on one of the pages I create a checkbox and do some server side tasks when the checkbox changes. I want to show a confirmation message before going to the code.

If I call the javascript function it returns true / false (onclick event) but is not included in CheckboxCheckChanged.

I want a confirmation message to appear and depending on the user input it will go into the CheckboxCheckChanged event in the code behind

+2


source to share


4 answers


CheckBox1.Attributes["onclick"] = "Check();";

function Check ( elem )
        {
            if ( window.confirm ( "are you sure you want to do this?" ) )
            {
                __doPostBack ( '' , '' );
            }
            else
            {
                return false;
            }
        }

      



+2


source


JavaScript validation on checkbox, see the following, it's very simple and works:



<asp:CheckBox ID = "cbx_CoBorrNotPresent" runat="server" Text="Not Present" 
 AutoPostBack="false" TextAlign="Left" Checked="true"  onclick="javascript:ChkClick();" />



<script id="igClientScript" type="text/javascript">

        function ChkClick() 
        {
            var checkBox1 = document.getElementById('ctl00_cph_PageContent_cbx_CoBorrNotPresent');


            if (confirm('Are you sure?')) 
            {
                __doPostBack('ctl00$cph_PageContent$cbx_CoBorrNotPresent', '');

            }
            else 
            {
                return false;  
            }

        };
     </script>

      

+1


source


I found this problem a couple of times when using ASP: CheckBoxes with AutoPostBack property set to true.

If this property is true, ASP.NET fires an ugly inline onclick event in the generated HTML:

<input id="CheckBox1" type="checkbox" name="CheckBox1" 
 onclick="javascript:setTimeout('__doPostBack(\'CheckBox1\',\'\')', 0)" />

      

So if you set the onclick event again, it will be overridden and PostBack will not.

I found a workaround for this, mainly to store the original onclick event that ASP.NET generates and assign a new onclick function that will show the confirmation and if the user cancels the cancellation it will return false, otherwise the original click event will execute fine with the correct context and event object, and a postback will occur, for example:

window.onload = function() {
  var checkBox1 = document.getElementById('<%=CheckBox1.ClientID %>'),
      originalOnClick = checkBox1.onclick; // store original click event

  checkBox1.onclick = function(e) {
    if (confirm('Are you sure?')) {
      originalOnClick.call(this, e); // call the original click with the right
                                     // context and event object
    } else {
      return false;  // cancel the click
    }
  };
};

      

0


source


<asp:CheckBox ID="chkbox" runat="server" AutoPostBack="false"
              onclick="javascript: SomeFunction();" />

      

This will help. I have struggled with this a lot and found a solution. Make sure the "onclick" text is in small letters.

-1


source







All Articles