asp.net - Calling a Javascript function first and then CheckChanged event of a Checkbox created dynamically - Stack Overflow

In my project in one of the page I am creating a checkbox and doing some server side task when the chec

In my project in one of the page I am creating a checkbox and doing some server side task when the checkbox check changes. What I want is to show a confirm message before going to the code behind.

If I am calling the Javascript function then it is returning true/false (onclick event) but not going inside CheckboxCheckChanged.

I want the confirmation message should appear and depending upon the user input it will go inside the CheckboxCheckChanged event in code behind

In my project in one of the page I am creating a checkbox and doing some server side task when the checkbox check changes. What I want is to show a confirm message before going to the code behind.

If I am calling the Javascript function then it is returning true/false (onclick event) but not going inside CheckboxCheckChanged.

I want the confirmation message should appear and depending upon the user input it will go inside the CheckboxCheckChanged event in code behind

Share Improve this question edited Aug 29, 2020 at 22:16 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 28, 2009 at 5:55 Deepak KumarDeepak Kumar
Add a ment  | 

4 Answers 4

Reset to default 2
CheckBox1.Attributes["onclick"] = "Check();";

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

JavaScript Confirm on check-box, See the following it is very simple and working:

<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>

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

If that property is true, ASP .NET creates an ugly inline onclick event on the generated HTML as this:

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

So if you set again the onclick event, it will be overriden, and the PostBack won't occur.

I found a workaround to this, is basically to store the original onclick event that ASP .NET generates, and assign a new onclick function, which will show the confirmation, and if the user selects cancel it will return false, otherwise the original click event will be executed normally with the right context and event object, and the 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
    }
  };
};
<asp:CheckBox ID="chkbox" runat="server" AutoPostBack="false"
              onclick="javascript: SomeFunction();" />

This will help. I struggled a lot on this one and found the solution. Make sure that the "onclick" text is all small letters.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744722250a4589977.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信