Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Newbie Here

Below is my script that runs each time a checkbox node is clicked within a treeview on a asp.net vb webpage. The Script works fine.

I would like for this same script to run each time a postback occurs on my page. I know that I believe that I need to use something like this:

VB
ScriptManager.RegisterStartupScript(Me, Page.GetType(), "scriptKey", "somefuncname();", True)


On my code behind . . . . but it does not seem to work

Any help is appreciated
Greg

JavaScript
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(function() {
            $("[id*=TreeView2] input[type=checkbox]").bind("click", function () {
                var table = $(this).closest("table");
                if (table.next().length > 0 && table.next()[0].tagName == "DIV") {
                    //Is Parent CheckBox
                    var childDiv = table.next();
                    var isChecked = $(this).is(":checked");
                    $("input[type=checkbox]", childDiv).each(function () {
                        if (isChecked) {
                            $(this).attr("checked", "checked");
                        } else {
                            $(this).removeAttr("checked");
                        }
                    });
                } else {
                    //Is Child CheckBox
                    var parentDIV = $(this).closest("DIV");
                    
                    if ($("input[type=checkbox]", parentDIV).length == $("input[type=checkbox]:checked", parentDIV).length) {
                                                $("input[type=checkbox]", parentDIV.prev()).attr("checked", "checked");
                        $("input[type=checkbox]", parentDIV.prev()).prop("indeterminate", false);
                    } else {
                       if($("input[type=checkbox]:checked", parentDIV).length == 0) {
                                                     $("input[type=checkbox]", parentDIV.prev()).removeAttr("checked");
                           $("input[type=checkbox]", parentDIV.prev()).prop("indeterminate", false);
                    }else{
                                                      $("input[type=checkbox]", parentDIV.prev()).prop("indeterminate",true);
                    }
                    }
                }
        });
        })
    });
   </script>
Posted
Updated 7-Jul-14 8:08am
v4
Comments
Why you need this to work on each Post Back?
Member 10926423 7-Jul-14 15:04pm    
Due to the inability of the TreeView to correctly handle the OnCheckedChanged Event.

When a postback occurs on my page, the TreeView checkbox nodes lost the Indeterminate prop assignment.
Kornfeld Eliyahu Peter 7-Jul-14 16:50pm    
That's not a problem of the TreeView or OnCheckedChange - both work perfect.
It sounds you have some design problems around your code - debug post-back scenario and check what happening to TreeView then...
Why it is not working correctly? Have you debugged and see what is happening?

1 solution

Hi, I'm not sure if this is the problem you are facing but normally when you bind event handlers with jquery in a web forms application, the binding is lost during post-backs. If that is the case, you would need to use the "Sys.WebForms.PageRequestManager" object to rebind the events between post-backs. You should try something like this:

JavaScript
<script type="text/javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestAddress);

        function EndRequest(o, e) {
            bindHandlersAddress();
        }
		
        function bindHandlers() {
		//rebind controls and handlers:
		            $("[id*=TreeView2] input[type=checkbox]").bind("click", function () {
                var table = $(this).closest("table");
                if (table.next().length > 0 && table.next()[0].tagName == "DIV") {
                    //Is Parent CheckBox
                    var childDiv = table.next();
                    var isChecked = $(this).is(":checked");
                    $("input[type=checkbox]", childDiv).each(function () {
                        if (isChecked) {
                            $(this).attr("checked", "checked");
                        } else {
                            $(this).removeAttr("checked");
                        }
                    });
                } else {
                    //Is Child CheckBox
                    var parentDIV = $(this).closest("DIV");
                    
                    if ($("input[type=checkbox]", parentDIV).length == $("input[type=checkbox]:checked", parentDIV).length) {
                                                $("input[type=checkbox]", parentDIV.prev()).attr("checked", "checked");
                        $("input[type=checkbox]", parentDIV.prev()).prop("indeterminate", false);
                    } else {
                       if($("input[type=checkbox]:checked", parentDIV).length == 0) {
                                                     $("input[type=checkbox]", parentDIV.prev()).removeAttr("checked");
                           $("input[type=checkbox]", parentDIV.prev()).prop("indeterminate", false);
                    }else{
                                                      $("input[type=checkbox]", parentDIV.prev()).prop("indeterminate",true);
                    }
                    }
                }
        });
        }

        $(document).ready(function () {
            bindHandlers();
        });
    </script>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900