0

I want to get checkbox checked value from Treeview.

I bind the data from oracle database on Treeview.

Code:

   protected void Page_Load(object sender, EventArgs e)
    {

        fill_Tree2();
    }

    void fill_Tree2()
    {

        DataSet PrSet = PDataset("Select * from Activity_Parent");

        TreeView1.Nodes.Clear();

        foreach (DataRow dr in PrSet.Tables[0].Rows)
        {

            TreeNode tnParent = new TreeNode();

            tnParent.CollapseAll();

            tnParent.Text = dr[1].ToString();

            tnParent.Value = dr[0].ToString();

            tnParent.ToolTip = "Click to get Child";

            TreeView1.Nodes.Add(tnParent);

            FillChild(tnParent, tnParent.Value);

        }
    }

    public void FillChild(TreeNode parent, string ParentId)
    {

        DataSet ds = PDataset("Select * from Activity_child where Activity_ID =" + ParentId);

        parent.ChildNodes.Clear();

        foreach (DataRow dr in ds.Tables[0].Rows)
        {

            TreeNode child = new TreeNode();

            child.Text = dr["ActivityName"].ToString().Trim();

            child.Value = dr["ActivityChild_ID"].ToString().Trim();

            parent.ChildNodes.Add(child);
        }

    }

    protected DataSet PDataset(string Select_Statement)
    {

        OleDbConnection DbConnection = new OleDbConnection("Provider=MSDAORA.SNIDB;Data Source=SNIDB;User ID=AZRAAR;Password=AZRAAR");

        OleDbDataAdapter ad = new OleDbDataAdapter(Select_Statement, DbConnection);

        DataSet ds = new DataSet();

        ad.Fill(ds);

        ViewState["ds"] = ds;

        return ds;

    }

In the below Button click event,I tried but it is not working

    protected void btnGet_Click(object sender, EventArgs e)
    {
        DataSet ds1= (DataSet)ViewState["ds"];
        foreach (DataRow dr in ds1.Tables[0].Rows)
        { 
          TreeNode tn = new TreeNode();
          if (tn.Checked == true)
          {
              Response.Write(tn.Text);

          }
          tn.Text = dr["ActivityName"].ToString(); 
          TreeView1.Nodes.Add(tn); 
        } 
     }
}

In the below image if I select Checkbox HR Activity1, Developer Activity3 etc.,

I want to get that value

Treeview

Any ideas? Thanks in advance

4

2 回答 2

0

获取所有选定 TreeNode 集合的简单方法是:

private List<TreeNode> AllCheckedNodes = new List<TreeNode>();

private void GetAllCheckedNodes()
{
    for (int i = 0; i < TreeView1.CheckedNodes.Count; i++)
    {
        AllCheckedNodes.Add(TreeView1.CheckedNodes[i]);
    }
}
于 2013-09-02T09:08:59.607 回答
0

您的 btnGet_Click 方法不正确 - 您在该方法中创建了新的 TreeView 对象,而这不是您页面上存在的对象。

您需要做的是为 SelectedNodeChanged 事件创建事件处理程序。

这是您应该添加到 fill_Tree2() 方法中的一行

private void FillTree()
{
    Treeview1.SelectedNodeChanged += Treeview1_SelectedNodeChanged;

}

protected void Treeview1_SelectedNodeChanged(object sender, EventArgs e)
{
   string value = Treeview1.SelectedNode.Value;
}

另一个错误是这样编写 Page_Load 方法。这将在每次重新加载页面时加载您的treview(即使在回发时)。你想要的是这样的

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       fill_Tree2();
    }
}
于 2013-09-04T00:54:02.190 回答