Sunday, April 3, 2011

Bind a Javascript function to TreeView CheckBox from ServerSide ASP.Net

I have a treeView with ShowAllCheckBox property to true, I need to bind a javascript function to each checkbox on their click event.

How to do this can any one suggest ?

From stackoverflow
  • Using Jquery, you can easily do it as follows

    $("#TreeView1 input[type='checkbox']").bind('click',function(){
                var ischecked = (this.checked == true);
                alert(ischecked);
             });
    
  • You can't do it server side w/o re-parsing render output. TreeView does not use Controls collection and renders input elements directly.

    You'd have to resort to some form of java script as suggested by gk or traditionally:

    <script type="text/javascript" language="javascript">
        var inputs = document.getElementsByTagName("input");
        for (i = 0; i < inputs.length; i++)
         if (inputs[i].type == "checkbox" && inputs[i].name.indexOf("TreeView1", 0) == 0)
          inputs[i].onclick = function() { alert('click'); };
    </script>
    
  • Hey Ruslan, Where do you put this script in the .aspx file? and how you can tell the TreeView to apply this script?

    ripper234 : Use a comment next time instead of an answer (or just ask a new question)

0 comments:

Post a Comment