Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Suppose that we have treeview with checkbox including node1 and sub node 1.1,1.2,1.3,1.4.
i want to do this that when a user select all of the sub nodes the main node selected automatically and if user select some of the sub nodes the main node selected in other way.like a filled square.
how can i do this??????
Posted 24-Jul-11 22:14pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Desing code:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
 
        function postBackByObject() {
            var o = window.event.srcElement;
            if (o.tagName == "INPUT" && o.type == "checkbox") {
                __doPostBack("", "");
            }
        }
   </script>
 

</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All"
 
            AutoGenerateDataBindings="False" ExpandDepth="0"
               EnableClientScript="true"
            ontreenodepopulate="TreeView1_TreeNodePopulate"
            ontreenodecheckchanged="TreeView1_TreeNodeCheckChanged"
            onselectednodechanged="TreeView1_SelectedNodeChanged" >
 
        </asp:TreeView>
 

     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
 

 

    </div>
    </form>
</body>
</html>
 

Cs code :
 

 
public partial class Default2 : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    private SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
            TreeView1.Attributes.Add("onclick", "postBackByObject()");
 
            if (!IsPostBack)
            {
                string cs = "server=sql2005 ;database=dbName; uid=sa;pwd=123";
                con = new SqlConnection(cs);
                con.Open();
                SqlCommand cme = new SqlCommand("select * from tbName where parenttab=0 and roleid=1", con);
                //cme.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cme);
                DataSet ds = new DataSet();
                da.Fill(ds, "T");
 
                dt = ds.Tables["T"];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TreeNode no = new TreeNode();
                    no.Text = dt.Rows[i]["LinkName"].ToString();
                    // no.SelectAction = TreeNodeSelectAction.SelectExpand;

                    no.Value = dt.Rows[i]["LinkId"].ToString();
                    int a = Convert.ToInt32(dt.Rows[i]["LinkId"]);
 
                    // int a = Convert.ToInt32(dt.Rows[i]["LinkId"]);
                    AddTrem(no,a);
                    this.TreeView1.Nodes.Add(no);
 
                }
            }
    }
 
    public void AddTrem(TreeNode no,int a)
    {
 
        SqlCommand cme = new SqlCommand("select * from tbName where parenttab="+a, con);
        SqlDataAdapter da = new SqlDataAdapter(cme);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = new DataTable();
        dt = ds.Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            TreeNode childnode = new TreeNode();
            childnode.Text = dt.Rows[i]["LinkName"].ToString();
            childnode.Value = dt.Rows[i]["linkid"].ToString();
           // AddSubject(childnode);
            no.ChildNodes.Add(childnode);
        }
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

sorry but i dont mean how can i add or retriev the tree items.
i want to change the selection type of tree view based on all of the sub nodes are selected or just some are selected.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 775
1 OriginalGriff 359
2 Manas Bhardwaj 295
3 Abhinav S 223
4 Sampath Lokuge 215
0 Sergey Alexandrovich Kryukov 7,967
1 OriginalGriff 4,332
2 Peter Leow 3,699
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid