Click here to Skip to main content
15,882,802 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Created tree view control,node values binded from database.now i want to add,edit update and delete dynamically.Using buttons or link buttons any thing.help me

What I have tried:

C#
<pre>  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            tvwItems.Nodes.Clear();
            BindRoots();
        }

    }
    private void BindRoots()
    {
        try
        {
            Page.Header.Title = "Treeview";

            string str = "Select id as ID , sitename  From  recipe_site where isactive=1";
            SqlDataReader reader = GetData(str);

            while (reader.Read())
            {
                TreeNode rootNode = new TreeNode(reader[1].ToString(), reader[0].ToString());
                tvwItems.Nodes.Add(rootNode);
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    private void BindChilds(TreeNode node, int id)
    {

        string str = "Select site as ID , title   From recipe_category where site=" + id;
        SqlDataReader reader = GetData(str);
        while (reader.Read())
        {
            TreeNode childNode = new TreeNode(reader[1].ToString(), reader[0].ToString());
            node.ChildNodes.Add(childNode);
        }
        reader.Close();
    }


    protected void tvwItems_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (!(tvwItems.SelectedNode.ChildNodes.Count > 0))
        {
            BindChilds(tvwItems.SelectedNode, Convert.ToInt32(tvwItems.SelectedNode.Value));
            tvwItems.SelectedNode.Expand();
        }
     
    }

    private SqlDataReader GetData(string commandText)
    {
        //string strConnection = "server=cel38;database=HRMIS010710;user id=sa; connection timeout=60";
        SqlConnection conRecipes = new SqlConnection(ConfigurationManager.ConnectionStrings["constrRecipes"].ConnectionString);
        SqlConnection conUpdb = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
 
        //SqlConnection con = new SqlConnection(strConnection);
        conRecipes.Open();
        SqlCommand sqlcmd = new SqlCommand(commandText, conRecipes);
        SqlDataReader dr = sqlcmd.ExecuteReader();

        return dr;
    }
Posted
Updated 10-Apr-17 1:49am

1 solution

 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900