Calling a Javascript function first and then CheckChanged event of a Checkbox created dynamically

11,892

Solution 1

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

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

Solution 2

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>
Share:
11,892

Related videos on Youtube

Brian Tompsett - 汤莱恩
Author by

Brian Tompsett - 汤莱恩

I am a lecturer of Computer Science at the University of Hull. I have worked in the software industry in the US and the UK. I have over 50 years software development experience, in writing compilers, operating system implementation and porting, networking protocol stacks, protocol analysers and software for arcane and unusual architectures. I am experienced at answering and solving students' programming and computer science questions. I teach the networking, compilers and security courses. My research areas include computer crime, forensics and security. I was the first timelord and created the tardis.

Updated on April 16, 2022

Comments

  • Brian Tompsett - 汤莱恩
    Brian Tompsett - 汤莱恩 about 2 years

    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