Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 700
  • Last Modified:

(ASP.Net - C#) how can I get the checked value from the check box in tree view?

Dear all, I'd like to ask, how can I get the checked value from the check box in tree view in ASP.net C#, and if i only check the child node, how can i get the corresponding root value as well?

Thank u very much!
0
rajatop
Asked:
rajatop
  • 2
1 Solution
 
guru_samiCommented:
where and when you want to get this checked value?
If you are using TreeNodeCheckChanged event then you can get the parent by:
e.Node.Parent.

The idea is get hold of the node and then access its Parent Property.
0
 
rajatopAuthor Commented:
this is the code in View.aspx:
<asp:TreeView ID="trvTable" runat="server" ShowCheckBoxes="All" onclick="SelectAllChildNodes()">
                    </asp:TreeView>


this is my code in code behind page:
private void getTableTree()
    {
        tmp = commonfunction.getTemp();

            SqlConnection SqlCon = new SqlConnection("server=" + tmp.ServerName + ";Trusted_Connection=Yes;");
            SqlCon.Open();
            SqlCommand cmd = new SqlCommand();
            string tableName = "";
            SqlDataReader rdr;

            string sql = "SELECT * FROM " + tmp.DatabaseName + ".sys.tables";
            cmd = SqlCon.CreateCommand();
            cmd.Connection = SqlCon;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            rdr = cmd.ExecuteReader();
           
            while (rdr.Read())
            {
                tableName = rdr.GetString(0);
                TreeNode masterNode = new TreeNode((string)tableName);
                trvTable.Nodes.Add(masterNode);
                getFieldTree(tableName, masterNode);
                counter++;
            }

            hidCounter.Value = Convert.ToString(counter);
           
        }


  private void getFieldTree(string TableName, TreeNode masterNode)
    {
        tmp = commonfunction.getTemp();
            SqlConnection connect = new SqlConnection("server=" + tmp.ServerName + ";Initial Catalog=" + tmp.DatabaseName + ";Trusted_Connection=Yes;");

            string strSQL = "SELECT * FROM [" + TableName + "]";
            SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSQL, connect);
            connect.Open();
            DataSet objDataSet = new DataSet();

            objDataAdapter.Fill(objDataSet, TableName);
            objDataAdapter.Dispose();

            DataTable dt = objDataSet.Tables[0];
            foreach (DataColumn dc in dt.Columns)
            {
                TreeNode childNode = new TreeNode(Convert.ToString(dc.ColumnName));
               
                masterNode.ChildNodes.Add(childNode);

            }

           
        }
could u give me the sample, after we check the check box, how can we get the checked value? Thank u so much!
0
 
guru_samiCommented:
--->could u give me the sample, after we check the check box, how can we get the checked value? Thank u so much!

modify your TreeView markup like below:

<asp:TreeView ID="trvTable" runat="server" ShowCheckBoxes="All"
        ontreenodecheckchanged="trvTable_TreeNodeCheckChanged">

code-behind:
protected void trvTable_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.Checked)
        {
            Response.Write(e.Node.Text + " Checked");
        }
        else
        {
            Response.Write(e.Node.Text + " UnChecked");
        }
    }

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now