Populate on demand treeview can be very fast compared to a treeview which is loaded altogether.When we click the parent node of the treeview only it's childnodes are populated, so we populate the treeview based on demand(parent node). The TreenodePopulate event is called whenever a node is clicked to expand. Do not forget to mention PopulateOnDemand as true. EnableClientScript is used to specify whether client-side validation is enabled. This will stop the post backs to the server.
public System.Web.UI.WebControls.TreeView tvCountry = new System.Web.UI.WebControls.TreeView(); string strCountry =”Country”; tvCountry.ID =”tvCountry”; tvCountry.EnableClientScript = true; //gets called whenever a node is clicked to expand. tvCountry.TreeNodePopulate += new TreeNodeEventHandler (tvCountry_TreeNodePopulate); TreeNode n2 = new TreeNode(strCountry, strCountry); n2.Expanded = false; n2.SelectAction = TreeNodeSelectAction.None; n2.PopulateOnDemand = true; n2.ShowCheckBox = false;
/* Implement logic for populating the treeview, here I have given a small test case to elaborate the functionality */
void tvGeoScope_TreeNodePopulate(object sender, TreeNodeEventArgs e) { string[] personNames = GetPersonNames(e.Node.Value);
foreach (string personName in personNames) { TreeNode nn = new TreeNode(personName, personName); nn.Expanded = false; nn.PopulateOnDemand = true; nn.ShowCheckBox = true; e.Node.ChildNodes.Add(nn); } }
public string[] GetPersonNames(string personType) { List persons = new List();
switch (personType) { case "Country": persons.Add("Europe"); persons.Add("Melanesia"); break;
case "Europe": persons.Add("Austria"); persons.Add("Begium"); break;
case "Melanesia": persons.Add("Fiji"); persons.Add("Solomon Islands"); break; }
return persons.ToArray(); }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|