Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

Rate It (1)

Last post 06-25-2009 6:38 AM by sudhi345. 9 replies.

Sort Posts:

  • Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    05-10-2007, 4:03 PM
    • Member
      point Member
    • yaoliu
    • Member since 01-11-2007, 5:37 PM
    • Posts 2

    My requirement is quite simple:

    1. Show an organizational hierarchy in a treeview. for example, company -> department1, department2 etc,  Every treenode has a checkbox displayed next to it.
    2. Whenever the parent node is checked, all its child nodes will get checked as a result of it. I want to handle this event on the server side.
    #1 is easy to do, but I can't figure out how to do #2 because there is no event gets fired for the checkbox click. Is there any way to fire this event to the server? Any help is greatly appreciated!!!
  • Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    05-13-2007, 11:36 PM
    Answer

    Hi,

     In the treeView you can use the TreeNodeCheckChanged event for the checkbox click.

    In the MSDN's  TreeView.TreeNodeCheckChanged Event , you could get more inforamtion.

    if you find the the treeview could not PostBack, I am afraid you should require javascript. Below is a simple example:

    <script language="javascript" type="text/javascript">

    function postBackByObject()
    {
        var o = window.event.srcElement;
        if (o.tagName == "INPUT" && o.type == "checkbox")
        {
           __doPostBack("","");
        }
    }
        </script>

     <asp:TreeView ID="TreeView1" EnableClientScript="true" onclick="javascript:postBackByObject()"  runat="server" DataSourceID="SiteMapDataSource1" LineImagesFolder="~/TreeLineImages" ShowLines="True" OnTreeNodeDataBound="TreeView1_TreeNodeDataBound" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged" ShowCheckBoxes="All">

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    05-16-2007, 2:31 PM
    • Member
      2 point Member
    • freeborn5
    • Member since 05-16-2007, 5:47 PM
    • Posts 4

     Thanks So much I was stumped on this the javascript works like a charm

  • Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    05-29-2007, 1:06 PM
    • Member
      point Member
    • yaoliu
    • Member since 01-11-2007, 5:37 PM
    • Posts 2

     Thanks a lot, Amanda, that will work.

  • Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    07-03-2007, 8:18 AM
    • Member
      2 point Member
    • jeny_p
    • Member since 07-03-2007, 12:07 PM
    • Posts 1

    Hi, I have the same problem but this code didn't work for me and I got the warning "Attribute onclick is not a valid attribute of element TreeView"

    What might the problem be?

    thanks

  • Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    07-19-2007, 8:26 AM
    • Member
      39 point Member
    • VinBrown
    • Member since 07-06-2007, 1:57 PM
    • Posts 23

    Amanda, you are awesome. I needed a quick solution to this problem and this worked great.

  • Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    08-24-2007, 4:54 AM
    • Member
      4 point Member
    • abcpaem
    • Member since 08-24-2007, 8:41 AM
    • Posts 2

    Thank you, Amanda, for your post, I just would change two little things in order to make it compatible with other navigators (Firefox, etc..):

    function postBackByObject(mEvent)
    {
        var o;
        // Internet Explorer    
        if (mEvent.srcElement)
        {
            o = mEvent.srcElement;
        }
        // Netscape and Firefox
        else if (mEvent.target)
        {
            o = mEvent.target;
        }
        if (o.tagName == "INPUT" && o.type == "checkbox")
        {
           __doPostBack("","");
        } 
    }

    and add the event parameter to the fucntion call: onclick="javascript:postBackByObject(event)"

    Thanks a lot!

    Patrick.

  • Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    08-24-2007, 12:16 PM
    • Member
      4 point Member
    • abcpaem
    • Member since 08-24-2007, 8:41 AM
    • Posts 2

    Hi again,

    After doing what Amanda suggested, the next step was to wrap my TreeView control with an Ajax UpdatePanel in order to do Asynchronous postbacks and refresh only the Treview control, it was then when I realized that with Ajax I didn't need the Amanda's postBackByObject approach anymore, I just attached a trigger from the UpdatePanel to a hidden button and that's it!, the button will do the Postback. See the code below:

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">                        
        <ContentTemplate>
             <asp:TreeView ID="TreeViewMenu" runat="server" DataSourceID="XmlDataSourceMenu" ShowCheckBoxes="All" BorderWidth="3px" BorderColor="White" NodeStyle-ForeColor="#5982A2" OnTreeNodeDataBound="TreeViewMenu_TreeNodeDataBound" OnPreRender="TreeViewMenu_PreRender" OnTreeNodeCheckChanged="TreeViewMenu_TreeNodeCheckChanged">
                    <DataBindings>                            
                         <asp:TreeNodeBinding DataMember="Menu" TextField="Name" Depth="0" SelectAction="None" ValueField="Id" />
                          <asp:TreeNodeBinding DataMember="Submenu" TextField="Name" Depth="1" SelectAction="None" ValueField="Id" />
                    </DataBindings>
                    <NodeStyle ForeColor="#5982A2" />
             </asp:TreeView>
             <asp:XmlDataSource ID="XmlDataSourceMenu" runat="server" DataFile="~/_resources/Menu.xml" XPath="//Menu" TransformFile="~/_resources/Menu.xslt">
             </asp:XmlDataSource>
       </ContentTemplate>
       <Triggers>
          <asp:AsyncPostBackTrigger ControlID="buttonCheck" EventName="click" />
       </Triggers>
    </asp:UpdatePanel>
    
    <asp:button ID="buttonCheck" runat="server" CausesValidation="false" />

    and the code behind is:

    buttonCheck.Attributes.CssStyle["visibility"] = "hidden";
    TreeViewMenu.Attributes.Add("onclick", string.Format("document.getElementById('{0}').click();", buttonCheck.ClientID));

     Of course there is no need to say that you must have an ScriptManager control for Ajax to work.

    Patrick.

  • Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    09-01-2007, 8:31 AM
    • Participant
      1,098 point Participant
    • Omkar Lale
    • Member since 04-12-2007, 10:38 AM
    • India
    • Posts 193

    Hi amanda and patrick,

    Thanks a lot

    I was facing this same problem of treeview.

    I had tried out with what you both had suggested. And its working perfectly fine.

    I am now using the code that patric had suggested since it doesn't not flash the page due to ajax.

    Thanks again

    Omkar

     

    Thanks & Regards,

    Omkar A. Lale

    "Courage is what it takes to stand up and speak; courage is also what it takes to sit down and listen."

    ~ Do Mark as Answer if it solves your query ~
  • Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?

    06-25-2009, 6:38 AM
    • Member
      16 point Member
    • sudhi345
    • Member since 05-26-2009, 9:20 AM
    • Posts 12


    buttonCheck.Attributes.CssStyle["visibility"] = "hidden"; TreeViewMenu.Attributes.Add("onclick", string.Format("document.getElementById('{0}').click();", buttonCheck.ClientID));


    Need an equivalent code in VB.NET


    Thanks in advance

Page 1 of 1 (10 items)