Refresh Treenode parent to show most current child nodes
I am able to select and expand to a particular tree node programmatically, but unable to refresh it to reflect most current data in the table. Is there Treeview1.Refresh() method ? or something like that to effect? any help will be appreciated. I have a treeview and I am adding child nodes to a parent node by having the user enter data and click on a button. After that insert into the table is done, I want the parent node to refresh and show all child entries.
protected void PopulateNode(Object sender, TreeNodeEventArgs e)
{
switch (e.Node.Depth)
{
case 0:
PopulateChild(e.Node);
break;
default:
//PopulateChild(e.Node);
break;
}
}
protected void PopulateChild(TreeNode node)
{
DataSet ResultSet = RunQuery("Select Id From tbl");
if (ResultSet.Tables.Count > 0)
{
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
TreeNode newNode = new TreeNode();
newNode.Text = row["Id"].ToString();
newNode.Value = row["Id"].ToString();
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(newNode);
}
}
}
DataSet RunQuery(String QueryString)
{
String ConnectionString = "asdasdasdasd";
OleDbConnection DBConnection = new OleDbConnection(ConnectionString);
OleDbDataAdapter DBAdapter;
DataSet ResultsDataSet = new DataSet();
try
{
DBAdapter = new OleDbDataAdapter(QueryString, DBConnection);
DBAdapter.Fill(ResultsDataSet)开发者_StackOverflow;
DBConnection.Close();
}
catch (Exception ex)
{
if (DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
}
}
return ResultsDataSet;
}
The only option is to reload the datatable and recreate the nodes as here http://www.tek-tips.com/faqs.cfm?fid=6177
The best way to work with Treeview is with XMLDataSource
. You can bind TreeView with it and rebind it rather than looping through rows/cols and creating each node.
Ref: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/navigation/treeview.aspx
This link gives you and idea to convert other datasource to XMLDataSource
and then use it to bind to treeviewl:
Once you have treeview bind to xmldatasbource, you need to refresh your datasource and then treeview with DataBind().
<datasource>.DataBind()
treeview1.DataBind()
Also, set EnableCaching = false
for objectdatasource.
http://forums.asp.net/p/1083582/1609724.aspx
The TreeView
you are using has an event SelectedNodeChanged
which is fired on whatever events are specified in the value of the TreeNodeSelectAction
property of that TreeNode
. In your case the value is Expand
as I can see in your code.
When you expand a node the SelectedNodeChanged
is fired and handled by the PopulateNode
function as specified by the OnSelectedNodeChanged
property that is assigned in the html.
What we can do here includes :
A- Collapse if already expanded then expand a tree node to repopulate its children ..
B- Change the TreeNodeSelectAction
property value to SelectExpand
. Here we've got one more case in which the node will be repopulated and that is node selection.
C- Call PopulateNode
function whenever we need and assign a TreeNode
object to its sender
parameter and a new TreeNodeEventArgs()
to its e
parameter.
References:
TreeNodeSelectAction Enumeration, TreeView.TreeNodePopulate Event, TreeNode.SelectAction Property
精彩评论