Search code examples
c#asp.netcheckboxupdatepanelautopostback

ASP.NET C#: OnCheckedChanged event processing inside of UpdatePanel


While creating my login-form, I need a checkbox option, allowing user to show <asp:TextBox TextMode="Password"> input, or hide it back again, but it seems my <asp:CheckBox ID="chkShowPass"> does not process chkShowPass_CheckedChange event. I found an article on this topic (ASP.NET Checkbox.Checked is not working), where chkShowPass.AutoPostBack property set to true settles the problem.

When I have plain ASP.NET example to put <asp:CheckBox ID="chkShowPass"> in, I'm able to do that with no problem, but the key thing, I have now added <asp:UpdatePanel> element to prevent my website pages from auto-scrolling on button-clicks, and to make my good-basket UI (which lies in Site.Master) update properly from those clicks.

What shall I do, to make my <asp:CheckBox ID="chkShowPass"> react to chkShowPass_CheckedChange event, but, still, keeping my Site.Master's content in <asp:UpdatePanel>? I will also appreciate, if you know any way to prevent page from upscrolling on click, without <asp:UpdatePanel>.


Solution

  • You can do it using jQuery. give id to Password Textbox and a checkbox

    <script type='text/javascript'>
        $(document).ready(function(){
            $('#checkbox').click(function(){
                if($(this).is(':checked'))
                {
                    $('#password_id').attr('type', 'text');
                }
                else
                {
                    $('#password_id').attr('type', 'password');
                }
            });
        });
    </script>
    

    Remember to add jquery script in a page.

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>