Populate a treeview -web-
VB Code:
void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
SqlConnection cn = new SqlConnection(Cnstring);
string SqlString = "Select CHR_ROOT, CHR_DESCRIPCION from SIS_MATRIZ_MENU Order by chr_root";
SqlCommand cmd = new SqlCommand(SqlString, cn);
cn.Open();
using (cn)
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
TreeNode newNode = new TreeNode();
newNode.PopulateOnDemand = true;
newNode.Text = reader["CHR_DESCRIPCION"].ToString();
newNode.Value = reader["CHR_ROOT"].ToString();
e.Node.ChildNodes.Add(newNode);
}
}
}
This code is to populate a treeview.
I want to:
1. Run this code as soon as the page is loaded or
2. Run the TreeView1_TreeNodePopulate from anywhere
How can i do this
Re: Populate a treeview -web-
I take it you are using ASP.NET 2.0, which is information that you should probably include rather than us having to investigate to find out. That is an event handler for the TreeNodePopulate event of TreeNode1, so it is intended to be executed when that event is raised. If you want to execute that code other times then you should take it out of that event handler and put it in another method, which you can then call from that event handler, the form's Load event handler or anywhere else.