The new solution a did like below. and include the save treeview's state.
public partial class WUCConferenceGroup : BaseControl
{
BLCommons blc = new BLCommons();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadTree(UIGCommons.CurrentClientID);
if (null == Session["TreeView"])
{
trvGroup.ExpandAll();
//StoreTreeViewStateToSession(trvGroup);
}
else
{
RestoreTreeViewStateFromSession(trvGroup);
}
if (UIGCommons.CurrentConfID != -1)
{
GoThrough(trvGroup.Nodes[0]);
}
}
}
protected void GoThrough(TreeNode fNode)
{
if (null != fNode.Parent)
{
if ((string.Compare(fNode.Value, UIGCommons.CurrentConfID.ToString()) == 0) && (string.Compare(fNode.Parent.Value, UIGCommons.CurrentConfGroupID.ToString()) == 0))
{
fNode.Selected = true;
}
}
foreach (TreeNode child in fNode.ChildNodes)
{
GoThrough(child);
}
}
protected void LoadTree(int clientID)
{
TreeNode node = new TreeNode();
string str1 = "Select isnull(max(ClientShortName),'') from Client where ClientID = '" + clientID + "' ";
SqlDataReader na = blc.BLCommonsGetDataAllSTP(str1);
while (na.Read())
{
ViewState["ClientShotName"] = na.GetString(0) + " Groups";
}
na.Close();
TreeNode root = new TreeNode();
root.Text = (string)ViewState["ClientShotName"];
root.Value = "0";
trvGroup.Nodes.Clear();
trvGroup.Nodes.Add(root);
string str = "Select GroupID,Title From ConferenceGroup" +
" where ConferenceGroup.ClientID = '" + clientID + "' and ParentGroupID is null order by Title";
SqlDataReader get = blc.BLCommonsGetDataAllSTP(str);
while (get.Read())
{
TreeNode child = new TreeNode();
child.Text = get.GetString(1);
child.Value = get.GetInt32(0).ToString();
trvGroup.Nodes[0].ChildNodes.Add(child);
AddChild(child, get.GetInt32(0));
}
get.Close();
AddConfsNotInGroup(root);
//trvGroup.ExpandAll();
SwitchNode();
}
protected void AddChild(TreeNode parent, int pID)
{
BLCommons blc = new BLCommons();
string str = "Select GroupID,Title From ConferenceGroup" +
" where ConferenceGroup.ClientID = '" + UIGCommons.CurrentClientID + "' and ParentGroupID='" + pID + "' order by Title";
SqlDataReader get = blc.BLCommonsGetDataAllSTP(str);
while (get.Read())
{
TreeNode child = new TreeNode();
child.Text = get.GetString(1);
child.Value = get.GetInt32(0).ToString();
parent.ChildNodes.Add(child);
AddChild(child, get.GetInt32(0));
}
if (!get.HasRows)
{
AddConfs(parent, pID);
return;
}
get.Close();
AddConfs(parent, pID);
}
protected void AddConfs(TreeNode parent,int gourpID)
{
string str1 = "Select Conference.ConfID,Name from Conference" +
" inner join ConferenceConferenceGroupLink on ConferenceConferenceGroupLink.ConfID = Conference.ConfID" +
" where GroupID = '" + gourpID + "' order by Name";
SqlDataReader get1 = blc.BLCommonsGetDataAllSTP(str1);
while (get1.Read())
{
TreeNode child1 = new TreeNode();
child1.Text = get1.GetString(1);
child1.Value = get1.GetInt32(0).ToString();
parent.ChildNodes.Add(child1);
if (-1==UIGCommons.CurrentConfID || child1.Value.Equals(UIGCommons.CurrentConfID.ToString()))
{
child1.Select();
UIGCommons.CurrentConfID = int.Parse(child1.Value);
}
}
get1.Close();
}
protected void AddConfsNotInGroup(TreeNode parent)
{
string strc = "Select ConfID,Name from Conference" +
" Where ClientID = "+ UIGCommons.CurrentClientID +" " +
" AND NOT EXISTS(SELECT 1 FROM ConferenceConferenceGroupLink WHERE ConferenceConferenceGroupLink.ConfID = Conference.ConfID) order by Conference.Name";
SqlDataReader getc = blc.BLCommonsGetDataAllSTP(strc);
while (getc.Read())
{
TreeNode childc = new TreeNode();
childc.Text = getc.GetString(1);
childc.Value = getc.GetInt32(0).ToString();
parent.ChildNodes.Add(childc);
if (-1 == UIGCommons.CurrentConfID || childc.Value.Equals(UIGCommons.CurrentConfID.ToString()))
{
childc.Select();
UIGCommons.CurrentConfID = int.Parse(childc.Value);
}
}
getc.Close();
}
public override void SwitchClient(int ClientID)
{
LoadTree(UIGCommons.CurrentClientID);
HttpContext.Current.Session.Remove("TreeView");
trvGroup.ExpandAll();
StoreTreeViewStateToSession();
}
protected void trvGroup_SelectedNodeChanged(object sender, EventArgs e)
{
SwitchNode();
}
private void SwitchNode()
{
if ((null != trvGroup.SelectedNode) && (trvGroup.SelectedNode.ChildNodes.Count == 0))
{
UIGCommons.CurrentConfID = Int32.Parse(trvGroup.SelectedNode.Value.ToString());
UIGCommons.CurrentConfName = trvGroup.SelectedNode.Text;
if (null == trvGroup.SelectedNode.Parent)
{
UIGCommons.CurrentConfGroupID = -1;
}
else
{
UIGCommons.CurrentConfGroupID = Int32.Parse(trvGroup.SelectedNode.Parent.Value.ToString());
}
}
else
{
UIGCommons.CurrentConfID = -1;
UIGCommons.CurrentConfName = "Workspace";
UIGCommons.CurrentConfGroupID = -1;
}
SoftConference soft = (HttpContext.Current.CurrentHandler as Page).Master as SoftConference;
soft.PutWorkBar();
}
public void StoreTreeViewStateToSession()
{
TreeView tvIn = new TreeView();
tvIn = trvGroup;
string strList = "";
StoreTreeViewStateToSession_Recurse(tvIn.Nodes[0], ref strList);
if (null != tvIn.SelectedNode && null != tvIn.SelectedNode.ValuePath)
{
strList = tvIn.SelectedNode.ValuePath + strList;
}
Session["TreeView"] = strList;
}
private static void StoreTreeViewStateToSession_Recurse(TreeNode tnIn, ref string strList)
{
if (tnIn.Expanded == true)
{
strList = "," + tnIn.ValuePath + strList;
}
foreach (TreeNode tnCurrent in tnIn.ChildNodes)
{
StoreTreeViewStateToSession_Recurse(tnCurrent, ref strList);
}
}
protected void RestoreTreeViewStateFromSession(TreeView tvIn)
{
if (Session["TreeView"] + "" != "")
{
string strSelectedNodeIndex = "";
foreach (string strCurrent in Session["TreeView"].ToString().Split(','))
{
if (strSelectedNodeIndex == "")
{
strSelectedNodeIndex = strCurrent;
}
else
{
try
{
tvIn.FindNode(strCurrent).Expanded = true;
}
catch
{
}
}
}
try
{
TreeNode tnTest = tvIn.FindNode(strSelectedNodeIndex);
tvIn.FindNode(strSelectedNodeIndex).Select();
((TreeNode)tvIn.FindNode(tvIn.SelectedNode.ValuePath).Parent).Expanded = true;
}
catch
{
}
//HttpContext.Current.Session.Remove("TreeView");
}
}
}//end of class
}
below is where call the save treeview state
if (null != Session["MenuFirstID"] && string.Compare((string)Session["MenuFirstID"], "3") == 0)
{
WUCConferenceGroup cg = (WUCConferenceGroup)pnlShow.FindControl("ConfsGroup");
if (null != cg)
{
cg.StoreTreeViewStateToSession();
}
}