Treeviews like microsoft

Last post 08-22-2008 1:50 AM by Amanda Wang - MSFT. 3 replies.

Sort Posts:

  • Treeviews like microsoft

    08-18-2008, 11:58 AM

    Hello,

    I am trying to implement a treeview using .NET 2.0 control <asp:treeview> with checkboxes.  However, I want to be able to when check a nodes checkbox, it automatically checks all the child nodes aswell.  However, when I untick one of the child nodes, i want the parent nodes to have a square in the checkbox - same as microsoft implement their tree views.  is this possible with the standard treeview control or would i have to use some 3rd party software for this..?

     thanks for your help

     

  • Re: Treeviews like microsoft

    08-19-2008, 11:11 PM
    Answer

    Hi,

    You can try to use javascript to implement it:

     for example:

    <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" ShowCheckBoxes="All" ShowLines="True" onclick="client_OnTreeNodeChecked(this);" >
            </asp:TreeView>  

    <script language=javascript type="text/javascript">    
    function client_OnTreeNodeChecked(evt)
            {
                var obj;
                if(window.event)    obj = window.event.srcElement;
                else                obj = (evt ? evt : (window.event ? window.event : null)).target;
                var treeNodeFound = false;
                var checkedState;
                if (obj.tagName == "INPUT" && obj.type == "checkbox" )
                {
                    checkedState = obj.checked;
                    do
                    {
                      obj = obj.parentNode;
                    }
                    while (obj.tagName != "TABLE")

                    var parentTreeLevel = obj.rows[0].cells.length;
                   
                    //get the current node's parent node.
                    var tables = obj.parentNode.getElementsByTagName("TABLE");
                    var numTables = tables.length;

                    if (numTables >= 1)
                    {
                        for (i=0; i < numTables; i++)
                        {
                            if (tables[i] == obj)
                            {
                                treeNodeFound = true;
                                i++;
                                if (i == numTables) return;
                            }
                           
                            if (treeNodeFound == true){
                                var childTreeLevel = tables[i].rows[0].cells.length;
                                if (childTreeLevel > parentTreeLevel){
                                    var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                                    var inputs = cell.getElementsByTagName("INPUT");
                                    inputs[0].checked = checkedState;
                                }
                                else  return;
                            }
                        }
                    }
                }
            }
    </script>
    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: Treeviews like microsoft

    08-21-2008, 9:13 AM
    • Member
      2 point Member
    • BufnitaCiumec
    • Member since 08-19-2008, 8:09 PM
    • Posts 3

     Thanks for the response, Amanda.  It answers half of my question, probably the most difficult half.

    The other part I need an answer to is how to use an Oracle table as the data source.

     This is my Oracle DataSource:

    <%--Datasource for Employee State, County, Zip Code Work Areas--%>
    <asp:SqlDataSource ID="SqlDataSourceEmpStateZip"
                           runat="server"
                           ConnectionString="<%$ ConnectionStrings:MSHPIDb %>"
                           ProviderName="<%$ ConnectionStrings:MSHPIDb.ProviderName %>"
                           SelectCommand="SELECT STATE, COUNTY_NAME, ZIP_CODE FROM TIME_ZONE GROUP BY STATE, COUNTY_NAME, ZIP_CODE ORDER BY STATE, COUNTY_NAME, ZIP_CODE"
                           UpdateCommand="UPDATE EMP_STATE_ZIP SET STATE = :STATE, COUNTY_NAME = :COUNTY_NAME, ZIP_CODE = :ZIP_CODE WHERE LOGINID = :LOGINID">
        <UpdateParameters>
            <asp:Parameter Name="STATE" />
            <asp:Parameter Name="COUNTY_NAME" />
            <asp:Parameter Name="ZIP_CODE" />
            <asp:Parameter Name="LOGINID" />
        </UpdateParameters>
    </asp:SqlDataSource>

     But when I try to attach it to the TreeView, I get this error message:

    The DataSourceID of 'tvEmpStateZip' must be the ID of a control of type IHierarchicalDataSource.

     I have tried to export the Zip Code data as an XML file, but the TreeView will no populate properly.

    Suggestions?  Thanks for any help you can provide.

     

  • Re: Treeviews like microsoft

    08-22-2008, 1:50 AM
    Answer

    Hi,

    In fact, the Menu and TreeView  have to use hierarchical data sources. For hierarchical data sources, we have got XmlDataSource and SiteMapDataSource.

     However , the SqlDataSource and ObjectDataSource are tabular data sources.

    So if we want to populate a Menu or TreeView from a database, we will  pretty much have to do it from code.

    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.
Page 1 of 1 (4 items)