Click here to Skip to main content
15,894,343 members

Comments by Member 15095035 (Top 1 by date)

Member 15095035 5-Apr-21 2:57am View    
I had used another method to get it done, but facing another small issue which is after i close the program and run again, the treeview showing the state before i close the program.. i need the state to be default which is all collapse..how to solve it..

In master page .aspx

window.onbeforeunload = function () {
try {
// The expanded state of the treeview is available in the hidden input
// ..._ExpandState. Store that string in a cookie so we have it on the server
// side when the page is loaded in order to restore the state of the treeview.
var expInfo = document.getElementById('<%=TreeView1.ClientID%>' + '_ExpandState');
if (expInfo)
{
createCookie('ToolsTVExpand', expInfo.value, 365);
}
}
catch (err) {
// Ignore the error
}
}
function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}


In behind code :

<protected void="" page_load(object="" sender,="" eventargs="" e)
{
="" if="" (!ispostback)
="" reloadtreeviewstate();
}
public="" reloadtreeviewstate()
{
="" try
="" {
="" httpcookie="" cookie="Request.Cookies[" toolstvexpand"];
"="" (cookie="" !="null" &amp;&amp;="" cookie.value="" int="" curridx="0;
" foreach="" (treenode="" mainnode="" in="" treeview1.nodes)
="" cookie.value,="" curridx);
="" }
="" catch="" (exception="" e)
="" just="" keep="" going
="" }
}
protected="" setnodestates(treenode="" node,="" string="" stateinfo,="" curridx)
{
="" (curridx="">= stateInfo.Length)
throw new Exception("State info shorter than list of nodes.");
Char state = stateInfo[currIdx];enter code here
if (state == 'e')
{
node.Expand();
}
currIdx++;
if (node.ChildNodes != null && node.ChildNodes.Count != 0)
{
foreach (TreeNode childNode in node.ChildNodes)
{
currIdx = setNodeStates(childNode, stateInfo, currIdx);
}
}
return currIdx;

}